Weird exception on thread "main" java.io.FileNotFoundException I / O Java

I have this error when I try to read a file:

Exception in thread "main" java.io.FileNotFoundException: \src\product.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at dao.Inventory.readFile(Inventory.java:30)
at view.InventoryView.init(InventoryView.java:33)
at view.InventoryView.<init>(InventoryView.java:21)
at view.InventoryView.main(InventoryView.java:211)

      

But the thing is, I have a product.txt file in my src folder.

My code is as follows:

  public void readFile() throws IOException {
            // input file must be supplied in the first argument
            InputStream istream;
                    File inputFile = new File("\\src\\product.txt");
                    istream = new FileInputStream(inputFile);

                    BufferedReader lineReader;
                lineReader = new BufferedReader(new InputStreamReader(istream));
                    String line;

                        while ((line = lineReader.readLine()) != null) {

                StringTokenizer tokens = new StringTokenizer(line, "\t");

                // String tmp = tokens.nextToken();
                // System.out.println("token " + tmp);
                ActionProduct p = new ActionProduct();
                prodlist.add(p);
                String category = p.getCategory();
                category = tokens.nextToken();
                System.out.println("got category " +category);

                int item = p.getItem();
                item = Integer.parseInt(tokens.nextToken());

                String name = p.getName();
                System.out.println("got name " +name);

                double price = p.getPrice();
                price = Double.parseDouble(tokens.nextToken());

                int units = p.getUnits();
                units = Integer.parseInt(tokens.nextToken());
            }
        }

      

I don't think there is anything wrong with my code. Also, I saw a similar post about a hidden extension like FILE.TXT.TXT, how would you show a hidden extension in MacOSX ?? Any suggestions? (Maybe there is another problem other than the hidden extension problem?)

+3


source to share


3 answers


/src/product.txt

is an absolute path, so the program will try to find the file in the src folder of your root path (/). Use src/product.txt

to tell the program to use this as a relative path.



+6


source


Is it possible (most likely?) That your Java code is not executing inside the parent src folder, but instead inside the "class" or "bin" folder with java compiled .class files.

Assuming 'src' and 'bin' are in the same directory, you can try ..\\src\\product.txt



See also http://en.wikipedia.org/wiki/Path_(computing )

+1


source


  • As other commenters have pointed out, the path is absolute and points to \ src \ product.txt, which is (hopefully) not where your sources are stored.

  • The path separator must be set regardless of OS using the System.getProperty ("path.separator") property. On a Unix system, you will have trouble hard-coding backslashes as path separators. Keep it portable!

String pathSeparator = System.getProperty("path.separator");
String filePath = "." + pathSeparator + "src" + pathSeparator + "product.txt";
File file = new File(filePath);

      

or better yet:

// this could reside in a non-instantiable helper class somewhere in your project
public static String getRelativePath(String... pathElements) {
    StringBuilder builder = new StringBuilder(".");
    for (String pathElement : pathElements) {
        builder.append(System.getProperty("path.separator");
        builder.append(pathElement);
    }
    return builder.toString();
}

// this is where your code needs a path
...
new File(getRelativePath("src", "product.txt");
...

      

+1


source







All Articles