我试图从文件中读取一些内容,将其解析为我自己的数据类型。但是,最初文件看起来像这样:
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
然后我这样读了:
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);
}
然后我的文件看起来像这样:
-----
; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
;;;;;;;;;;;;;;;
; ; ; ;;;;;;;;; ; ; ;
;;; ;;;;;;;;; ;;;
;;; ;;;;;;;;; ;;;
;;; ;;;;;;;;; ;;;
;;; ;;;;;;;;; ;;;
;;; ;;;;;;;;; ;;;
;;; ; ; ; ;;; ; ; ; ;;;
;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;
; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
这是在Notepad ++中打开的:
我尝试使用InputStream等初始化BufferedReader的不同变体。当我尝试使用BufferedWriter写回文件时,会发生同样的事情。
文件扩展名(虽然我不知道为什么会这么重要)是.ddm
。
所以我想我想知道为什么会这样,以及如何解决它。
在您的代码中的某个时刻(缺少)您正在写入该文件。我怀疑你的代码看起来像这样:
for(Integer value:values){
bufferedWriter.write(value);
}
将value
视为char
,将整数0
转换为(char)0
。您希望将值写为String,因此您应该使用
bufferedWriter.write(String.valueOf(value));