找到你要的答案

Q:Is it good idea to create two different files for reading and writing?

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

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.

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

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

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?

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

    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;

    }
}

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.

1)如果我创建两个不同的文本文件的写作和阅读对象?有什么区别吗?

2)如何指定路径(或给文件名),如果我使用/ /而不是\?

3)我一定要创造一个这样的新文件对象:文件=新的文件(“C:/ /用户/文件/ / NetBeansProjects / bankfile_assignment .txt”);在我的具体情况?

最后但并非最不重要,如果你可能会怀疑我的文件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;

    }
}
answer1: 回答1:

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

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 .

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 .

1)更好的办法是使用的数据库(MySQL,SQLite,…)来访问你所有的数据不容易I / O忧虑。

2)如果您的应用程序可以工作在不同的操作系统,一个安全的方式来避免与系统的具体象征什么麻烦(在Windows,Unix /对,MAC)是使用file.separator例如。更多关于这个主题。

3)它必须工作在Windows,但不能在UNIX。你可以使用(适应路径)这不是:文件=新的文件(系统。getProperty(”用户。家”)+ file.separator + bankfile_assignment .txt);看到这个。

java  file  file-io  fileinputstream  fileoutputstream