0

I'm trying to code something to copy the content of one file into another existent file without deleting its content. The problem is that, that other existent file is created inside a loop and I don't know how to do it to save it and then using it in another method.

Here's the loop:

if (line.startsWith("tipo1.")) {
    FileWriter fw = new FileWriter(name + ".txt");
    char[] vector = name.toCharArray();
    char[] vector2 = address.toCharArray();
    int index = 0;
    while (index < vector.length) {
        fw.write(vector[index]);
        index++;
    }
    index = 0;
    while (index < vector2.length) {
        fw.write(vector2[index]);
        index++;
    }

    fw.close();
}

And what I want to do is to save (name + ".txt") file and then adding the content of another file into it.

I'm sure it's not so difficult to do, but I'm really stuck.

rghome
  • 8,529
  • 8
  • 43
  • 62
Johnny
  • 41
  • 1
  • 10
  • [This may help.](http://www.tutorialspoint.com/java/java_filewriter_class.htm) I think you may need to add the boolean to the filewriter to make it append to the file – Parttimereaper Jan 12 '16 at 21:50
  • [FileWriter documentation](https://docs.oracle.com/javase/8/docs/api/java/io/FileWriter.html#FileWriter-java.lang.String-boolean-). [Another thread in StackOverflow](http://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java) – AndrewMcCoist Jan 12 '16 at 21:57

2 Answers2

0

You already have two separate pieces of information being written into the file: the name (as vector) and the address (as vector2). Why not just read in your other file as, say, vector3 and add one more while loop?

Erick G. Hagstrom
  • 4,873
  • 1
  • 24
  • 38
  • I tried it before, but it doesn't let me to convert it into a char array, that's why I was asking how to save it and use it in another method @Erick G. Hagstrom – Johnny Jan 12 '16 at 22:13
  • But Maria, you know how to read a file in to a String and convert it to a char array. Why is this any different? – Erick G. Hagstrom Jan 13 '16 at 00:35
0

If you want this done the easy way then use Apache IO, and use: File ip = new File("input.txt"); File op = new File("output.txt"); FileUtils.copyFile(ip, op);

FileReader ip = new FileReader("input.txt");
FileWriter op = new FileWriter("output.txt", true);
int c;
while (true) {
  c = ip.read();
  if (c == -1) {
    break;
  }
  op.write((char)c);
}
ip.close();
op.close();
LOTUSMS
  • 10,317
  • 15
  • 71
  • 140
Jay Patel
  • 46
  • 5
  • where should i write that? Inside the if loop? @Jay Patel – Johnny Jan 12 '16 at 22:12
  • The 'new FileOutputStream' will create a new file with given name only if it does not exist. The write method will append the bytes to the file as we opened the file using 'true' value. So when ever you want to open a file use new FileOutputStream and when you want to append anything to file use write method. – Jay Patel Jan 12 '16 at 22:25
  • The problem is that I want to write one file into another, not just words into a file. I'm not sure if you understood what I meant. @Jay Patel – Johnny Jan 12 '16 at 22:32
  • If you want this done the easy way then use [Apache IO](http://central.maven.org/maven2/org/apache/commons/commons-io/1.3.2/commons-io-1.3.2.jar), and use: `File ip = new File("input.txt"); File op = new File("output.txt"); FileUtils.copyFile(ip, op);` – Jay Patel Jan 12 '16 at 22:44
  • And what's not the easy way? @Jay Patel – Johnny Jan 12 '16 at 22:51
  • I am sorry. I may have misunderstood your question. I have change my answer after understanding your requirement. Let me know if this is not what you required. – Jay Patel Jan 12 '16 at 23:01
  • The problem is that the file to append doesn't have a certain name (check the code I posted), and my idea was to use only FileReader, FileWriter and BufferedReader, is that possible? @Jay Patel – Johnny Jan 12 '16 at 23:05