public class GameMap{byte[] terrain;OK,自己解决了。 public void loadTerrian(String filename){ terrain = new byte[20][20]; try{ FileInputStream fis = new FileInputStream(new File(filename)); for(int i = 0; i < 20; i ++){ fis.read(terrain[i], 0, 20); } }catch(FileNotFoundException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } }
先把数据都读到[400]的数组terrain里
然后赋值给二维数组:byte a[20][20];
for(int i=0;i<20;++i){
for(int j=0;j<20;++j){
a[i][j] = terrain[i*20+j];
}
}
public void loadTerrian(String filename){
terrain = new byte[20][20];
try{
FileInputStream fis = new FileInputStream(new File(filename));
for(int i = 0; i < 20; i ++){
fis.read(terrain[i], 0, 20);
}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}