【问题标题】:How to overwrite a file that is altering an arraylist如何覆盖正在更改数组列表的文件
【发布时间】:2017-11-01 19:03:54
【问题描述】:

我正在编写一个程序来跟踪我的图书馆中的 DVD。我在更改保存从 arraylist 中添加或删除的 DVD 对象的文本文件时遇到问题。每当我调用我的保存方法时,它会覆盖包含所有信息的现有文本文件,它不会改变它。我的添加和删除方法工作正常,但它只是覆盖我正在读取的文件的保存方法不起作用。以下代码是我试图用来将数组列表保存到文件中的代码。我的文件名是 DVDCollection.txt,布尔变量标志是一个静态变量,用于检查是否到达了从数组列表中添加或删除对象的代码。

public void save() {
    try{
        if(flag=true){
            FileWriter instream = new FileWriter("DVDCollection.txt",false);
            instream.close();
        }else{
            return;
        }
    }catch(IOException e){
        System.out.println("The file could not be written to!");
    }
}

【问题讨论】:

  • 你在哪里写入文件?
  • 您有任何实际尝试写入文件的代码吗?目前你只有一个文件写入器,它会立即关闭

标签: java arraylist io


【解决方案1】:

如果您使用的是 java 8 或更高版本,则很简单:

    List<String> lines = Arrays.asList("first line", "second line");
    try {
        Files.write(Paths.get("my-file.txt"), lines);
    } catch (IOException e) {
        //handle exception
    }

确保您提供正确的路径!

【讨论】:

    【解决方案2】:

    不确定,为什么这个方法应该保存一个数组列表,因为写入这个文件的实际代码丢失了。这是一个简单的测试,让我们从这里开始:

    import java.io.*;
    import java.util.*;
    
    public class FileSaveTest {
    
        public static void main(String[] args) {
            FileSaveTest test = new FileSaveTest();
            test.fill();
            test.save();
        }
    
        public void fill() {
            arrayList.add("My disc 1");
            arrayList.add("My disc 2");
            arrayList.add("Another disc");
        }
    
        public void save() {
            try {
                if(flag) { // you dont need ==true
                    FileWriter instream = new FileWriter("DVDCollection.txt",false);
                    for (String entry : arrayList) {
                        instream.write(entry + "\n");
                    }
                    instream.close();
                } else {
                    return;
                }
            } catch(IOException e) {
                System.out.println("The file could not be written to!");
            }
        }
    
        private ArrayList<String> arrayList = new ArrayList<>();
        private static boolean flag = true;
    }
    

    接下来,以这种方式关闭文件不太好。如果写入时发生异常,文件将不会被关闭。 instream.close() 应该放入“finally”块中。这个块在任何情况下都会被执行,不管是否发生了异常或者是否遇到了return关键字:

    public void save() {
        Writer instream = null;
        try {
            if(flag) { // you dont need ==true
                instream = new FileWriter("DVDCollection.txt",false);
                for (String entry : arrayList) {
                    instream.write(entry + "\n");
                }
            } else {
                return;
            }
        } catch(IOException e) {
            System.out.println("The file could not be written to!");
        } finally {
            try {
                if (instream != null)
                    instream.close();
            } catch (IOException e) {
                System.err.println("Exception during close");
            }
        }
    }
    

    或者,如果您使用的是 java 7,则可以使用 try-with-resources 语法:

    public void save() {
        if(flag) { // you dont need ==true
            try (Writer instream = new FileWriter("DVDCollection.txt",false)) {
                for (String entry : arrayList)
                    instream.write(entry + "\n");
            } catch(IOException e) {
                System.out.println("The file could not be written to!");
            }
        } // you dont need "return  else { return; }" anymore
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-31
      • 2015-12-26
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 2020-12-29
      • 2017-01-01
      • 1970-01-01
      相关资源
      最近更新 更多