Weird exception on thread "main" I / O Java

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

Exception in thread "main" \src\product.txt (No such file or directory)
at Method)
at dao.Inventory.readFile(
at view.InventoryView.init(
at view.InventoryView.<init>(
at view.InventoryView.main(


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();
                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?)


source to share

3 answers


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.



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 )



  • 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) {
    return builder.toString();

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




All Articles