2014-07-26 31 views
0

我正在嘗試打開,關閉和寫入文件。每當我嘗試打開一個文件時,如果它不存在於我提供的程序告訴我的路徑中。如果存在,程序將讀取裏面的內容並顯示它。如果用戶不想查找文件,則可以選擇創建文件並填充數據。在文件I/O期間來自未知源的Java掃描程序異常

它工作到目前爲止,除了每當我讀取文件,它成功顯示數據後,我得到一個未知的源掃描異常。這是我到目前爲止的代碼:

import java.util.Scanner; 
import java.io.File; 
import java.io.IOException; 
import java.io.PrintWriter; 

public class FileIO { 
    public static void main(String[] args) throws IOException{ 

     Scanner input = new Scanner(System.in); 

     while (true){ 

      System.out.println("1. Load file from path of your choosing."); 
      System.out.println("2. Answer eight questions and save the answers into a file."); 
      System.out.println("3. Exit the program."); 


      int userChoice = input.nextInt(); 

      switch (userChoice){ 
      case 1: 
       loadFile(); 
       break; 
      case 2: 
       answerQuestions(); 
       break; 
      case 3: 
       System.out.println("Goodbye!"); 
       break; 
      } 
     } 
    } 

    public static void loadFile() throws IOException{ 
     System.out.println("What is the file name you want to try to read?"); 
     Scanner input = new Scanner(System.in); 
     File f = new File(input.nextLine()); 
     if (f.exists() && !f.isDirectory()){ 
      System.out.println("The file exists!"); 
      Scanner inputFile = new Scanner(f); 
      while (inputFile.hasNext()){ 
       String answer = inputFile.next(); 
       System.out.println(answer); 
      } 

      input.close(); 
     } 
     else { 
      System.out.println("File does not exist at that given path!"); 
     } 

    } 

    public static void answerQuestions() throws IOException{ 

     Scanner input = new Scanner(System.in); 
     System.out.println("What is your name?"); // 1 
     String name = input.nextLine(); 
     System.out.println("What is your favorite color?"); // 2 
     String color = input.nextLine(); 
     System.out.println("What is your favorite type of music?"); // 3 
     String music = input.nextLine(); 
     System.out.println("What is your favorite place?"); // 4 
     String place = input.nextLine(); 
     System.out.println("What is your favorite food?"); // 5 
     String food = input.nextLine(); 
     System.out.println("What is your favorite book?"); // 6 
     String book = input.nextLine(); 
     System.out.println("What is your favorite programming language?"); // 7 
     String language = input.nextLine(); 
     System.out.println("Do you prefer laptop or desktop computers?"); // 8 
     String computer = input.nextLine(); 

     System.out.println("All the questions are answered. Name the file you want to save the answers to: "); 
     File f = new File(input.nextLine()); 
     if (f.exists()){ 
      System.out.println("File already exists!"); 
      return; 
     } 
     PrintWriter output = new PrintWriter(f); 
     output.print(name + "\n"); 
     output.print(color + "\n"); 
     output.print(music + "\n"); 
     output.print(place + "\n"); 
     output.print(food + "\n"); 
     output.print(book + "\n"); 
     output.print(language + "\n"); 
     output.print(computer + "\n"); 

     output.close(); 

    } 
} 

這是錯誤:

Exception in thread "main" java.util.NoSuchElementException 
    at java.util.Scanner.throwFor(Unknown Source) 
    at java.util.Scanner.next(Unknown Source) 
    at java.util.Scanner.nextInt(Unknown Source) 
    at java.util.Scanner.nextInt(Unknown Source) 
    at Lab4.main(Lab4.java:18) 

到底是什麼問題?我嘗試將第19行的掃描器輸入更改爲輸入2,但這不起作用。如果文件在檢查路徑時不存在,那麼該程序可以讓我選擇儘可能多的菜單選項,所以我猜測如果找到文件,在開始將東西打印到控制檯的部分,程序出了問題。但我不知道它會是什麼。我會很感激任何提示或提示,指導我處理這個錯誤。謝謝你的時間。

+0

而例外是什麼? – immibis

+0

對不起,我添加了錯誤。 – HandleThatError

回答

1
Scanner input = new Scanner(System.in); 
/* other code here */ 
input.close(); 

關閉掃描儀還會關閉正在使用的數據流。您已關閉System.in。就像任何其他流一樣,如果它關閉了,就不能使用它。

理想的解決方案是創建一次掃描程序(在程序開始時),然後在每次要讀取輸入時使用它。

+0

非常感謝!所以如果我知道我將使用一臺掃描儀進行相同類型的輸入,我可以在開始時創建一臺掃描儀......做我需要做的事情,然後關閉最後的掃描儀?如果它最終結束,關閉它有什麼意義?由於程序無論如何都結束,是否有任何意義關閉掃描儀? – HandleThatError

+1

如果程序即將結束,關閉掃描儀也不會有什麼區別。事實上,即使該計劃不會結束,它也可能不會有所作爲。除了關閉流('System.in')外'Scanner.close'可能不會做任何事情。 – immibis

0

loadFile()刪除input.close();函數,因爲它正在關閉引用的標準輸入流,阻止進一步的IO。

相關問題