0

所以我用过DataInputStream、FileInputStream、BufferInputStream、FileReader、BufferedReader、Scanner,随便你说。它们都抛出 FileNOtFoundException 或 CorruptedStreamException。

异常 java.io.FileNotFoundException: java.io.FileReader@253498.data (The system cannot find the file specified) 被抛出在 FileReader 使用文件名“Accounts.txt”初始化的行,这是一个文件我已经在 bin 中使用所需的文本进行了初始化。

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;
          }

}
4

3 回答 3

1

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

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

于 2012-10-17T02:58:07.310 回答
0

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

于 2012-10-17T02:52:24.493 回答
0

如果您使用的是命令行,请将文件放在运行 java 命令的文件夹中,并.在 CLASSPATH 中添加为

  set CLASSPATH=%CLASSPATH%;.

然后运行你的java程序。

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

于 2012-10-17T03:19:45.123 回答