AndideBob
AndideBob

Reputation: 160

BufferedReader changes contents of read file

I am trying to read some drom a File, parsing it into my own DataType. However, initially the file looks like this:

16
12
-----
0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0
0;2;2;2;2;2;2;2;2;2;2;2;2;2;2;0
0;2;1;1;1;1;1;2;2;1;1;1;1;1;2;0
0;2;1;0;0;0;0;5;5;0;0;0;0;1;2;0
0;2;1;0;2;2;2;2;2;2;2;2;0;1;2;0
0;2;1;0;2;2;2;2;2;2;2;2;0;1;2;0
0;2;1;0;2;2;2;2;2;2;2;2;0;1;2;0
0;2;1;0;2;2;2;2;2;2;2;2;0;1;2;0
0;1;1;0;2;2;2;2;2;2;2;2;0;1;1;0
0;0;0;0;2;2;2;2;2;2;2;2;0;0;0;0
0;2;2;2;2;2;2;2;2;2;2;2;2;2;2;0
0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0

Then I read it like this:

try {
    File file = new File(path);
    if (!file.exists()) {
        return new ScreenMap(id, 16, 12);
    }
    FileReader fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr);
    String line = br.readLine();
    int lineIndex = 0;
    //Map Constants
    ScreenMap result = new ScreenMap(id, 1, 1);
    int width = 1;
    int height = 1;
    while(line != null){
        if(lineIndex == 0){
            width = Integer.parseInt(line);
        }
        else if(lineIndex == 1){
            height = Integer.parseInt(line);
        }
        else if(lineIndex == 2){
            //Create Map
            result = new ScreenMap(id, width, height);
        }
        else if(lineIndex-3 < height){
            int y = lineIndex - 3;
            String[] tiles = line.split(seperatorString);
            for(int x = 0; x < width; x++){
                parseTileOntoMap(x,height-y-1,tiles[x],result);
            }
        }
        lineIndex++;
        line = br.readLine();
    }
    br.close();
    return result;
} catch (IOException e) {
    Logger.logError(e);
}

And afterwards my file looks like this:



-----
 ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; 
 ;;;;;;;;;;;;;;; 
 ; ; ; ;;;;;;;;; ; ; ; 
 ;;; ;;;;;;;;; ;;; 
 ;;; ;;;;;;;;; ;;; 
 ;;; ;;;;;;;;; ;;; 
 ;;; ;;;;;;;;; ;;; 
 ;;; ;;;;;;;;; ;;; 
 ;;; ; ; ; ;;; ; ; ; ;;; 
 ;;;;;;;;;;;;;;; 
 ;;;;;;;;;;;;;;; 
 ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; 

Here it is opened in Notepad++: an Image because I don't know how the text looks after copy and paste

I have tried using different varriations of initializing the BufferedReader using InputStreams, etc. The same thing happens when I try to write back to the file using a BufferedWriter.

The file extension (though I don't know why that would matter) is .ddm.

So I guess I want to know why this happens, and how to fix it.

Upvotes: 0

Views: 195

Answers (1)

f1sh
f1sh

Reputation: 11934

At some point in your code (which is missing) you are writing to the file. I suspect that your code looks something like this:

for(Integer value:values){
  bufferedWriter.write(value);
}

which treats value as a char, converting the Integer 0 to (char)0. You want to write the values as String, so you should use

bufferedWriter.write(String.valueOf(value));

Upvotes: 2

Related Questions