fairyjee
fairyjee

Reputation: 75

merge file and delete the old file

I have been trying to merge two files into new file and below code does it job. But after the merge i want to delete the old files. The code I am using to delete files just delete 1 file (file2) not the both of them.

public static void Filemerger() throws IOException  
    { 
        
       String resultPath = System.getProperty("java.io.tmpdir");
       File result = new File(resultPath+"//NewFolder", "//file3.txt"); 
        
        // PrintWriter object for file3.txt 
        PrintWriter pw = new PrintWriter(result); 
          
        // BufferedReader object for file1.txt 
        BufferedReader br = new BufferedReader(new FileReader(resultPath+"file1.txt")); 
          
        String line = br.readLine(); 
          
        // loop to copy each line of  
        // file1.txt to  file3.txt 
        while (line != null) 
        { 
            pw.println(line); 
            line = br.readLine(); 
        } 
        
        pw.flush();
          
        br = new BufferedReader(new FileReader(resultPath+"file2.txt")); 
          
        line = br.readLine(); 
          
        // loop to copy each line of  
        // file2.txt to  file3.txt 
        while(line != null) 
        { 
            pw.println(line); 
            line = br.readLine(); 
        } 
          
        pw.flush(); 
          
        // closing resources 
        br.close(); 
         
        pw.close(); 
        
        File dir = new File(resultPath);
        
        FileFilter fileFilter1 = new WildcardFileFilter(new String[] {"file1.txt", "file2.txt"}, IOCase.SENSITIVE); 
        File[] fileList1 = dir.listFiles(fileFilter1);
            
        for (int i = 0; i < fileList1.length; i++) {
              if (fileList1[i].isFile()) {
                  
                File file1 = fileList1[i].getAbsoluteFile();
                file1.delete();
              }
        }
        
    }

I also try this code to delete the file1 as above code delete the file2:

Path fileToDeletePath = Paths.get(resultPath+"file1.txt");
       Files.delete(fileToDeletePath);

but it throws an exception that Exception in thread "main" java.nio.file.FileSystemException: C:\Users\haya\AppData\Local\Temp\file1: The process cannot access the file because it is being used by another process.

Upvotes: 0

Views: 533

Answers (1)

DuncG
DuncG

Reputation: 15176

Closing the streams as suggested in the comments will fix. However you are writing a lot of code which is hard to debug / fix later. Instead simplify to NIO calls and add try with resources handling to auto-close everything on the way:

    String tmp  = System.getProperty("java.io.tmpdir");
    Path result = Path.of(tmp, "NewFolder", "file3.txt");
    Path file1  = Path.of(tmp,"file1.txt");
    Path file2  = Path.of(tmp,"file2.txt");

    try(OutputStream output = Files.newOutputStream(result)) {
        try(InputStream input = Files.newInputStream(file1))  {
            input.transferTo(output);
        }
        try(InputStream input = Files.newInputStream(file2))  {
            input.transferTo(output);
        }
    }
    Files.deleteIfExists(file1);
    Files.deleteIfExists(file2);

EDIT

The above works fine though I am unsure as to why I didn't suggest using Files.copy(Path, OutputStream) which does exactly what is shown above, simplifying the copying as:

try(OutputStream output = Files.newOutputStream(result)) {
    // use a loop over the files to copy or:
    Files.copy(file1, output);
    Files.copy(file2, output);
}

Viewing source of JDK20 Files.copy(Path, OutputStream) shows this implementation:

public static long copy(Path source, OutputStream out) throws IOException {
    // ensure not null before opening file
    Objects.requireNonNull(out);

    try (InputStream in = newInputStream(source)) {
        return in.transferTo(out);
    }
}

Upvotes: 3

Related Questions