How to copy many files into one file and how to manage the content of the final file?

Ads
 

How to copy many files into one file and how to manage the content of the final file?

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

View Answers

January 19, 2011 at 12:49 PM

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


January 19, 2011 at 2:31 PM

Hi thank you for your quick reply. i will try youe example c u


January 19, 2011 at 3:09 PM

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









Related Tutorials/Questions & Answers:
How to copy many files into one file and how to manage the content of the final file?
How to convert many class files into one exe file?
Advertisements
How to convert many class files into one exe file?
How to copy files from one folder to another based on lastmodified date - Java Beginners
How to copy files from one folder to another based on lastmodified date - Java Beginners
how to copy files from remote location to local using java?
Copy file in php, How to copy file in php, PHP copy file
How to copy a file in java
How to Convert Text Files into Gzip File
Copy one file into another
how to copy file from one directory to another in java with examples
how to copy files from remote to local machine using SFTP SSL in java?
files that are present in any folder , how to make it clickable downloadble file in php
Copy Files - Java Beginners
How to list files in hadoop?
how to upload multiple files in jsp and saving the path in database and the file in folder
Java File - Learn how to handle files in Java with Examples and Tutorials
How Compare 2 xml files with JDOM - Java Beginners
how to backup files and folder with Java
Copy multiple files
i want to copy files from one machine to other using java and the code will be running on third machine
Java file
How to create a new text file that contains the file names of the copied files in a new directory? - Java Beginners
how to read files of directory objective c
How to write content of one file to another file.
How do I decompile Java class files?
How to avoid Java Code in JSP-Files?
How To Write the FORM Tag Correctly for Uploading Files?
how to creating hbm files in hibernate using Myeclipse
How to upload files to server using JSP/Servlet?
how to download the uploaded folder files using jsp

Ads