Try
Dim sr As New IO.StreamReader(Mapfile & ".txt")
'Dim intValue As String = ""
Dim strLine As String = ""
Dim X As Integer = 0
Dim Y As Integer = 0
Do Until sr.EndOfStream
strLine = sr.ReadLine
strLine = strLine.Replace(strLine.LastIndexOf(","), "")
For Each item As String In Split(strLine, ",", -1)
'MsgBox("X:" & X & " Y:" & Y & "= " & item)
If item = "" Then
item = 0
End If
If X <= MapWidth Then
Map(X, Y, 0) = Int(item)
End If
X = X + 1
Next
X = 0
Y = Y + 1
Loop
sr.Close()
sr.Dispose()
Catch ex As Exception
MsgBox("Map: " & Mapfile & " could not be loaded." & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.Critical, "ERROR")
IsOn = False
End Try尝试将此代码从Visual Basic转移到Java。我试过使用Buffered Reader,但似乎没有做到这一点。上面的代码是针对Visual Basic的,下面的代码是我的java端口,看起来似乎没有相同的工作。 http://pastebin.com/freXYTi3
public void readFile(Context c) {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(c.getAssets().open("map1.txt")));
String line = null;
String newLine = "";
int x = 0;
int y = 0;
while ((line = br.readLine()) != null) {
int length = line.length();
String lastChar = line.substring(length-1);
if (lastChar.contains(",")) {
newLine = line.substring(0,length-1) + "";
}
//line = line.substring(0, line.lastIndexOf(",")) + "";
for (String str : line.split(",", -1)) {
System.out.println(str);
if(str == ""){
str = "0";
}
if(x <= mapwidth){
System.out.println(x + " " + y);
int N = Integer.parseInt(str);
Map[x][y] = N;
}
x = x + 1;
}
x = 0;
y = y + 1;
}
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
finally {
try {
if (br != null)
br.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}