【问题标题】:Java: How to make make overwriting to the same file I am reading from inside a GUI?Java:如何覆盖我从 GUI 内部读取的同一个文件?
【发布时间】:2013-11-21 13:36:24
【问题描述】:

我正在构建一个 GUI,它从包含所有客户信息的文本文件中读取,然后在我的 GUI 中显示信息。我希望让客户通过我的 GUI 对他或她的信息进行更改,然后通过点击“保存更改”按钮,我能够将所有这些更改保存到我正在读取的同一个文本文件中。我是 FileReader/FileWriter 和 BufferedReader/PrinterWriter 的新手。有人可以告诉我如何设计吗?非常感谢!

【问题讨论】:

    标签: java user-interface text jbutton overwrite


    【解决方案1】:

    如果您使用的是 Java 7,则要容易得多。例如:

    public static void main(String[] args) throws IOException {
    
        // You get this file with JFileChooser
        File selectedFile = new File("file.txt");
    
        // Read file and close file.
        List<String> lines = Files.readAllLines(selectedFile.toPath(),
                StandardCharsets.UTF_8);
    
        // Modify some in the lines...
    
        // This replace the contents and close the file
        Files.write(selectedFile.toPath(), lines, StandardCharsets.UTF_8);
    
    }
    

    【讨论】:

    • 那么我应该把这个块放在我的保存更改按钮监听器类定义中吗?
    猜你喜欢
    • 2015-12-15
    • 2013-11-27
    • 1970-01-01
    • 2013-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2018-02-18
    相关资源
    最近更新 更多