0

I have source file:

String sourceFileName = "C:\\Users\\blah\\source\\sourceFileName.csv"

And I have the destination file:

String destinationFileName= "C:\\Users\\blah\\destination\\destinationFileName.csv"

I need to overwrite the content of destination file with the content from the source file. However, I can't figure out how to do this without deleting the destination file. All methods I can think of are simply deleting the destination file, and replacing it with the source file. I CANNOT delete the destination file itself. I can only delete the content of that file, and paste the new content. How can I do this in Java?

My current method

public static void overwriteTheContent(File sourceFile, String targetFileName) {
    Path sourcePath = sourceFile.toPath();
    Path targetPath = Paths.get(targetFileName);
    File file = targetPath.toFile();
    if(file.isFile()){
        try {
             
            Files.delete(targetPath); // I want to delete only the content, not the file !!!!!!!!
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    try {
        Files.move(sourcePath, targetPath);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
john
  • 647
  • 5
  • 23
  • 53

1 Answers1

0

are you searching for something like this?:

    PrintWriter writer = new PrintWriter(FILE_PATH);
writer.print("");
writer.close();

and then:

PrintWriter writer2 = new PrintWriter(FILE_PATH);
writer.print("what you want to save to the file");