2011-12-22 44 views
4

我有一個particular file是Java的說是空的...Java說一個非空文件是空的?

源代碼

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***"); 
    } 
} 

輸出

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

...但該文件不爲空:

控制檯

[email protected]:~$ 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> 
[email protected]:~$ 

問題

這究竟是爲什麼???

+0

我試着用附加文件的程序,我能夠正確讀取文件的內容。 – 2011-12-22 02:25:06

+0

@Jugal Joop建議,由於文件是法文的,我應該使用'Scanner fileIn = new Scanner(新文件(文件名),「Cp1252」);'正確讀取文件。此修補程序適用於我。出於好奇,知道爲什麼你的系統默認可以正確閱讀法文(但我的不能)? – 2011-12-22 02:33:11

回答

3

另外做掃描儀fileIn =新掃描儀(新文件(文件名),「Cp1252」);因爲這是法語的編碼,並且您的系統似乎是UTF-8。 如果掃描儀認爲要讀取UTF-8多字節,可能會遇到編碼問題。

0

您可能缺少掃描儀的默認分隔符,因此它將整個文件看作一行而沒有結束,因此hasNextLine()爲false。確保您獲得的字符爲

Scanner.delimiter() 

存在於您的文件中。如果它們不匹配,則可以使用

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

將其設置爲正確的一個。

+0

也做掃描儀fileIn =新掃描儀(新文件(文件名),「Cp1252」);因爲這是法語的編碼,並且您的系統似乎是UTF-8 – 2011-12-22 02:17:39

+1

即使整個文件只有一行,「hasNextLine()」應該爲該第一行返回「true」,對不對?那麼,當然,它會隨後返回「false」。 – 2011-12-22 02:18:32

+0

@Joop你建議使用'掃描儀fileIn =新掃描儀(新文件(文件名),「Cp1252」);'工作!做一個正式的回答說,我會接受它:) – 2011-12-22 02:25:55

0

根據Java文檔,行分隔符是以下任何一個。你的文件是否包含任何?

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