Java BufferedReader更改读取文件的内容
我试图从文件中读取一些drom,将其解析为我自己的数据类型。但是,文件最初看起来如下所示:Java BufferedReader更改读取文件的内容,java,io,bufferedreader,Java,Io,Bufferedreader,我试图从文件中读取一些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
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);
}
-----
; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
;;;;;;;;;;;;;;;
; ; ; ;;;;;;;;; ; ; ;
;;; ;;;;;;;;; ;;;
;;; ;;;;;;;;; ;;;
;;; ;;;;;;;;; ;;;
;;; ;;;;;;;;; ;;;
;;; ;;;;;;;;; ;;;
;;; ; ; ; ;;; ; ; ; ;;;
;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;
; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
for(Integer value:values){
bufferedWriter.write(value);
}
然后我这样读:
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);
}
-----
; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
;;;;;;;;;;;;;;;
; ; ; ;;;;;;;;; ; ; ;
;;; ;;;;;;;;; ;;;
;;; ;;;;;;;;; ;;;
;;; ;;;;;;;;; ;;;
;;; ;;;;;;;;; ;;;
;;; ;;;;;;;;; ;;;
;;; ; ; ; ;;; ; ; ; ;;;
;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;
; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
for(Integer value:values){
bufferedWriter.write(value);
}
在这里,它是在记事本++中打开的:
我尝试过使用InputStreams等不同的变量初始化BufferedReader。
当我尝试使用BufferedWriter写回文件时,也会发生同样的情况
文件扩展名(尽管我不知道这有什么关系)是.ddm
因此,我想我想知道为什么会发生这种情况,以及如何修复它。在代码中的某个点(缺少)正在写入文件。我怀疑您的代码如下所示:
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);
}
-----
; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
;;;;;;;;;;;;;;;
; ; ; ;;;;;;;;; ; ; ;
;;; ;;;;;;;;; ;;;
;;; ;;;;;;;;; ;;;
;;; ;;;;;;;;; ;;;
;;; ;;;;;;;;; ;;;
;;; ;;;;;;;;; ;;;
;;; ; ; ; ;;; ; ; ; ;;;
;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;
; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
for(Integer value:values){
bufferedWriter.write(value);
}
它将value
视为char
,将整数0
转换为(char)0
。您希望将值写入字符串,因此应该使用
bufferedWriter.write(String.valueOf(value));
在代码中的某个点(缺少)正在写入文件。我怀疑您的代码如下所示:
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);
}
-----
; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
;;;;;;;;;;;;;;;
; ; ; ;;;;;;;;; ; ; ;
;;; ;;;;;;;;; ;;;
;;; ;;;;;;;;; ;;;
;;; ;;;;;;;;; ;;;
;;; ;;;;;;;;; ;;;
;;; ;;;;;;;;; ;;;
;;; ; ; ; ;;; ; ; ; ;;;
;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;
; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
for(Integer value:values){
bufferedWriter.write(value);
}
它将value
视为char
,将整数0
转换为(char)0
。您希望将值写入字符串,因此应该使用
bufferedWriter.write(String.valueOf(value));
您的代码没有写入任何文件,因此我怀疑您的文件是否发生了更改。无论您在哪里写入文件,您似乎都会将
char
值而不是Integer
值写入文件,并将0
转换为NUL
等等。方法parseTileOntoMap()
的作用是什么?您确定没有其他进程或程序的一部分正在写入文件吗?“当我尝试使用BufferedWriter写回文件时,也会发生同样的情况。”表示是这样,这就是最有可能发现问题的地方。读取文件不会更改其内容。您的代码不会向任何文件写入任何内容,因此我怀疑您的文件是否会更改。无论您在哪里写入文件,您似乎都会将char
值而不是Integer
值写入文件,并将0
转换为NUL
等等。方法parseTileOntoMap()
的作用是什么?您确定没有其他进程或程序的一部分正在写入文件吗?“当我尝试使用BufferedWriter写回文件时,也会发生同样的情况。”表示是这样,这就是最有可能发现问题的地方。读取文件不会更改其内容。或者可能bufferedWriter.write((char)(值+0')
将整数转换为正确的数字charo,或者bufferedWriter.write((char)(值+0')
将整数转换为正确的数字字符