将多个文件合并为一个

我在一些位置有4个不同的文件,如:D:\ 1.txt D:\ 2.txt D:\ 3.txt和D:\ 4.txt

我需要创建一个新文件NewFile.txt ,它应该包含上述文件中存在的所有内容1.txt,2.txt,3.txt 4.txt …….

所有数据都应出现在New Single文件(NewFile.txt)中。

请建议我在java或Groovy中做同样的想法….

这是在Groovy中执行此操作的一种方法:

// Get a writer to your new file new File( '/tmp/newfile.txt' ).withWriter { w -> // For each input file path ['/tmp/1.txt', '/tmp/2.txt', '/tmp/3.txt'].each { f -> // Get a reader for the input file new File( f ).withReader { r -> // And write data from the input into the output w << r << '\n' } } } 

这样做的好处(在每个源文件上调用getText )是在将其内容写入newfile之前不需要将整个文件加载到内存中。 如果您的某个文件非常庞大,则另一种方法可能会失败。

这是时髦的

 def allContentFile = new File("D:/NewFile.txt") def fileLocations = ['D:/1.txt' , 'D:/2.txt' , 'D:/3.txt' , 'D:/4.txt'] fileLocations.each{ allContentFile.append(new File(it).getText()) } 

我尝试解决这个问题,如果你将内容复制到一个数组并将数组写入另一个文件,我发现它很容易

 public class Fileread { public static File read(File f,File f1) throws FileNotFoundException { File file3=new File("C:\\New folder\\file3.txt"); PrintWriter output=new PrintWriter(file3); ArrayList arr=new ArrayList(); Scanner sc=new Scanner(f); Scanner sc1=new Scanner(f1); while(sc.hasNext()) { arr.add(sc.next()); } while(sc1.hasNext()) { arr.add(sc1.next()); } output.print(arr); output.close(); return file3; } /** * * @param args * @throws FileNotFoundException */ public static void main(String[] args) { try { File file1=new File("C:\\New folder\\file1.txt"); File file2=new File("C:\\New folder\\file2.txt"); File file3=read(file1,file2); Scanner sc=new Scanner(file3); while(sc.hasNext()) System.out.print(sc.next()); } catch(Exception e) { System.out.printf("Error :%s",e); } } } 

你可以在Java中做这样的事情。 希望它可以帮助您解决问题:

 import java.io.*; class FileRead { public void readFile(String[] args) { for (String textfile : args) { try{ // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream(textfile); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null) { // Print the content on the console System.out.println (strLine); // Write to the new file FileWriter filestream = new FileWriter("Combination.txt",true); BufferedWriter out = new BufferedWriter(filestream); out.write(strLine); //Close the output stream out.close(); } //Close the input stream in.close(); }catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } } } public static void main(String args[]) { FileRead myReader = new FileRead(); String fileArray[] = {"file1.txt", "file2.txt", "file3.txt", "file4.txt"}; myReader.readFile(fileArray); } } 

我告诉你在java中完成它的方式:

 public class Readdfiles { public static void main(String args[]) throws Exception { String []filename={"C:\\WORK_Saurabh\\1.txt","C:\\WORK_Saurabh\\2.txt"}; File file=new File("C:\\WORK_Saurabh\\new.txt"); FileWriter output=new FileWriter(file); try { for(int i=0;i 

告诉我你是否有任何疑问

一个class轮示例:

 def out = new File(".all_profiles") ['.bash_profile', '.bashrc', '.zshrc'].each {out << new File(it).text} 

要么

 ['.bash_profile', '.bashrc', '.zshrc'].collect{new File(it)}.each{out << it.text} 

如果您有大文件,Tim的实施会更好。

 public static void main(String[] args) throws IOException { List files=new ArrayList(); for(int i=10;i<14;i++) files.add("C://opt/Test/test"+i+".csv"); String destFile ="C://opt/Test/test.csv"; System.out.println("TO "+destFile); long st=System.currentTimeMillis(); mergefiles(files, destFile); System.out.println("DONE."+(st-System.currentTimeMillis())); } public static void mergefiles(List files,String destFile){ Path outFile = Paths.get(destFile); try(FileChannel out=FileChannel.open(outFile, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) { for(String file:files) { Path inFile=Paths.get(file); System.out.println(inFile); try(FileChannel in=FileChannel.open(inFile, StandardOpenOption.READ)) { for(long p=0, l=in.size(); p