-2

I am doing a program which reads data from a text file in my macbook and then print asterisks. The problem is that Eclipse can't even find the text file. I checked the file directory multiple times to see if it is right. I just don't know why I am getting this error. It would be really helpful If I could get solutions or suggestions.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class FileIO 
{
    public static void main (String [] args) throws FileNotFoundException 
    {
        int num;
        Scanner reader = new Scanner (new File ("/User/3020418/Desktop/MyData.txt"));
        PrintWriter  writer = new PrintWriter (new File("/⁨Users/3020418⁩/⁨Desktop⁩/Output.txt"));  
        while (reader.hasNext())  
        {   // reads until EOF 
              num = reader.nextInt(); 
             System.out.print(num);
              if(num > 0)
              {
                  for (int i = 1; i < num; i++)
                  {
                      writer.print("x");
                  }
              }

              if(num == 0)
              {
                  writer.print(" ");
              }

              if(num < 0)
              {
                  for(int i = Math.abs(num); i > 0; i--)
                  {
                      writer.print("\n");
                  }
              }
        }
        writer.close();
        reader.close();


}

}

Here is the full error that I keep getting:

Exception in thread "main" java.io.FileNotFoundException: /User/3020418/Desktop/MyData.txt (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.util.Scanner.<init>(Scanner.java:611)
at FileIO.main(FileIO.java:11)

3 Answers3

3

If you are on a Mac, then it should be /Users/3020418/Desktop/MyData.txt, you were missing an s at the end of User... maybe?

craigpastro
  • 795
  • 5
  • 14
  • Really? And you already checked the file exists? And that you can read it? And you did change line 11 to look like `Scanner reader = new Scanner (new File ("/Users/3020418/Desktop/MyData.txt"));` Is it the same error? – craigpastro Oct 16 '19 at 00:31
  • Did you try the solution by @George Z? That is certainly the better way to go in the first place. – craigpastro Oct 16 '19 at 00:34
1

Instead of using the absolute path as a String and messing with file separators, use System.getProperty("user.home") in order to get home directory and then use File class costructor to build the path step-by-step/file-by-file:

File desktop = new File(System.getProperty("user.home"), "Desktop");
File myDataTxt = new File(desktop, "MyData.txt");
George Z.
  • 6,643
  • 4
  • 27
  • 47
0

Use System.getProperty("user.home") to navigte to home directory, then locate your file.

String home = System.getProperty("user.home");
File f = new File(home + File.separator + "Desktop" + File.separator + "MyData.txt");

In order to be independent on OS, use File.separator intead of front or back slashes.