如何编写FileOutputStream存在的文件?当我运行两次此程序时,第二次oosfos为null

 public class ReadFile {
    static FileOutputStream fos = null;
    static ObjectOutputStream oos = null;
    public static void main(String[] args) throws IOException, ClassNotFoundException {

        File f = new File("file.tmp");
        if (f.exists()) {
            //How to retreive an old oos to can write on old file ?
            oos.writeObject("12345");
            oos.writeObject("Today");
        }
        else
        {
            fos = new FileOutputStream("file.tmp");
            oos = new ObjectOutputStream(fos);
        }
        oos.close();
    }
 }

最佳答案

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f,true));


如果要附加到文件

关于java - 写一个已经存在的文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13232582/

10-09 03:14