I tried to use a Java BufferedReader to read an empty txt file.
Here's my code:
File recentFile = new File(path);
try {
BufferedReader reader = new BufferedReader(newInputStreamReader(newFileInputStream(recentFile), "UTF-8"));
String temp = reader.readLine();
reader.close();
if (temp == null) {System.out.println("your file is empty");}
else {System.out.println(temp);}
} catch (IOException ex) {}
The txt file is completely empty, but when I run the program, The command prompt prints out "?" instead of "your file is empty".
When I change "UTF-8" to "Unicode", and change my txt file encoding format to Unicode, I get a "your file is empty" from the prompt.
Why do I get this result when I use UTF-8?
btw, if this is a duplicate please let me know, I tried to search this multiple times on google but couldn't find anything helpful to me.
The file is not completely empty; that is the only explanation. Most likely there is a byte order mark at the start. This doesn't look like a character (if you open the file in notepad, it'll probably show up as seemingly completely empty), but it does count.
Note that I believe BR will probably return 1 empty string first before it starts returning null; however, that is not what's happening here (if it was, you wouldn't have seen your program print ?
).
You can check the actual bytes that are there with a hex editor. Alternatively, this snippet of java code will tell you:
try (var in = new FileInputStream("/path/to/the/file")) {
for (int c = in.read(); c != -1; c = in.read()) {
System.out.print("%02X", c & 0xFF);
}
}
System.out.println();