0

I have a program that is supposed to generate a file with a random integer name and append 200 characters of data to each file. I have already succeeded in being able to create a file:

BufferedOutputStream bos = new BufferedOutputStream(Files.newOutputStream(
         new File("C:/Users/mirvine/Desktop/SPAM").toPath()));

And I have gotten it to write chars to the file:

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

But when I combine the two with a for loop, it doesn't work:

try {
            while(true) {
                int i = rand.nextInt();
                File outputFile = new File("C:/Users/mirvine/Desktop/SPAM/"+ String.valueOf(i) +".txt");

                    bos = new BufferedOutputStream(Files.newOutputStream(outputFile.toPath()));
                    PrintWriter writer = 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 to " + String.valueOf(i) + ".txt!");
                }
                    System.out.println("File named \'" + String.valueOf(i) + ".txt\' created!");

            }
        } catch (Exception e) {e.printStackTrace(); return;}

I will get the output "Total: (number) characters written to (whatever).txt!" but it won't actually write the characters. However, if I make the loop infinite (by changing qu++ to qu--) it will write the characters, of course with only one file though. I even tried changing it to a while loop and it didn't work. Any ideas?

mirvine
  • 126
  • 1
  • 2
  • 14

2 Answers2

1

Consider changing the use of BufferedOutputStream and PrintWriter for FileWriter, which will take your file as an argument to the constructor.

Also, make sure you flush and close the stream after finishing with it.

Fallso
  • 1,311
  • 1
  • 9
  • 18
  • I didn't feel the need to close it because it was supposed to go on for infinity, but I did forget to flush, and I noticed that I defined it inside the `while` loop and should close it. That was my problem. Thanks! – mirvine Nov 11 '14 at 14:23
0

You should use a BufferedWriter, it's more efficient, and don't forget to close it at the end. The infinite loop is useless.

int i = rand.nextInt();
File outputFile = new File("C:/Users/mirvine/Desktop/SPAM/"+ String.valueOf(i) +".txt");
FileOutputStream fos = new FileOutputStream(outputFile);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));  
for(int qu = 0; qu <= 2000; qu++)
{
    bw.write((char)rand.nextInt(255));
    System.out.println("Total " + String.valueOf(qu) + " characters written to " + String.valueOf(i) + ".txt!");
}
bw.close();
ToYonos
  • 16,469
  • 2
  • 54
  • 70
  • Whilst viable, it's recommended to use FileWriter instead of OutputStreamWriter when writing character data to files, as it will automatically setup character encoding. Nice tip about wrapping it in a BufferedWriter, though! Forgot the raw FileWriter wasn't buffered... – Fallso Nov 11 '14 at 14:34