我在使用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
.......
......
......
这条线的错误&gt;&gt; L = buffer.readLine(); 我试图交换顺序:在第一种方法之前使用第二种方法。第二种可以使用,但第一种方法是错误。 我认为这是关于Inputstream的问题,所以我尝试制作两个Inputstream,但它并不是更好。
答案 0 :(得分:1)
您似乎已经在LayoutDimensions方法中关闭了输入流。
此
reader1.close();