Java merge files

Question

Answers ( 1 )

    0
    2024-02-02T15:04:10+00:00

    It involves combining multiple files into a single file using Java programming language.

    To merge files in Java, you can use various approaches depending on the type of files you are dealing with (e.g., text files, binary files, etc.). Below are some common methods:

    1. Merging Text Files

    This involves reading each file line by line and writing the contents to a new file.

    import java.io.*;
    
    public class MergeFiles {
        public static void main(String[] args) throws IOException {
            PrintWriter pw = new PrintWriter("output.txt");
            
            BufferedReader br1 = new BufferedReader(new FileReader("input1.txt"));
            BufferedReader br2 = new BufferedReader(new FileReader("input2.txt"));
            
            String line1 = br1.readLine();
            String line2 = br2.readLine();
            
            while (line1 != null || line2 != null) {
                if (line1 != null) {
                    pw.println(line1);
                    line1 = br1.readLine();
                }
                
                if (line2 != null) {
                    pw.println(line2);
                    line2 = br2.readLine();
                }
            }
            
            pw.flush();
            
            br1.close();
            br2.close();
            pw.close();
        }
    }
    

    2. Merging Binary Files

    For binary files (like images, videos), you read and write bytes.

    import java.io.*;
    
    public class MergeBinaryFiles {
        public static void main(String[] args) throws IOException {
            FileOutputStream fos = new FileOutputStream("output.bin");
            
            FileInputStream fis1 = new FileInputStream("input1.bin");
            FileInputStream fis2 = new FileInputStream("input2.bin");
            
            byte[] buffer = new byte[1024];
            int length;
            
            while ((length = fis1.read(buffer)) > 0) {
                fos.write(buffer, 0, length);
            }
            
            while ((length = fis2.read(buffer)) > 0) {
                fos.write(buffer, 0, length);
            }
            
            fis1.close();
            fis2.close();
            fos.close();
        }
    }
    

    3. Using Java NIO

    Java NIO (New IO) can be used for more efficient file handling.

    import java.nio.file.*;
    import java.io.IOException;
    
    public class MergeFilesNIO {
        public static void main(String[] args) throws IOException {
            Path output = Paths.get("output.txt");
            Path input1 = Paths.get("input1.txt");
            Path input2 = Paths.get("input2.txt");
    
            Files.write(output, Files.readAllBytes(input1), StandardOpenOption.CREATE);
            Files.write(output, Files.readAllBytes(input2), StandardOpenOption.APPEND);
        }
    }
    

    Notes

    • Always handle exceptions and ensure streams are closed properly to avoid memory leaks.
    • The method you choose depends on your specific requirements, like file size and type.
    • For large files, consider using buffered streams to improve performance.
    • These examples show basic file merging. Additional logic might be needed for more complex scenarios, like handling file headers or specific formats.

Leave an answer