Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending multiple files into one

I have 4 different files in some locations like: D:\1.txt D:\2.txt D:\3.txt and D:\4.txt

I need to create a new file as NewFile.txt, It should contains all the contents present in the above files 1.txt, 2.txt,3.txt 4.txt.......

All Data should present in the New Single file(NewFile.txt)..

Please suggest me some idea to do the same in java or Groovy....

like image 324
Bharath A N Avatar asked Oct 25 '12 09:10

Bharath A N


4 Answers

Here's one way to do it in 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'
    }
  }
}

The advantage of doing it this way (over calling getText on each of the source files) is that it will not need to load the entire file into memory before writing its contents out to newfile. If one of your files was immense, the other method could fail.

like image 173
tim_yates Avatar answered Oct 14 '22 07:10

tim_yates


This is in groovy

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()) }
like image 44
Vamsi Emani Avatar answered Oct 14 '22 09:10

Vamsi Emani


i am showing you the way it is to be done in 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<filename.length;i++)
      {
        BufferedReader objBufferedReader = new BufferedReader(new FileReader(getDictionaryFilePath(filename[i])));

        String line;
        while ((line = objBufferedReader.readLine())!=null )
        {
          line=line.replace(" ","");

          output.write(line);
        }
        objBufferedReader.close();
      }
      output.close();
    }
    catch (Exception e) 
    {
      throw new Exception (e);
    }
  }

  public static String getDictionaryFilePath(String filename) throws Exception
  {
    String dictionaryFolderPath = null;
    File configFolder = new File(filename);
    try 
    {
      dictionaryFolderPath = configFolder.getAbsolutePath();
    } 
    catch (Exception e) 
    {
      throw new Exception (e);
    }
    return dictionaryFolderPath;
  }
}

tell me if you have any doubts

like image 30
saurabh j Avatar answered Oct 14 '22 09:10

saurabh j


I tried solving this and i found its quite easy if you copy the contents to an array and write the array to a different file

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);
    }
}
}
like image 22
The_Fresher Avatar answered Oct 14 '22 07:10

The_Fresher