Java writing to text files.

Soldato
Joined
29 Apr 2007
Posts
4,841
Location
London
Hi im writing to text files in Java using

Code:
PrintWriter out = new PrintWriter(new FileWriter("file.txt"));
int i = 0;
for (i=0; i < toPrint.length - 1; i++)
{
       out.print(toPrint [i] + "\n");
}

To print an array of 2000 words.

However it stop as soon as the file size reaches 8KB.
Have you got any ideas how I can fix this? It just stops half way through a word.
 
Last edited:
well now im using this code

Code:
BufferedWriter out = new BufferedWriter(new FileWriter("file.txt"), int sz);
 
int i = 0;
for (i=0; i < toPrint.length - 1; i++)
{
out.write(toPrint [i])
out.newLine();
}
out.close();

That int sz bit declares the size of the buffer. What should I type in there?

Seems to work by typing 100000 in but maybe thats a silly number?
 
Last edited:
Thanks very much :) That works well.

Weird thing is, before reading in the original file has 2,011 words. After writing out it has 1870, im not really sure why.
 
Last edited:
Back
Top Bottom