Hi,
i'm trying to code that copy the content of all txt file in a folder and paste it into one file.
My following code is working but the problem is for example if i have three txt files my program copy the content of the first txt file, after the second file is copied just below the first file and the third file is copied just after the second txt file but not bellow.
How is it possible to put the content of each file below the other one ?
Here is my code snipet:
[CODE] package FileStream2; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.*; import java.util.logging.*;
class FileStream2 { public static void main(String[] args) {
File save = new File("Final_save.txt"); if(!save.exists()){ try { save.createNewFile(); } catch (IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } File here = new File("."); OutputStream out = null; try { out = new FileOutputStream(save); / for(File content : here.listFiles()) { if(!(content.getName().equals(save.getName())) && content.isFile() && content.getName().endsWith(".txt")) { FileInputStream in = null; try { in = new FileInputStream(content); byte buffer[] = new byte[512*1024]; int nbLecture = 0; while( (nbLecture = in.read(buffer)) != -1 ) { out.write(buffer, 0, nbLecture); } } catch (IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } finally{ try { in.close(); } catch (IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } } } } catch (FileNotFoundException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } finally{ try { out.close(); } catch (IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } }
[CODE/]
Thanks a lot
Hi Friend,
You can try the following code:
import java.io.*; class OnlyExt implements FilenameFilter{ String ext; public OnlyExt(String ext){ this.ext="." + ext; } public boolean accept(File dir,String name){ return name.endsWith(ext); } } public class FileStream2 { public static void main(String[] args) throws Exception { FilenameFilter ff = new OnlyExt("txt"); File folder = new File("c:/"); File save = new File("Final_save.txt"); BufferedWriter bw = new BufferedWriter(new FileWriter(save,true)); File[] files = folder.listFiles(ff); for (int i = 0; i < files.length; i++) { System.out.println(); System.out.println("Processing " + files[i].getPath() + "... "); BufferedReader br = new BufferedReader(new FileReader(files[i].getPath())); String line; while ((line=br.readLine())!=null) { bw.write(line); bw.newLine(); bw.newLine(); System.out.println(line); } br.close(); } bw.close(); } }
Thanks
Hi thank you for your quick reply. i will try youe example c u
hi, your code is working very well, as i'm learning java it 's really usefull for me.
But suppose i have: - 4 text files -and i want to concatenate them in one file but during concatenating i want to keep the whole content of the first file.
-and for the 3 following files i want skip the 10 first line of the text.
I don"t know how to do it, i know how to extract specific line but the code is too long and too heavy.
Thank you very much
Ads