Buffered Writer偶尔会创建符号而不是数字

时间:2016-09-07 22:30:59

标签: java io notepad bufferedwriter wordpad

所以我有一个程序收集大量数据并连续地将数据连接成一个字符串,每个条目之间只有一个空格。在我关闭例程期间,我使用缓冲编写器将String打印到txt文件中。大约50%的时间数据显示为(大多数)中文符号。 VM是否正在做一些奇怪的Unicode内容?为什么这有时只会发生?

我在其他论坛上环顾四周,还没有看到此问题的其他实例。我所知道的其他CS专业都不了解正在发生的事情。

编辑:数据是0-1365范围内的所有整数;

更新:经过进一步的研究,我发现this让我觉得可能需要一个PrintStream而不是BufferedWriter可以让任何人说话吗?我测试了PrintStream,我将无法使用FileWriter构建它,因为我会使用BufferedWriter,这意味着我需要更多的研究来写入我的txt。

更新:打印到控制台不会发生此错误。我将接受一个解释方式的答案记事本(我用来打开txt的程序)有时会显示数字,有时会显示符号。

以下是相关代码:

 //fields
private static BufferedWriter out;
private File saveFile;
String data;
 //inside constructor 
this.saveFile = new File("C:\\Users\\HPlaptop\\Desktop\\MouseData.txt");
                this.saveFile.delete();
                try{this.saveFile.createNewFile();}
                catch (IOException e ){System.out.println("File creation error");}
try {out = new BufferedWriter(new FileWriter("C:\\Users\\HPlaptop\\Desktop\\MouseData.txt"));}
                catch (IOException e) {System.out.println("IO Error");}
                this.control.addWindowListener(new WindowAdapter()
                {
                 public void windowClosing(WindowEvent e)
                    { //there is a method call here but the basics are below
                        out.write(data);
                    out.close();
                        System.exit(0);
                    }
                });

以下是正确打印的示例数据集:

 1365 767 1365 767 1365 767 1364 767 1353 756 1268 692 1114 604 980 488 812 334 744 283 694 244 593 150 473 81 328 13 207 0 124 0 115 0 102 0 99 6 107 13 132 20 173 32 187 31 190 25 194 20 201 17 215 14 221 10 224 7 224 7 224 7 226 6 226 6 226 6 226 6 226 6 226 6 226 6

这个数据集是在几秒钟之后拍摄的,并不是我想要的

㐀ㄹ㈠㤰㐠㔸㈠㈱㐠㠶㈠㐱㐠㘲㈠㘰㌠㠷ㄠ㔹㌠㌳ㄠ㌹㈠㘹㈠㄰㈠㠷㈠㜳㈠㐶㈠㐷㈠㐶㈠㔷㈠㌶㈠㔵㈠㐵㈠㠰㈠㤴ㄠ㔲㈠㤴㐠‶㐲‹㌱㈠㘴〠㈠㘴〠㈠㘴〠㈠㜴〠㈠㠴〠㈠㠴〠㈠㜴㠠㈠㔴ㄠ‶㐲‵㤱㈠㔴ㄠ‹㐲‵㠱㈠㜴ㄠ‶㐲‹ㄱ㈠〵ㄠ‰㔲‰〱

2 个答案:

答案 0 :(得分:1)

BufferedWriter没有出错,代码是正确的,除了 使用

的冗余
class BodyClass(BaseClass):
    def __init__(self, name):
        super().__init__(name)


    def Start(self):
        global i
        i = 0
        global d
        d = True
        global obj1
        obj1 = ChildClassA('Obj1')
        global obj2
        obj2 = ChildClassB('Obj2')
        global obj3
        obj3 = ChildClassC('Obj3')
        global obj4
        obj4 = ChildClassB('Obj4')
        global obj5
        obj5 = ChildClassB('Obj5')
        global obj6
        obj6 = ChildClassB('Obj6')

    def Update(self):


        global i
        global d
        global obj1
        global obj2
        global obj3
        global obj4
        global obj5
        global obj6

        print ("TYPE:", obj3.CType())
        i = i+ 1
        time.sleep(0.05)
        print("i: ", i)
        if i >= 10:
            if d:
                print("UNLOADING")
                obj2.Unload()
                obj4.Unload()
                obj5.Unload()
                obj6.Unload()

                d=False

this.saveFile.delete();
 try{this.saveFile.createNewFile();}
                catch (IOException e ){System.out.println("File creation error");}

打开文件时会发生读取数据的错误。由于软件读取数据的方式,根据打开数据的程序显示不同的结果。记事本显示符号,因为它将数字解释为ASCII。控制台没有尝试解释数据,只显示写入的内容。使用不尝试解释文件中的数字的程序将允许正确查看数据。

答案 1 :(得分:0)

由于您没有提供您在流中写入的数据的示例,您可能会遇到 bush隐藏事实现象。