2015-10-10 6 views
0

Текст печатает в бесконечном цикле. Любой способ избежать этого?Печать текста в бесконечном цикле

if (eLoad) { 
    File file = new File("reservation.txt"); 

    /** reading a file */ 

    try(FileInputStream fis = new FileInputStream(file)) { 
     int content; 
     while ((content = fis.read()) != -1) { 
      // convert to char and display it 
      System.out.print((char) content); 
      /**print out the result contents of the file. */ 
     } 
    } 

    catch(IOException e) { 
     e.printStackTrace(); 
    } 
} 

Ooutput показано

Homeworkdue Сб 10 октября 2015 00:00:00 PDT 23:00 23
Homeworkdue Сб 10 октября 2015 00:00:00 PDT 23:00 23
Homeworkdue сб 10 октября 00:00:00 PDT 2015 23:00 23

+0

Какое содержимое содержит ваши текстовые файлы? –

+1

ЗДЕСЬ ЛИНИЯ ДЛЯ РЕШЕНИЯ ЭТИХ ПРОБЛЕМ http://stackoverflow.com/questions/731365/reading-and-displaying-data-from-a-txt-file –

ответ

0
if (eLoad) { 
    File file = new File("reservation.txt"); 

    /** reading a file */ 

    try(FileInputStream fis = new FileInputStream(file)) { 
     int content; 

     BufferedReader reader = new BufferedReader(new InputStreamReader(fis)); 
     String line = null; 

     while ((line = reader.readLine()) != null) { 
     // convert to char and display it 
      System.out.print(line); 
      /**print out the result contents of the file. */ 
     } 

     reader.close(); 
    } 

    catch(IOException e) { 
     e.printStackTrace(); 
    } 
} 
+0

Можете ли вы объяснить, почему вы считаете это полезным? Потому что это не так. – Tom

+0

Если чтение файла заканчивается, тогда его значение null возвращается, а затем останавливает выполнение, которое прерывает цикл. –

+0

Итак, вы все еще думаете, что 'int' variable' content' может быть 'null'? – Tom

0

вы можете добавить

System.out.print(content); 

после

System.out.print((char) content); 

Я не очень хорошо осведомлен еще в программировании, но это может быть потому, что она читает fis.read() как значения ASCII, и это либо 000 или 003.

Если это так, я бы поставил

content = fis.read()); 
while (content != 3 && content != 0 || content == null) // if either not 3 and not 0 it doesn't run or if it isn't defined 
{ 
    // do stuff 
} 

для условия в то время как

«Это означает, что API пытается прочитать строку файла, получить -1 и вернуть EOF». (Похоже, что он не возвращает -1)

Смотрела: https://superuser.com/questions/789026/eof-ascii-hex-code-in-text-files

0

Если вы хотите читать построчно из файла можно также попробовать

while ((line = fis.readLine()) != null) { 

Остальное кода выглядит отлично.

0
if (eLoad) { 
    File file = new File("reservation.txt"); 

    /** reading a file */ 

    try(FileInputStream fis = new FileInputStream(file)) { 
     int content; 
     while ((content = fis.read()) != 0) //JUST CHANGE THE -1 TO 0, THEN IT WORKS { 
      // convert to char and display it 
      System.out.print((char) content); 
     /**print out the result contents of the file. */ 
    } 

    catch(IOException e) { 
     e.printStackTrace(); 
    } 
} 
Смежные вопросы