Java says non-empty file is empty?

I have a specific file that says Java is empty ...

Source

import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class MinimumWorkingExample { public static void main(String[] args) throws FileNotFoundException { String filename = "/home/tyson/Data/English-French_test/test/test.f"; Scanner fileIn = new Scanner(new File(filename)); System.out.println("***START***"); while(fileIn.hasNextLine()) { System.out.println(fileIn.nextLine()); } System.out.println("***FINISH***"); } } 

Output

 ***START*** ***FINISH*** 

... but the file is not empty:

Console

 tyson@tyson-desktop :~$ head /home/tyson/Data/English-French_test/test/test.f <s snum=0001> 2 . </s> <s snum=0002> 2 . </s> <s snum=0003> oh , oh ! </s> <s snum=0004> oh , oh ! </s> <s snum=0005> oh , oh ! </s> <s snum=0006> souvenons - nous , monsieur le Orateur , que ce sont ces secteurs de notre soci t  qui servent de  pine dorsale   notre  conomie . </s> <s snum=0007> bravo ! </s> <s snum=0008> bravo ! </s> <s snum=0009> monsieur le Orateur , ma question se adresse   le ministre charg  de les transports . </s> <s snum=0010> tous deux poss dent de nombreuses ann es de exp rience dans la fabrication et la distribution de les produits forestiers . </s> tyson@tyson-desktop :~$ 

Question

Why is this happening???

+4
source share
3 answers

Also make the scanner fileIn = new scanner (new file (file name), "Cp1252"); since it is an encoding for the French language, and your system seems UTF-8. The scanner may have encoding problems if it thinks to read UTF-8 multibytes.

+3
source

Perhaps you are missing the default separator for the scanner, so it sees your entire file as one line without end, so hasNextLine () is false. Make sure the character you got from

 Scanner.delimiter() 

present in your file. If they do not match, you can use

 Scanner.useDelimiter("\\s or your regex/string here") 

to set it to the correct one.

0
source

According to Java Docs, line separators are any of the following. Does your file contain?

 private static final String LINE_SEPARATOR_PATTERN = "\r\n|[\n\r\u2028\u2029\u0085]" 
0
source

Source: https://habr.com/ru/post/1387488/


All Articles