由于某种原因,当使用我的缓冲读取器读取.mcf(地图配置文件)时,它将无法正确找到字符。这就是我用于测试的目的。
RendererPanel.java:
File file = new File("src/net/PlatformPeril/resources/Railroad.mcf");
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String line;
try {
while ((line = br.readLine()) != null) {
if(line.equalsIgnoreCase("0")){
System.out.println("Air Block!");
}else if(line.equalsIgnoreCase("1")){
System.out.println("Rock Block!");
}else if(line.equalsIgnoreCase(" ")){
System.out.println("Empty Line Char; Ignoring");
continue;
}else System.out.println("What!");
}
} catch (IOException e) {
e.printStackTrace();
}
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
MCF:
1 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
0 1 1 0 1 0 1 1 1 1 0 0 1 0 0 0 1 1 0 1 0 1 0 1 1 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
而且,我得到了很多'什么!'.... 我知道我做错了什么?
答案 0 :(得分:1)
当你比较第一个令牌时,你正在比较整行,看看string.split来帮助
答案 1 :(得分:0)
此line.equalsIgnoreCase(" "
)可能会导致问题,请尝试使用line.trim().isEmpty()
答案 2 :(得分:0)
让文件包含以下内容 -
0
1
0
1
考虑一个空行
1
以下程序运行正常,与您的相同,但有一些改进。
package com.kvvssut.misc;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class BufferReaderTester {
public static void main(String[] args) {
processFile("C:\\Users\\srimanta_sahu\\Desktop\\MyText.mcf");
}
private static void processFile(String string) {
BufferedReader bufferedReader = null;
FileReader fileReader = null;
try {
fileReader = new FileReader(new File(string));
bufferedReader = new BufferedReader(fileReader);
String currentLine;
while ((currentLine = bufferedReader.readLine()) != null) {
currentLine = currentLine.trim();
if(currentLine.equals("0")){
System.out.println("Air Block!");
} else if (currentLine.equals("1")){
System.out.println("Rock Block!");
} else if(currentLine.equals("")){
System.out.println("Empty Line Char; Ignoring");
}else {
System.out.println("What!");
}
}
} catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
} catch (IOException ioException) {
ioException.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
fileReader.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
}
}
Plz不要使用不必要的equalsIgnoreCase来检查整数,如果它不需要做任何事情就不要使用continue!