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