2017-08-07 8 views
0

私のルートプロジェクトディレクトリにある.txtファイルを読み込むために次のコードを使用していますが、ファイルのボディ全体。私のコードは次の通りです:スキャナで.txtファイルを読み込もうとすると、テキストが失われる

public void readFile() throws IOException { 
    int index = 0; 
    int indexT = 1; 
    File fileName = new File(file); 
    Scanner inFile = new Scanner(fileName); 
    while (inFile.hasNext()) { 
     String line = inFile.nextLine(); 
     System.out.println(line); 
     if (indexT%3 == 0) { 
      fileList[index] = inFile.nextLine(); 
     } else if (indexT == 1 || indexT == 4 || indexT == 7){ 
      playList[index] = inFile.nextLine(); 
     } else { 
      break; 
     } 
     index++; 
     indexT++; 
    } 
    inFile.close(); 
} 

私は同じ質問をして、私のコードで問題を特定できませんでした。私が見る限り、それは完璧に働くはずです。すべての助けに感謝します!

答えて

3

あなたはループを1行ずつ読み込んでいますが、それから2行目でそれを抜け出しています。それ以上は進んでいません。

while (inFile.hasNext()) { 
    if (indexT%3 == 0) { 
     //... 
    } else if (indexT == 1 || indexT == 4 || indexT == 7){ 
     //... 
    } else { 
     break; 
    } 
    indexT++; 
} 

indexTは1で始まります。これは大文字と小文字がありますが、それは増分されます。今回は、特別なケースはないので、「ブレーク」に達します。そしてループから抜け出す。だから、それ以上の行は読まれない。

関連する問題