You are on page 1of 1

String nextLine;

int lineNumber = 0;
while((nextLine = reader.readLine()) != null) //get the next line and stop when
none are left.
{
if(lineNumber== line) //Insert line here
{
out.write(str);
writer.newLine();
}
if(lineNumber > 0) //insert a new line before we write the next line unless we
are at the beginning.
{
out.newLine();
}
out.write(nextLine);
lineNumber++;
}
if(lineNumber <= line) //Line not yet written.
{
//Add extra lines. If you never want extra lines, make sure you at least handle
the case where a line is the very last line.
if(lineNumber == 0) //special check for empty files.
lineNumber++;
for(;lineNumber <= line; lineNumber++)
out.newLine();
out.write(str);
}

You might also like