【问题标题】:How to copy content of a file to another file but at the top of the file in Java [duplicate]如何将文件的内容复制到另一个文件但在Java中的文件顶部[重复]
【发布时间】:2015-02-24 08:12:06
【问题描述】:

我想将文件“Convert.sql”的内容复制到文件“Entity.sql”而不覆盖“Entity.sql”文件。这部分我已经能够做到,但除此之外,我希望将文件“Convert.sql”的内容复制到“Entity.sql”文件的顶部,而不是在同一行。

例如:Convert.sql

学生

员工

例如:Entity.sql

名字

地址

例如:结果

学生

员工

名字

地址

这是我将文件内容复制到另一个文件而不覆盖文件的代码,但它被复制到我不想复制的同一行上。

package Final;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class Mainy {
    public static void main(String[] args) throws IOException {
        File dir = new File(".");

        String source = dir.getCanonicalPath() + File.separator + "Convert.sql";
        String dest = dir.getCanonicalPath() + File.separator + "Entity.sql";

        File fin = new File(source);
        FileInputStream fis = new FileInputStream(fin);
        BufferedReader in = new BufferedReader(new InputStreamReader(fis));

        FileWriter fstream = new FileWriter(dest, true);
        BufferedWriter out = new BufferedWriter(fstream);

        String aLine = null;
        while ((aLine = in.readLine()) != null) {
            //Process each line and add output to Dest.txt file
            out.write(aLine);
            out.newLine();
        }

        // do not forget to close the buffer reader
        in.close();

        // close buffer writer
        out.close();
    }
}

有人可以帮我做这件事吗?

【问题讨论】:

  • 读取文件一的内容,读取文件二的内容,将文件一的内容写入文件二,将文件二的内容追加到文件二。

标签: java file filereader fileinputstream filewriter


【解决方案1】:

这里有一个 sn-p,它将按照您的描述完成工作

Charset usedCharset = Charset.defaultCharset();

// read all lines from Convert.sql into a list
List<String> allLines = Files.readAllLines(Paths.get("Convert.sql"), usedCharset);

// append all lines from Entity.sql to the list
allLines.addAll(Files.readAllLines(Paths.get("Entity.sql"), usedCharset));

// write all lines from the list to file Entity.sql
Files.write(Paths.get("Entity.sql"), allLines, usedCharset);

【讨论】:

  • 太棒了!!万分感谢!! :) 你太棒了!
【解决方案2】:

使用 FileWriter(file,true) 追加到文件

File file=new File("Entity.sql");
FileWriter fileWritter = new FileWriter(file.getName(),true);
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);

然后读取Convert.sql并写入数据

bufferWritter.write(data);

然后关闭bufferWritter

bufferWritter.close();

【讨论】:

    【解决方案3】:

    如果您想处理整个文件,则根本不需要处理行或将内容解释为文本(即应用字符编码):

    Path in=Paths.get("Convert.sql"), out=Paths.get("Entity.sql");
    byte[] old=Files.readAllBytes(out);
    Files.copy(in, out, StandardCopyOption.REPLACE_EXISTING);
    Files.write(out, old, StandardOpenOption.APPEND);
    

    只需as others said,完整读取您要添加到的文件,复制另一个文件并添加您在第一步中读取的旧数据。

    这使用了 Java 7 API,但我认为值得学习。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-26
      • 1970-01-01
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      • 1970-01-01
      • 2022-11-24
      • 1970-01-01
      相关资源
      最近更新 更多