내 응용 프로그램이 텍스트 파일에서 임의의 줄을 읽고 반환하는 방법이 있습니다. 메신저 randTxt()를 사용하여 txt 파일에서 임의의 줄을 읽고 반환합니다. 하지만 매번 같은 줄 (첫 번째 줄) 만 보여줍니다.InputStreamReader 및 .txt 파일에서 임의의 행을 읽는 중
public String randTxt(){
// Read in the file into a list of strings
InputStreamReader inputStream = new InputStreamReader(getResources().openRawResource(R.raw.randomstuff));
//ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
String theLine="";
int i;
try {
i = inputStream.read();
while (i != -1) {
i = inputStream.read();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
LineNumberReader rdr = new LineNumberReader(inputStream);
int numLines = 30;
Random r = new Random();
rdr.setLineNumber(r.nextInt(numLines));
try {
theLine = rdr.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return theLine;
}
어떻게 해결할 수 있습니까? 누군가 내 코드에서 무엇이 잘못되었는지 설명 할 수 있습니까?
디버거에서 코드를 밟았을 때 무엇을 배웠습니까? –