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

Ads

 
 

Share on Google+Share on Google+

HHH
How to copy many files into one file and how to manage the content of the final file?
3 Answer(s)      6 years and 4 months ago
Posted in : Java Beginners

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

Ads
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:
Advertisements
Tutorials   
Java Spring Hibernate Struts Training How do beginners learn about Java? ClassNotFoundException HttpRequestInterceptor java.lang.noclassdeffounderror: org/apache/http/httprequest noclassdeffounderror: org/apache/http/client/methods/httpurirequest java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient How do I resolve this Java Class not found exception? httpclient java.lang.NoClassDefFoundError Apache Commons ioutils maven dependency Read/Convert an inputStream to a String What is the meaning of Java Platform? Why Java is a platform independent language? What is the benefits of learning Core Java? Which technology should I learn after Java? What is array in java with example? How to Convert ArrayList to Array? How to substring in Java? How to format number in Java? What is instance variable in Java? How to download MySQL JDBC driver? What is Calendar class in Java? Which is the best Java tutorials for beginners? How to rename a file in Java? How to delete file in Java code? How to get day from date in Java using Calendar? How to get day of week in Java? How to calculate Date Difference in Java? How to compare date in Java? How to declare array in Java? How to calculate average of array in Java? What is Array in Java? write a java program to find the summation of all the integers entered on command line Sum of two numbers using command line arguments in Java How to create and use Array in Java? How to pass command line arguments in Java? How to create Applet Hello World? Appending String efficiently in Java How to append String in Java? How to list even numbers between 1 and 100? How to add BigDecimal in Java? What is Abstraction In Java? Which is best Beginners Java Tutorial? What is java.util package? Create list from array in Java Filter collection in Java 8 What is the best way to filter a Java Collection? Easy way to transform Collection to Array? How to convert Collection to Array in Java? What are Basic Java Language Elements? Advanced Java Tutorials in 2017 Java brief history

Ads

 
Advertisement null

Ads