2017-06-19 7 views
-5

私のコードメソッドでは、scanner.hasNext()は常にfalseの結果を返します。私はデバッグされたコードを理解しました。誰が何が問題なのか知っていますか?私はファイルをチェックした。ファイルは文字列で始まります。java Scanner.hasNext always false

public class TextFileImplementor { 
    public static void main (String [] args){ 
     System.out.println("Enter the name of file\n"); 
     Scanner scanner = new Scanner(System.in); 
     File file = new File(scanner.nextLine()); 
     try { 
      scanner = new Scanner(file); 
      StringBuilder stringBuilder = new StringBuilder(); 
      while (scanner.hasNext()){ 
       stringBuilder.append(scanner.nextLine()); 
      } 
      String [] strings = stringBuilder.toString().split("\\. "); 
      File newFile = new File("new_" + file.getName()); 
      FileWriter fileWriter = new FileWriter(newFile, true); 
      for (String string : 
        strings) { 
       if (string.length()<141){ 
        fileWriter.write(string + "\n"); 
       } 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 


    } 
} 
+2

おそらくファイルが空ですか? –

+1

'scanner.nextLine()'を呼び出す場合は、 'scanner.hasNextLine()'で確認してください。しかし、おそらく問題はありません。 –

+0

@AndyTurnerがscanner.hasNextLine()に変更されました。問題は解決されていません。 –

答えて

0

問題が解決しました。私は入力ファイルのエンコーディングに問題がありました。そして私は追加しなければならなかった

fileWriter.flush(); 
fileWriter.close(); 
関連する問題