在Java中,如何将文件内容复制到另一个文件但位于文件顶部
我希望将文件“Convert.sql”的内容复制到文件“Entity.sql”,而不重写“Entity.sql”文件。这一部分我已经能够做到了,但除此之外,我希望文件“Convert.sql”的内容复制到“Entity.sql”文件的顶部,并且不在同一行 Ex:Convert.sql 学生 雇员 Ex:Entity.sql 名字 地址 Ex:Result 学生 雇员 名字 地址 这是我的代码,可以将一个文件的内容复制到另一个文件中,而不覆盖该文件,但它被复制到我不想复制的同一行。在Java中,如何将文件内容复制到另一个文件但位于文件顶部,java,file,filereader,fileinputstream,filewriter,Java,File,Filereader,Fileinputstream,Filewriter,我希望将文件“Convert.sql”的内容复制到文件“Entity.sql”,而不重写“Entity.sql”文件。这一部分我已经能够做到了,但除此之外,我希望文件“Convert.sql”的内容复制到“Entity.sql”文件的顶部,并且不在同一行 Ex:Convert.sql 学生 雇员 Ex:Entity.sql 名字 地址 Ex:Result 学生 雇员 名字 地址 这是我的代码,可以将一个文件的内容复制到另一个文件中,而不覆盖该文件,但它被复制到我不想复制的同一行。 package
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();
}
}
有人能帮我做这个吗?使用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.close();
这里有一个代码片段,它将完成您描述的工作
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);
Charset usedCharset=Charset.defaultCharset();
//将Convert.sql中的所有行读取到列表中
列出allLines=Files.readAllLines(path.get(“Convert.sql”)、usedCharset);
//将Entity.sql中的所有行追加到列表中
addAll(Files.readAllLines(path.get(“Entity.sql”),usedCharset));
//将列表中的所有行写入文件Entity.sql
write(path.get(“Entity.sql”)、allLines、usedCharset);
如果要处理整个文件,根本不需要处理行或将内容解释为文本(即应用字符编码):
只需完全读取要预编译的文件,复制另一个文件并附加第一步读取的旧数据
这使用了Java 7 API,但我认为值得学习。读取文件1的内容,读取文件2的内容,将文件1的内容写入文件2,将文件2之前的内容附加到文件2。Superbe!!非常感谢!!:)你太棒了!
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);