简体   繁体   English

Java使用renameTo(file)在方法中重命名文件

[英]Java Renaming files in a method with renameTo(file)

I am using this method that copies a file with altered info to the new one. 我正在使用这种方法将具有更改信息的文件复制到新文件。 I am simply attaching this code(that works as intended),in case my ignorance exceeds my expectations. 如果我的无知超出了我的期望,我只是附上此代码(可以正常工作)。

public void Writedevices(String Start)throws IOException{
                File S = null;
                File D = null;
                BufferedReader reader = null;
                String line;
                BufferedWriter writer = null;
                PrintWriter Newfile = new PrintWriter("Destination.txt","UTF-8");
                int i=0;
                try{
                        S = new File(Start);
                        D = new File("Destination.txt");
                    }
                    catch (NullPointerException e){
                    System.err.println("File not found.");
                    }

                try{
                    reader = new BufferedReader(new FileReader(S));
                    writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(D), "utf-8"));
                    line = reader.readLine();
                    while(line != null){
                        if(!line.toLowerCase().contains("pieces")){
                            writer.write(line);
                            writer.append(System.getProperty("line.separator"));
                        }
                        else{
                            writer.write("            PIECES "+Availability.get(i));
                            writer.append(System.getProperty("line.separator"));
                            i++;
                        }
                        line=reader.readLine();
                    }
                }  
                catch (FileNotFoundException e ){
                System.err.println("Error opening file!");
                }
                finally{
                    writer.close();
                    reader.close();
                }

This is where I encounter a problem.The starting file deletes,but the final correctly altered file doesn't rename as intended. 这是我遇到的问题。起始文件已删除,但最终正确更改的文件未按预期重命名。

S.delete();
                File Name = new File("Available_Devices.txt");
                if(D.renameTo(Name)){
                System.out.println("Device list availability update succesfull.");
                        }
            }

I can confirm that the code below when executed results to: 我可以确认以下代码在执行后会导致:

  • "Source.txt" file gets deleted "Source.txt"文件被删除
  • "Destination.txt" gets created and contains characters as expected 创建"Destination.txt"并包含预期的字符
  • "Destination.txt" gets renamed to "Available_Devices.txt" keeping all its data intact "Destination.txt"重命名为"Available_Devices.txt"保持其所有数据不变

If this is not working on your system as described, check if your java program has the permissions to do the required manipulations on the files (if you are on linux try running your program with sudo? ) 如果这在您所描述的系统上不起作用,请检查您的Java程序是否具有对文件进行所需操作的权限(如果您在linux上,请尝试使用sudo运行程序)。

If your problem is different, please describe it in more detail. 如果您的问题不同,请更详细地描述。

package tempProject;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class FileTransformer2 {
    public static void main(String[] args) {
        try {
            Writedevices("Source.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void Writedevices(String Start) throws IOException {
        File S = null;
        File D = null;
        BufferedReader reader = null;
        String line;
        BufferedWriter writer = null;
        int i = 0;
        try {
            S = new File(Start);
            D = new File("Destination.txt");
        } catch (NullPointerException e) {
            System.err.println("File not found.");
        }

        try {
            reader = new BufferedReader(new FileReader(S));
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(D), "utf-8"));
            line = reader.readLine();
            while (line != null) {
                if (!line.toLowerCase().contains("pieces")) {
                    writer.write(line);
                    writer.append(System.getProperty("line.separator"));
                } else {
                    // writer.write(" PIECES "+Availability.get(i));
                    writer.append(System.getProperty("line.separator"));
                    i++;
                }
                line = reader.readLine();
            }

        } catch (FileNotFoundException e) {
            System.err.println("Error opening file!");
        } finally {
            writer.close();
            reader.close();

        }

        S.delete();
        File Name = new File("Available_Devices.txt");
        if(D.renameTo(Name)){
        System.out.println("Device list availability update succesfull.");
                }


    }
}

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

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