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)