8

由于某种原因,此代码会导致 text.txt 文件被截断。它应该(根据我)写出 1000 个结果,但输出文件有不同数量的行(取决于运行)。奇怪的是,对文件的写入在写入命令的中间停止,因此一行可能不完整。目前,最新运行的文本文件的最后三行如下:

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

就是这样。之后就没有别的了。

这是代码:

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++;
        }
    }
}

}

4

4 回答 4

17

您忘记 close() 编写器,因此您从未给它机会将缓冲的输出刷新到磁盘。

于 2012-08-16T01:48:55.057 回答
2

您应该考虑在每次写入后刷新您的流。尝试这样的事情:

try{
    //your code
    out.write("" + i + ", " + red + ", " + green + ", " + blue
            + ", " + (.21 * red + .71 * green + 0.08 * blue + "\n"));
    i++;
}finally{
    //Close the stream
    out.close();
}

此外,您应该确保在操作结束时关闭您的流。构建程序的一个好方法可能是:

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();
    }
}
于 2012-08-16T01:45:28.467 回答
2

两件事情。

  1. 冲洗流
  2. 关闭流

尝试类似:

 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) {
    }
}

试着记住,它是一个“缓冲区”。这意味着它将内容存储在内存中,直到它决定需要写入或您明确要求它“刷新”它的内容。

此外,您应该始终使用close您的流。这可以防止可能的锁定文件问题和文件句柄问题:P

于 2012-08-16T01:45:51.347 回答
0

一旦打开 io(Reader、Writter 或 BufferR/W,...),您必须稍后关闭它以关闭该流并释放资源。如果您的代码有嵌套流 (File, FileReader, BufferedReader) 或 (Writer1, Writer2),您必须在 (BufferedReader, FileReader, File) 或 (Writer1.close() 之后使用 Writer2 之前单独关闭它)

于 2018-10-31T12:00:45.180 回答