how can i Merge files

寵の児 提交于 2019-12-13 11:03:21

问题


How can i wright in the beggining of the first file infomation which contains in second file, so that they merge each other?? please help, thanks.

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        FileInputStream fileReader = new FileInputStream(reader.readLine());
        FileOutputStream fileWriter = new FileOutputStream(reader.readLine(), true);
        while (fileReader.available() > 0) {
            int data = fileReader.read();
            fileWriter.write(data);
        }
        fileReader.close();
        fileWriter.close();
        reader.close();

回答1:


This can be done in pure Java... It reads the second file and appends to the first file.
Keep in mind, that this will not work for large files, since it saves all of fileA content into memory.

Scanner reader = new Scanner(System.in);
System.out.println("Enter the first file path");
String fileA = reader.next();
System.out.println("Enter the second file path");
String fileB = reader.next();

try {

    // Read from and cache fileA
    StringBuilder cache = new StringBuilder();
    BufferedReader readerA = new BufferedReader(new FileReader(new File(fileA)));
    String line = null;
    while((line = readerA.readLine()) != null) {
        cache.append(line);cache.append("\n");
    }
    readerA.close();

    // Read from fileB and overwrite to fileA
    FileWriter writerB = new FileWriter(new File(fileA));
    BufferedReader readerB = new BufferedReader(new FileReader(new File(fileB)));
    line = null;
    while((line = readerB.readLine()) != null) {
        writerB.write(line);writerB.write("\n");
    }
    writerB.close();
    readerB.close();

    // Append original fileA content back into fileA
    FileWriter writerA = new FileWriter(new File(fileA), true);
    writerA.write(cache.toString());
    writerA.close();

} catch (Exception e) {
    e.printStackTrace();
}



回答2:


Simply use

org.apache.commons.io.FileUtils.copyFile(srcFile, destFile);
//rename the file names if needed

OR you can use SequenceInputStream to create a sequence of multiple input stream.

Sample code:

    File srcFile = new File("resources/abc.txt");
    File destFile = new File("resources/xyz.txt");

    File temp = new File("resources/temp.txt");

    try {
        FileInputStream fileInputStream1 = new FileInputStream(srcFile);
        FileInputStream fileInputStream2 = new FileInputStream(destFile);

        SequenceInputStream inputStream = new SequenceInputStream(fileInputStream1,
                fileInputStream2);
        FileOutputStream outputStream = new FileOutputStream(temp);
        byte[] buffer = new byte[1024];
        int read = -1;
        while ((read = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, read);
        }

        inputStream.close();
        outputStream.close();

        fileInputStream1.close();
        fileInputStream2.close();

        // here you can rename the temp file or delete a source file if needed
    } catch (IOException e) {
        e.printStackTrace();
    }


来源:https://stackoverflow.com/questions/23174843/how-can-i-merge-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!