if (fileScan.hasNext(posPattern)) {
posCB = fileScan.nextInt() + 1;

for (int i = posCB; i > 0; i--) {
String writeByte = fileScan.next();
BufferedWriter out = new BufferedWriter(new FileWriter("E:\\dataReadable.txt"));
out.write(writeByte);
out.close();
}//for
}

Using a for loop, how would I write to the file without clearing the contents, so it just appends to the end of the file?

Recommended Answers

All 4 Replies

// Use this constructor
FileWriter("PATH",boolean append)

i mean to enable append use
FileWriter("PATH",true);

You should better remove the initialization of BufferedWriter outside the loop. Why you need it in? Each time you create a new one, you close it then create again a new one?
Put it outside the loop, in the loop call only the write method and close after the loop.
Understand what your code does.

The suggestions from both Majestics and javaAddict will work. For larger data sets, opening the file once, rather than appending to it, will give much better performance. (This favors the javaAddict solution.)

Thanks for all the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.