Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bufferedwriter stops in the middle of writing

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 (depending on the run). Weirdly, the writing to the file stops in the middle of the write command, such that a line may not be complete. Currently, the last three lines of the text file for the latest run was as follows:

749, 78.97988, 97.80454, 99.6625, 94.00000015258789
750, 4.1745043, 86.64212, 107.59311, 71.00000008583069
751,

and that's it. Nothing else after that.

Here is the code:

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Random;

public class ColorGrayScale {

/**
 * @param args
 * @throws IOException
 */
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    Writer out = new BufferedWriter(new FileWriter("test.txt"),16*1024);
    Random generator = new Random();
    float red = 0, green = 0, blue = 0;
    int i = 0;

    while (i<1000) {

        float grey = generator.nextInt(127) + 64;
        int sequence = generator.nextInt(6) + 1; // to pick from 1 of six
                                                    // orders
        switch (sequence) { // the various orders that red green and blue
                            // are going to be in
        case 1:
            red = (float) (generator.nextFloat() * (grey / .21));
            green = (float) (generator.nextFloat() * ((grey - (red * .21)) / .71));
            blue = (float) ((grey - (red * .21) - (green * .71)) / 0.08);
            break;
        case 2:
            red = (float) (generator.nextFloat() * (grey / .21));
            blue = (float) (generator.nextFloat() * ((grey - (red * .21)) / .08));
            green = (float) ((grey - (red * .21) - (blue * .08)) / 0.71);
            break;
        case 3:
            green = (float) (generator.nextFloat() * (grey / .71));
            red = (float) (generator.nextFloat() * ((grey - (green * .71)) / .21));
            blue = (float) ((grey - (red * .21) - (green * .71)) / .08);
            break;
        case 4:
            green = (float) (generator.nextFloat() * (grey / .71));
            blue = (float) (generator.nextFloat() * ((grey - (green * .71)) / .08));
            red = (float) ((grey - (green * .71) - (blue * .08)) / .21);
            break;
        case 5:
            blue = (float) (generator.nextFloat() * (grey / .08));
            red = (float) (generator.nextFloat() * ((grey - (blue * .08)) / .21));
            green = (float) ((grey - (blue * .08) - (red * .21)) / .71);
            break;
        case 6:
            blue = (float) (generator.nextFloat() * (grey / .08));
            green = (float) (generator.nextFloat() * ((grey - (blue * .08)) / .71));
            red = (float) ((grey - (blue * .08) - (green * .71)) / .21);
            break;
        }
        if (red < 256 && blue < 256 && green < 256) {
             out.write("" + i + ", " + red + ", " + green + ", " + blue
                    + ", " + (.21 * red + .71 * green + 0.08 * blue + "\n"));
            i++;
        }
    }
}

}

like image 913
user1602004 Avatar asked Aug 16 '12 01:08

user1602004


People also ask

What is the difference between FileWriter and BufferedWriter?

FileWriter writes directly into Files and should be used only when the number of writes is less. BufferedWriter: BufferedWriter is almost similar to FileWriter but it uses internal buffer to write data into File. So if the number of write operations is more, the actual IO operations are less and performance is better.

What does the BufferedWriter close () method do?

close() method flushes the characters from the stream and then closes it. After closing, further write(), append() or flush() invocations will throw an IOException.

How do you write a new line in buffered writing?

The newLine() method of BufferedWriter class in Java is used to separate the next line as a new line. It is used as a write separator in buffered writer stream.

Can BufferedWriter only be used with files?

BufferedWriter can only be used with files. A class can have multiple methods with the same name. When writing to a file, the file must exist.


2 Answers

You forgot to close() the writer, so you never gave it a chance to flush buffered output to disk.

like image 134
Gabriella Gonzalez Avatar answered Oct 13 '22 00:10

Gabriella Gonzalez


Two things.

  1. Flush the stream
  2. Close the stream

Try something like:

 Writer out = null;
 try {
    out = new BufferedWriter(new FileWriter("test.txt"),16*1024);

    // Write some stuff

    out.flush();
 } finally {
    try {
        out.close();
    } catch (Exception exp) {
    }
}

Try and remember, it's a "buffer". That means that it's keeping stuff stored in memory until it decides it needs to be written or your explicitly ask it to "flush" it's contents.

Also, you should always close your streams. This prevents possible locked file issues and file handle issues :P

like image 32
MadProgrammer Avatar answered Oct 12 '22 22:10

MadProgrammer