Skip to main content
added 28 characters in body
Source Link
bobobobo
  • 233
  • 1
  • 6

Definitely, definitely take the if statement out of the loop. People always talk about "the optimizer" but optimizers are different, and whatever you can do to help it is probably a good idea.

//Write contents to the file
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
for(int i = 0; i < lines.size() - 1; i++) {
    writer.write(lines.get(i));
    writer.newLine();
} 

// Write the last one without extra newline
if( lines.size() )
  writer.write(lines.get(lines.size()-1));

Definitely, definitely take the if statement out of the loop. People always talk about "the optimizer" but optimizers are different, and whatever you can do to help it is probably a good idea.

//Write contents to the file
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
for(int i = 0; i < lines.size() - 1; i++) {
    writer.write(lines.get(i));
    writer.newLine();
}
// Write the last one without extra newline
writer.write(lines.get(lines.size()-1));

Definitely, definitely take the if statement out of the loop. People always talk about "the optimizer" but optimizers are different, and whatever you can do to help it is probably a good idea.

//Write contents to the file
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
for(int i = 0; i < lines.size() - 1; i++) {
    writer.write(lines.get(i));
    writer.newLine();
} 

// Write the last one without extra newline
if( lines.size() )
  writer.write(lines.get(lines.size()-1));
Source Link
bobobobo
  • 233
  • 1
  • 6

Definitely, definitely take the if statement out of the loop. People always talk about "the optimizer" but optimizers are different, and whatever you can do to help it is probably a good idea.

//Write contents to the file
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
for(int i = 0; i < lines.size() - 1; i++) {
    writer.write(lines.get(i));
    writer.newLine();
}
// Write the last one without extra newline
writer.write(lines.get(lines.size()-1));