Search code examples
javaionotepadbufferedwriterwordpad

Buffered Writer occasionally creates symbols rather than numbers


So I have a program that collects a bunch of data and continuously concatenates the data into a string with a single white space between each entry. During my close routine I print the String into a txt file using buffered writer. About 50% of the time the data shows up as (mostly) Chinese symbols. Is the VM doing some weird Unicode stuff? Why does this only occur sometimes?

I've looked around on other forums and have not seen other instances of this problem. None of the other CS majors I know understand what is happening.

EDIT : the data is all integer numbers ranging 0-1365;

UPDATE: upon further research I found this which makes me think a may need a PrintStream rather than a BufferedWriter can anyone speak to that? I tested the PrintStream and I will not be able to construct it with a FileWriter as I would a BufferedWriter which means I need more research to write to my txt.

UPDATE: printing to the console does not make this error occur. I will accept an answer that explains way Notepad (the program I am using to open the txt) sometimes displays numbers and sometimes displays symbols.

Here is the relevant code:

 //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);
                    }
                });

Here is an example data set printed correctly:

 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

This data set was taken seconds later and is not what I want

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

Solution

  • The BufferedWriter is not making an error and the code is correct except for the redundancy of using

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

    and

    new FileWriter
    

    The error in reading the data occurs when the file is opened. The depending on what program opens the data different results are displayed because of the way the software reads the data. Notepad was displaying symbols because it interpreted the numbers as ASCII. The console did not try to interpret the data and just displayed what was written to it. Using a program that does not try to interpret the numbers in the file will allow the data to be viewed correctly.