我有一些关于使用 readline() 的问题。我有一个 inputStream 它来自主类
private String url = "BoardLayoutSet/01_basic.templete";
private InputStream boardName = getResources().getAssets().open(url);
Board b = new Board(boardName);
我将这个“boardName”是 InputStream 发送到“Board”类。在这个 Board.class 中有如下代码的构造函数
public Board(InputStream boardName) throws IOException{
int[] d = LayoutDimensions(boardName); //<----First,use InputStream
....
......
build(null, boardName); //<<--Second,Use InputStream
}
第一个方法由这个构造函数调用(上图)。第一种方法它像这样使用“InputStream”(下)
public int[] LayoutDimensions(InputStream boardName) throws IOException {
BufferedReader reader1 = new BufferedReader(new InputStreamReader(boardName));
L1 = reader1.readLine(); // #Basic
L1 = reader1.readLine(); // %Level 1
L1 = reader1.readLine(); // space line
L1 = reader1.readLine(); // First row of board
for (int i = 0; i < L1.length(); i++) {
if (L1.charAt(i) != ' ') {
x++;
}
}
System.out.println(L1);
System.out.println("Width(x):" + x);
// ////////////////
boardName.reset();
BufferedReader reader2 = new BufferedReader(new InputStreamReader(boardName));
L2 = reader2.readLine(); // #Basic
L2 = reader2.readLine(); // %Level 1
L2 = reader2.readLine(); // space line
L2 = reader2.readLine(); // First row of board
while (L2.length()!=0) {
System.out.println(L2.charAt(0));
y++;
L2 = reader2.readLine();
}
System.out.println("Height(y):" + y);
.....
......
reader1.close();
reader2.close();
reader3.close();
return dimensions;
}
当我只使用一种方法时,它可以工作。接下来,我需要像这样调用第二种方法
protected void build(Random r1, InputStream boardName) throws IOException {
if (r1==null) {
long seed = new Random().nextLong();
r = new Random(seed);
} else {
r = r1;
}
tTile = new Tile[depth][height][width];
int x;
int y;
int z=-1;
BufferedReader buffer = new BufferedReader(new InputStreamReader(boardName));
String L=null;
buffer.reset();
L = buffer.readLine(); //Basic
.......
......
......
这是错误,因为这一行>> L=buffer.readLine(); 我试图交换命令:在拳法之前使用第二种方法。第二种可以使用,但第一种方法是错误的。我认为这是关于 Inputstream 的问题,所以我尝试制作两个 Inputstream,但效果并不好。