2012-11-21 171 views
0

嗨我的Java程序應該讀入並顯示一個.txt文件,用戶在出現提示時輸入,將文件中的整數轉換爲輸出.dat文件,然後讀入該.dat文件並再次顯示數字。當我運行我的程序時,它會顯示文件的內容,並創建.dat文件,但不會再讀入它。我的代碼如下。我需要做什麼?爲什麼我的程序不能打開我的.dat文件?

public class InputFile 
{ 
    public static void main(String [] args) 
    { 
     BufferedReader inputStream = null; 

      System.out.print("Enter file name (with .txt extension): "); 
      Scanner keys = new Scanner(System.in); 
      String inFileName = keys.next(); 

      try 
      { 
       inputStream = new BufferedReader (new FileReader(inFileName)); 
       System.out.println("The file " + inFileName + " contains the following lines:"); 
       String inFileString = inputStream.readLine(); 
       while(inFileString != null) 
       { 
        System.out.println(inFileString); 
        inFileString = inputStream.readLine(); 
       } 
       inputStream.close(); 
      } 
      catch(FileNotFoundException e) 
      { 
       System.out.println(inFileName + " not found! Try Again."); 
      } 
      catch(IOException e) 
      { 
       System.out.println(e.getMessage()); 
      } 

      String fileName = "numbers.dat"; 
      try 
      { 
       ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(fileName)); 
       int anInt = 0; 
       while(anInt >=0); 
       { 
        anInt = Integer.parseInt(inputStream.readLine()); 
        outputStream.writeInt(anInt); 
       } 
       outputStream.close(); 
      } 
      catch(FileNotFoundException e) 
      { 
       System.out.println("Problem opening file."); 
      } 
      catch(IOException e) 
      { 
       System.out.println("Problem with output to the file."); 
      } 

      try 
      { 
       ObjectInputStream inputStream2 = new ObjectInputStream(new FileInputStream(fileName)); 
       System.out.println("The file being read yields:"); 
       int anInteger = inputStream2.readInt(); 
       while(anInteger >= 0) 
        { 
         System.out.println(anInteger); 
         anInteger = inputStream2.readInt(); 
        } 
       inputStream2.close(); 
      } 
      catch(FileNotFoundException e) 
      { 
       System.out.println("Problem with opening the file."); 
      } 
      catch(EOFException e) 
      { 
       System.out.println("Problem reading the file."); 
      } 
      catch(IOException e) 
      { 
       System.out.println("There was a problem reading the file."); 
      } 
    } 
} 
+1

你可能應該結合前兩個'while'循環。 – irrelephant

+0

對不起,我更新Java。它編譯並運行時沒有任何錯誤消息。和@irrelephant,你會如何建議我這樣做? – Derek

回答

1

您沒有寫入輸出流,因爲那段時間inputStream已經耗盡並關閉。

創建一個集合來存儲第一個文件中的元素。

String inFileName = keys.next(); 
Collection<String> lines = new ArrayList<String>(); 
... 
System.out.println(inFileString); 
lines.add(inFileString); 

... 
for(String line : lines){ 
... 
    outputStream.write(Integer.parseInt(line)); 
... 
} 
2

有一個錯誤(或至少我認爲這是一個錯字)難以發現,使您的第二個循環無限。

(...) 
try 
     { 
      ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(fileName)); 
      int anInt = 0; 
      while(anInt >=0); <===== 
      { 
       anInt = Integer.parseInt(inputStream.readLine()); 
       outputStream.writeInt(anInt); 
      } 
      outputStream.close(); 
     } 

刪除';'過了一段時間,我想它會正常運行。

+0

謝謝!我甚至沒有看到!我刪除它,它確實運行,但現在我得到「輸出到文件的問題 正在讀取的文件產生: 讀取文件時出現問題。」顯示在屏幕上:( – Derek

+1

你剛剛讀完輸入流後就關閉了,所以當你試圖再次閱讀它時,它會關閉,嘗試在創建outputStream之前重新打開它(例如:inputStream = new BufferedReader(new FileReader(inFileName )); ObjectOutputStream outputStream(...)) –

+0

hmmm:/ that that just yield this error:「Exception in thread」main「java.lang.NumberFormatException:null \t at java.lang.Integer.parseInt(Integer.java: 417) \t at java.lang.Integer.parseInt(Integer.java:499) \t at InputFile.main(InputFile.java:63)「 – Derek

相關問題