0

I have this code but when I run it, it can not read the file and I get this error:

Exception in thread "main" java.io.FileNotFoundException: PBM.txt (The system cannot find the file specified)

is anybody can help me which part of the code has problem? I made a file with the name:"PBM" in my package, but it does not work!

here is my code:

            import java.util.Scanner;
            import java.io.*;

            public class PBM {

                private int[][] bits;
                private int rows, columns;

                PBM() throws IOException {

                    Scanner PBM = new Scanner(new File("PBM.txt"));
                    if (!PBM.next().equals("P1"))
                        throw new IOException("Format error");
                    columns = PBM.nextInt();
                    rows = PBM.nextInt();
                    bits = new int[rows][columns];
                    for (int i = 0; i < rows; i++) {
                        String line = PBM.next();
                            for (int j = 0; j < columns; j++)
                                bits[i][j] = line.charAt(j) - '0';
                    }
                }

            public String toString() {

                String result = "";
                for (int i = 0; i < rows; i++) {
                    for (int j = 0; j < columns; j++) {
                        if (bits[i][j] == 1)
                            result += "*";
                        else
                            result += " ";
                    }
                    result += "\n";
                }
                return result;
            }

            public static void main(String a[]) throws IOException {

                PBM Ob = new PBM();
                System.out.println(Ob);
            }

    }
SteliosKts
  • 65
  • 1
  • 9
  • what's the folder structure of the project? – Ramanlfc Jan 22 '18 at 18:00
  • The "PBM.txt" file will be looked in the current working folder of your process, so it really depends on where did you create the file. By default, your working folder is your project root folder, so if your file is not there, you'll need to either provide path to it, or change the working folder. See https://stackoverflow.com/questions/32312124/launch-java-application-main-from-specified-directory for more details. – Alex Savitsky Jan 22 '18 at 18:03
  • Please format your code. It looks like a mess – and it probably is. – MC Emperor Jan 22 '18 at 18:39

2 Answers2

1

Add this (just before your read the file) to find your current working directory:

System.out.println("Working Directory = " + System.getProperty("user.dir"));

and then move PBM.txt file in the correct directory and the program should work.

NB: don't forget to remove the above line once you're done!

Boris
  • 22,667
  • 16
  • 50
  • 71
  • Now just move PBM.txt in the directory which was printed out "Working Directory =..." just above the error message. – Boris Jan 22 '18 at 18:11
  • what do you mean by moving file to directore? –  Jan 22 '18 at 18:20
  • You can cut and paste the file in the correct directory, can you not? – Boris Jan 22 '18 at 18:27
  • Can you see a message being printed out "Working Directory =..." ? If not make sure the line System.out.println("Working Directory... is before Scanner PBM = new... line. – Boris Jan 22 '18 at 18:34
  • I checked that address and file exist there! –  Jan 22 '18 at 19:57
  • So you don't get FileNotFoundException any more, correct? – Boris Jan 22 '18 at 20:18
  • could you please tell me what "throw new IOException("Format error")" mean? or what is doing in this code? thanks! –  Jan 22 '18 at 20:30
  • The code checks that the PBM.txt file starts with "P1", if it doesn't an IOException is being thrown and the program terminates. – Boris Jan 23 '18 at 08:43
0

This exception happen because either the path to file is wrong, in your case, or the file doesn't exist.

Try to valid the path and check if the file exist. Also, as @Boris said, use the current working path to avoid a relative path as can be "PBM.txt" and to be sure what the correct path is where you application is looking for the file:

    String path = System.getProperty("user.dir") + File.separator + "PBM.txt";
    System.out.println("PATH: " + path);

    File f = new File(path);
    if(!f.exists())
    {
        //throw new IOException("File was not founded.");
    }
    Scanner PBM = new Scanner(f);

In this example the output is:

PATH: /Users/jorgeomarmedratorres/Documents/workspace/Dummy/PBM.txt
Throw File was not founded.

In my case the current working path is "/Users/jorgeomarmedratorres/Documents/workspace/Dummy"

Jorge Omar Medra
  • 978
  • 1
  • 9
  • 19
  • I checked that address and file exist there –  Jan 22 '18 at 19:58
  • Well, the error exception says another thing: `java.io.FileNotFoundException: PBM.txt (The system cannot find the file specified)`. It could be useful if you shared us an output with the path that your program is used to find the file. – Jorge Omar Medra Jan 22 '18 at 20:07