For some reason this code results in a truncated text.txt file. It should (according to me) write out 1000 results, but the output file has various amounts of lines (dependi
You should consider flushing your stream after each write. Try something like this:
try{
//your code
out.write("" + i + ", " + red + ", " + green + ", " + blue
+ ", " + (.21 * red + .71 * green + 0.08 * blue + "\n"));
i++;
}finally{
//Close the stream
out.close();
}
Also you should make sure that you close your stream at the end of your operation. A good way to structure your program might be this:
Random generator = new Random();
float red = 0, green = 0, blue = 0;
int i = 0;
Writer out = null;
try{
out = new BufferedWriter(new FileWriter("test.txt"), 16 * 1024);
while (i < 1000) {
//your logic
if (red < 256 && blue < 256 && green < 256) {
out.write("" + i + ", " + red + ", " + green + ", " + blue
+ ", " + (.21 * red + .71 * green + 0.08 * blue + "\n"));
i++;
}
}
}finally{
if(out != null){
out.close();
}
}