Thursday, 15 July 2010

java - For-loop not executing File writing commands -



java - For-loop not executing File writing commands -

i have programme supposed generate file random integer name , append 200 characters of info each file. have succeeded in beingness able create file:

bufferedoutputstream bos = new bufferedoutputstream(files.newoutputstream( new file("c:/users/mirvine/desktop/spam").topath()));

and have gotten write chars file:

bos.write((char)rand.nextint(255));

but when combine 2 loop, doesn't work:

try { while(true) { int = rand.nextint(); file outputfile = new file("c:/users/mirvine/desktop/spam/"+ string.valueof(i) +".txt"); bos = new bufferedoutputstream(files.newoutputstream(outputfile.topath())); printwriter author = new printwriter(bos); for(int qu = 0; qu <= 2000; qu++) { writer.write((char)rand.nextint(255)); system.out.println("total " + string.valueof(qu) + " characters written " + string.valueof(i) + ".txt!"); } system.out.println("file named \'" + string.valueof(i) + ".txt\' created!"); } } grab (exception e) {e.printstacktrace(); return;}

i output "total: (number) characters written (whatever).txt!" won't write characters. however, if create loop infinite (by changing qu++ qu--) write characters, of course of study 1 file though. tried changing while loop , didn't work. ideas?

consider changing utilize of bufferedoutputstream , printwriter filewriter, take file argument constructor.

also, create sure flush , close stream after finishing it.

java for-loop io printwriter bufferedoutputstream

No comments:

Post a Comment