Skip to main content
added 5 characters in body
Source Link
Simon Forsberg
  • 59.4k
  • 9
  • 156
  • 309

A small twist on @rolfls' answer:

BufferedWriter writer = new BufferedWriter(new FileWriter(file));
if ( lines.size() > 0 ) {
    writer.write(lines.get(0));
}
for (int i = 1; i < lines.size(); i++) {
    writer.newLine();
    writer.write(lines.get(i));
}

Exact same idea though, move the extra check outside of the loop.

A small twist on @rolfls' answer:

BufferedWriter writer = new BufferedWriter(new FileWriter(file));
if ( lines.size() ) {
    writer.write(lines.get(0));
}
for(int i = 1; i < lines.size(); i++) {
    writer.newLine();
    writer.write(lines.get(i));
}

Exact same idea though, move the extra check outside of the loop.

A small twist on @rolfls' answer:

BufferedWriter writer = new BufferedWriter(new FileWriter(file));
if ( lines.size() > 0 ) {
    writer.write(lines.get(0));
}
for (int i = 1; i < lines.size(); i++) {
    writer.newLine();
    writer.write(lines.get(i));
}

Exact same idea though, move the extra check outside of the loop.

Source Link
smassey
  • 161
  • 6

A small twist on @rolfls' answer:

BufferedWriter writer = new BufferedWriter(new FileWriter(file));
if ( lines.size() ) {
    writer.write(lines.get(0));
}
for(int i = 1; i < lines.size(); i++) {
    writer.newLine();
    writer.write(lines.get(i));
}

Exact same idea though, move the extra check outside of the loop.