0

I would liked to copy the content of file "Convert.sql" to file "Entity.sql" without overriding the "Entity.sql" file. This part i've been able to do it but in addition to that, i would liked the content of the file "Convert.sql" to be copied at the top of "Entity.sql" file and also not on the same line.

Ex: Convert.sql

Student

Employee

Ex: Entity.sql

Name

Address

Ex:Result

Student

Employee

Name

Address

Here is my code to copy content of a file to another file without overriding the file but it is being copied it on the same line which i don't want to.

package Final;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class Mainy {
    public static void main(String[] args) throws IOException {
        File dir = new File(".");

        String source = dir.getCanonicalPath() + File.separator + "Convert.sql";
        String dest = dir.getCanonicalPath() + File.separator + "Entity.sql";

        File fin = new File(source);
        FileInputStream fis = new FileInputStream(fin);
        BufferedReader in = new BufferedReader(new InputStreamReader(fis));

        FileWriter fstream = new FileWriter(dest, true);
        BufferedWriter out = new BufferedWriter(fstream);

        String aLine = null;
        while ((aLine = in.readLine()) != null) {
            //Process each line and add output to Dest.txt file
            out.write(aLine);
            out.newLine();
        }

        // do not forget to close the buffer reader
        in.close();

        // close buffer writer
        out.close();
    }
}

Can someone please help me out to do this?

Lana
  • 171
  • 1
  • 7
  • 18
  • 3
    Read the contents of file one, Read the contents of file two, write the contents of file one to file two, append the previous contents of file two to file two. – Stultuske Feb 24 '15 at 08:13

3 Answers3

1

Here a snippet which will do the job as you described

Charset usedCharset = Charset.defaultCharset();

// read all lines from Convert.sql into a list
List<String> allLines = Files.readAllLines(Paths.get("Convert.sql"), usedCharset);

// append all lines from Entity.sql to the list
allLines.addAll(Files.readAllLines(Paths.get("Entity.sql"), usedCharset));

// write all lines from the list to file Entity.sql
Files.write(Paths.get("Entity.sql"), allLines, usedCharset);
SubOptimal
  • 22,518
  • 3
  • 53
  • 69
0

Use FileWriter(file,true) to Append to File

File file=new File("Entity.sql");
FileWriter fileWritter = new FileWriter(file.getName(),true);
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);

and then read the Convert.sql and write data

bufferWritter.write(data);

Then Close the bufferWritter

bufferWritter.close();
prasadmadanayake
  • 1,415
  • 15
  • 23
0

If you want to process whole files, there is no need to deal with lines or to interpret the contents as text (i.e. applying a character encoding) at all:

Path in=Paths.get("Convert.sql"), out=Paths.get("Entity.sql");
byte[] old=Files.readAllBytes(out);
Files.copy(in, out, StandardCopyOption.REPLACE_EXISTING);
Files.write(out, old, StandardOpenOption.APPEND);

Just as others said, read the file you want to prepend to completely, copy the other file and append the old data you have read in the first step.

This uses the Java 7 API but I think, it’s worth learning it.

Community
  • 1
  • 1
Holger
  • 285,553
  • 42
  • 434
  • 765