2012-10-17 49 views
0

因此,我使用了DataInputStream,FileInputStream,BufferInputStream,FileReader,BufferedReader,Scanner等名称。它们都抛出FileNOtFoundException或CorruptedStreamException。从文本文件中读取抛出异常

异常 java.io.FileNotFoundException:[email protected](系统找不到指定的文件) 被扔在其中的FileReader与文件名初始化“Accounts.txt”行,这是一个我已经初始化的文件,在文件箱中,需要它的文本。

import java.io.*; 
import java.util.ArrayList; 

/** 
* Class to load account files 
*/ 
public class AccountLoader { 

    /** 
    * Add an account file 
    * @param newAccount 
    */ 
    public static void addAcountFile(Account newAccount) { 
     try { 
      PrintWriter out = new PrintWriter(new File("Accounts.txt")); 

      out.print(" " + newAccount.getOwner().getName()); 
      System.out.println("saved account " + newAccount.getOwner().getName()); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
    } 


    public static ArrayList<Account> loadAccountsList() throws EOFException, IOException, ClassNotFoundException{ 
     ArrayList<Account> accounts = new ArrayList(); 

      FileReader load = new FileReader("Accounts.txt"); 

      String file = load.toString(); 

      String[] accountsload = file.split(" "); 
      for (String string : accountsload){ 
       accounts.add(loadAccount(string + ".data")); 
      } 
      load.close(); 
      return accounts; 

    } 
    public static void save(Account account) { 

     String filename = account.getOwner().getName() + ".data" ; 
      if (filename != null) {   
      try { 
      FileOutputStream fos = new FileOutputStream(filename); 

      ObjectOutputStream out = new ObjectOutputStream(fos); 


      out.writeObject(account);  

      out.flush();      
      out.close();       
      } 

      catch (IOException e) { System.out.println(e); } 
     } 

     } 

    public static Account loadAccount(String filename) { 
      Account newAccount = null; 
      if (filename != null) {   
       try { 

       FileInputStream fis = new FileInputStream(filename); 

       ObjectInputStream in = new ObjectInputStream(fis); 

       newAccount = (Account)in.readObject(); 
       in.close();          
       } 

       catch (Exception e) { System.out.println(e); } 
      } 
      return newAccount; 
      } 

} 
+0

您是否使用命令行运行? –

+2

我99%确定你没有在正确的位置查找文件,用户目录,就像99%的经常***问这个问题的人(如果我有一个每季度一次......)。通过运行这一行找到用户目录:'System.out.println(System.getProperty(“user.dir”));' –

+0

@HovercraftFullOfEels如果我有一个季度,每次你是对的... – MadProgrammer

回答

0

尝试将文本文件移动到其他文件夹。超出bin文件夹的一个文件夹可能是正确的位置。

+0

Woops,我撒谎了。它位于垃圾箱一步之外的文件夹中。 – Swanijam

1

您可能需要将文本文件放入“项目根目录”文件夹(包含srcbin),而不是bin文件夹。如果你是从Eclipse运行这个,那肯定是你需要做的,因为从Eclipse运行的Java项目的上下文始终是该项目的Eclipse项目文件夹。

当您要求Java通过名称打开文件而不给它一个路径时,JVM将在其当前工作目录中查找该文件。当前工作目录根据你运行程序的方式而变化,在你的情况下,它看起来像“bin”文件夹不是你当前的工作目录。

+0

它实际上在项目的根文件夹中。我的错。 – Swanijam

0

如果使用命令行,把文件从正在运行java命令的文件夹中,并在CLASSPATH中添加.作为

set CLASSPATH=%CLASSPATH%;. 

,然后运行Java程序。

如果您使用的是eclipse,请尝试将该文件放在项目的根文件夹中或使用相对于根文件夹的相对路径。

+0

我正在使用eclipse,并且该文件位于根文件夹中。 – Swanijam

+0

通过'root'文件夹,我的意思是项目文件夹不是'src'或'JavaSource'。它在项目文件夹(src文件夹的父目录)中吗? –

+0

是的。无论如何,该文件夹是从这个类首先保存的,所以工作目录必须是相同的,对吗? – Swanijam

相关问题