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

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 a file in java
how to copy files from remote location to local using 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
Java File - Learn how to handle files in Java with Examples and Tutorials
How to list files in hadoop?
files that are present in any folder , how to make it clickable downloadble file in php
how to upload multiple files in jsp and saving the path in database and the file in folder
how to copy files from remote to local machine using SFTP SSL in java?
how to backup files and folder with Java
how to read the values for text and csv files and store those values into database in multiple rows..means one value for one row
How to delete files in Java?
Copy multiple files
One byte consists of how many bits?
i want to copy files from one machine to other using java and the code will be running on third machine
How to copy a file
how to read files of directory objective c
Java file
How to create d db for upload files using bolb i m followin dat upload file in db tutorial
How to create d db for upload files using bolb i m followin dat upload file in db tutorial
We just have one file on the hard disk. But we can create directories, sub-directories and files in that file only
We just have one file on the hard disk. But we can create directories, sub-directories and files in that file only
files
files
files
files
Files

Ads