Search code examples
javaiofilenotfoundexception

FileNotFoundException for a valid file using FileInputStream


When I write an FileInputStream, while I have the valid file, it throws a FileNotFoundException. I used this:

package io;
import java.io.*;
public class implementIo {
    public static int i;
    public static FileOutputStream output;
    public static FileInputStream input;
    public static void main(String args[]) {
    try {
                output = new FileOutputStream("writeModification.txt");
            input = new FileInputStream("modification.txt");
            do {
        i = input.read();
        if(i != -1) output.write(i);
        }while(i != -1);
    } catch (Exception e) {
        System.out.println("Exception caught " + e);
    } finally {
        try {
            if(output == null) input.close();
        }catch(IOException e) {
            System.out.println("IOException caught: " + e);
        }
    }
}

}

While I had a two separate files named "modification.txt" and "printModification.txt" in the same package folder, yet the system threw a FileNotFoundException. Please help!


Solution

  • This is because the FileInputStream doesn't provide the file creation during initialization like new FileOutputStream() does. So if these have been said, we can see one interesting thing to keep in mind: the modification.txt will be created every time when you initialize the FileOutputStream (and won't be overwritten) and this is why most probably your code breaks at the new FileInputStream() line.

    How can you handle your exception? You either create your file before executing the code( manually with New -> Text Document etc. ) or modify your code and make use of File class :

    File file = new File("modification.txt");
        try {
            file.createNewFile();
            input = new FileInputStream(file);
            //your code here - output etc.
    

    Your code still doesn't work even if you have the files created in the same package folder? It's because the default path that your streams are looking for your files is the current working directory. Here is an example :

    myproject
       |___src
       |    |___main
       |          |___java
       |                |___io
       |                     |___implementIo
       |___writeModification.txt
       |___modification.txt
    

    This is the correct structure if you want to use the streams like you did (with just a simple file name in stream constructor argument). But if your files are not there, you have to specify the absolute path. Here is an example :

     myproject
       |___src
            |___main
                  |___java
                        |___io
                             |___implementIo
                             |___writeModification.txt
                             |___modification.txt
    

    And the correct way to access the files is this:

    FileInputStream input = new FileInputStream("C://myproject//src//main//java//io//modification.txt");
    

    Same for the output stream. (Please modify the path with your correct file location)