为阅读和写作创建两个不同的文件是个好主意吗?

时间:2021-10-06 20:23:48

I am building a basic bank application, although the usage of the java language is intermediate level. There I am using file input and output a lots. Along the way some questions has popped up in my mind about the file-i/o in java.

我正在构建一个基本的银行应用程序,尽管java语言的使用是中等水平的。我在那里使用文件输入和输出很多。一路上,我脑海里浮现出一些关于java中的文件i / o的问题。

1) What if I create two different text file for writing and reading objects? Does it make any difference?

1)如果我创建两个不同的文本文件来写和读对象怎么办?它有什么不同吗?

2) How about the specifying path (or giving file name), what if I use // instead of \\?

2)指定路径(或给出文件名)怎么样,如果我使用//代替\\怎么办?

3) Do I necessarily need to create a new file object like this: File file=new File("C://Users//Documents//NetBeansProjects//BankFile_assignment.txt"); in my specific case?

3)我是否一定需要创建一个新的文件对象:File file = new File(“C://Users//Documents//NetBeansProjects//BankFile_assignment.txt”);在我的具体情况?

Last but not least if you may wonder about my file-i/o class:

最后但并非最不重要的,如果你可能想知道我的文件i / o类:

    public class ReaderWriter {

    public void writeToFile(List<BankAccount> accounts) {
        try {
            File file = new File("C://Users//Documents//NetBeansProjects//BankFile_assignment.txt");
            FileOutputStream fos = new FileOutputStream(file);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(accounts);//take the arrayList 
            oos.flush();
            oos.close();
            fos.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<BankAccount> readFromFile() {
        List<BankAccount> readData = null;
        try {
            File file = new File("C://Users//Documents//NetBeansProjects//BankFile_assignment.txt");
            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);
            readData = (List<BankAccount>) ois.readObject();

            ois.close();
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return readData;

    }
}

1 个解决方案

#1


1) Better way is to use databases (mySQL, SQLite,...) to access easily to all your datas without I/O worries.

1)更好的方法是使用数据库(mySQL,SQLite,...)轻松访问所有数据,而无需I / O担忧。

2) If your application might work on different Operating Systems, a safe way to avoid any trouble with the specific symbol of system ( \ on Windows, / on Unix, Mac) is to use File.separator for example. More about this subject .

2)如果您的应用程序可能在不同的操作系统上运行,那么避免系统特定符号(\在Windows上,/在Unix上,Mac上)的任何问题的安全方法是使用File.separator。更多关于这个主题。

3) It must work on Windows, but fails on Unix. You can use (with adaptation for path) this instead of: File file = new File(System.getProperty("user.home")+ File.separator + BankFile_assignment.txt); See this .

3)它必须在Windows上运行,但在Unix上运行失败。您可以使用(改为路径)而不是:File file = new File(System.getProperty(“user.home”)+ File.separator + BankFile_assignment.txt);看到这个。

#1


1) Better way is to use databases (mySQL, SQLite,...) to access easily to all your datas without I/O worries.

1)更好的方法是使用数据库(mySQL,SQLite,...)轻松访问所有数据,而无需I / O担忧。

2) If your application might work on different Operating Systems, a safe way to avoid any trouble with the specific symbol of system ( \ on Windows, / on Unix, Mac) is to use File.separator for example. More about this subject .

2)如果您的应用程序可能在不同的操作系统上运行,那么避免系统特定符号(\在Windows上,/在Unix上,Mac上)的任何问题的安全方法是使用File.separator。更多关于这个主题。

3) It must work on Windows, but fails on Unix. You can use (with adaptation for path) this instead of: File file = new File(System.getProperty("user.home")+ File.separator + BankFile_assignment.txt); See this .

3)它必须在Windows上运行,但在Unix上运行失败。您可以使用(改为路径)而不是:File file = new File(System.getProperty(“user.home”)+ File.separator + BankFile_assignment.txt);看到这个。