2012-11-21 168 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

相关问题