i have a problem trying read a file on java with MacBook OS Mojave version- 10.14.1 with eclipse version Oxygen.3a Release (4.7.3a).
when i run this: BufferedReader in = new BufferedReader(new FileReader("Macintosh HD/Users/Username/Desktop/Java/Hi.txt"));
when i run the code it appears: java.io.FileNotFoundException: ("/Macintosh HD/Users/Username/Desktop/Java/Hi.txt") (No such file or directory)
is there something another way to find the path to file or something that I'm missing writing the actual path to file?
I'm expecting the output to be a String "this is line 1"
To determine the full path to file on your Mac:
The full path is written into the Terminal. You may copy the text from there. Paste into your Java source code.
See the Java Tutorial by Oracle.
Here is a complete example in a single .java
file.
package com.basilbourque.example;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
public class FileReader {
public static void main ( String[] args ) {
FileReader app = new FileReader();
app.doIt();
}
private void doIt ( ) {
String pathText = "/Users/basilbourque/Desktop/Hi.txt";
Path path = FileSystems.getDefault().getPath( pathText );
try {
Files.lines( path ).forEach( s -> System.out.println( s ) );
} catch ( IOException e ) {
System.out.println( "ERROR - IOException while reading the `Hi.text` file. Message # 8640b80f-49a1-4ee7-992c-c661ef4cf38e." );
e.printStackTrace();
}
}
}
When run.
Bonjour
Olá
Hello
Sawa
Hei
Halló
こんにちは
Salve
'Macintosh HD' is just a name Finder gives to the main disk, it isn't used as part of the file path. So your file path would be:
/Users/Username/Desktop/Java/Hi.txt
This assumes your user name is 'Username' and the file is in a folder 'Java' in the 'Desktop' folder.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.