BufferedWriter overwriting itself

Posted by Danson on Stack Overflow See other posts from Stack Overflow or by Danson
Published on 2013-10-22T15:34:19Z Indexed on 2013/10/22 15:54 UTC
Read the original article Hit count: 184

Filed under:

I want to read in a file and create a duplicate of the file but my code only write the last line in the file. How do I make so that whenever I call write(), it writes to a new line. I want to create a new file for the duplicate so I can't add true to FileWriter constructor.

This is my code:

    //Create file reader
    BufferedReader iReader = new BufferedReader(new FileReader(args[1]));

    //Create file writer
    BufferedWriter oWriter = new BufferedWriter(new FileWriter(args[2], true));

    String strLine;
    //reading file
    int iterate = 0;
    while((strLine = iReader.readLine()) != null)  {
        instructions[iterate] = strLine;
    }

    //creating duplicate
    for(int i = 0; i < instructions.length; i++) {
        if(instructions[i] != null) {
            oWriter.write(instructions[i]);
            oWriter.newLine();
        }  else {
            break;
        }
    }

    try {
        iReader.close();
        oWriter.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

© Stack Overflow or respective owner

Related posts about java