繁体   English   中英

文件似乎没有在Java中重命名

[英]File does not seem to be renaming in Java

我在用Java编辑文本文件中的记录时遇到麻烦。 我可以打印到临时文件,但是无法删除主文件并重命名临时文件。

private void editFile(String old, String newLine) throws FileNotFoundException, IOException {
    try {  
        File inFile = new File ("Members.txt"); 
        File tempFile = new File ("Members_Temp.txt");
        inFile.setReadable(true);
        inFile.setWritable(true);
        tempFile.setReadable(true);
        tempFile.setWritable(true);
        PrintWriter PW;

        try ( 
            //Defines the BufferedReader to read the Current File
            BufferedReader BR = new BufferedReader(new FileReader(inFile))) {
            FileWriter temp = new FileWriter(tempFile);
            PW = new PrintWriter (temp);
            String line = null;
            //While Loop to read the current file till it ends
            while ((line = BR.readLine()) != null) {
                String replace = old.replace(old, newLine); //Replaces old data with the new data
                PW.write(replace); //Writes to the temporary file
                //PW.flush();
            }

            BR.close();
            PW.close();

            inFile.delete();
            tempFile.renameTo(inFile);
        }            
    }
    catch (IOException ex) {          
    }

}

我已经尝试过交换语句,但这也不起作用。 我正在考虑Files.move语句,但是不确定如何使用它? 这些文件位于program文件夹中,并且我计划使其可移植,以便在其他计算机上使用该程序时目录会有所不同,因此为什么我认为每次路径都不同。

下面的代码对我来说完全正常。 它编辑文件以及删除文件。

private void editFile(String old, String newLine)
            throws FileNotFoundException, IOException {
        try {
            File inFile = new File("Members.txt");
            File tempFile = new File("Members_Temp.txt");
            inFile.setReadable(true);
            inFile.setWritable(true);
            tempFile.setReadable(true);
            tempFile.setWritable(true);
            PrintWriter PW;

            try (
            // Defines the BufferedReader to read the Current File
            BufferedReader BR = new BufferedReader(new FileReader(inFile))) {
                FileWriter temp = new FileWriter(tempFile);
                PW = new PrintWriter(temp);
                PW.write("");
                String line = null;
                // While Loop to read the current file till it ends
                while ((line = BR.readLine()) != null) {
                    if (line.contains(old)) {
                        String replace = line.replace(old, newLine);
                        PW.append(replace+"\n");
                    } else

                        PW.append(line+"\n");
                }

                BR.close();
                PW.close();

                inFile.delete();
                tempFile.renameTo(inFile);


            }
        } catch (IOException ex) {
        }

    }

怎么测试?

最初, Members.txt文件将具有以下条目

ravi
ravindra
devadiga

而编辑方法的参数将是“ indra ”和“ dee

运行Java应用程序后,Members.txt将具有以下条目

ravi
ravdee
devadiga

意味着,在删除原始Members.txt之后,Members_Temp.txt文件已创建并重命名为Members.txt。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM