我需要從1 .txt文件中檢索兩行並將它們輸出到對話框。我以現在的代碼是閱讀特定行 - Java
private String getfirstItem() {
String info = "";
File details = new File(myFile);
if(!details.exists()){
try {
details.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
BufferedReader read = null;
try {
read = new BufferedReader (new FileReader(myFile));
} catch (FileNotFoundException e3) {
e3.printStackTrace();
}
for (int i = baseStartLine; i < baseStartLine + 1; i++) {
try {
info = read.readLine();
} catch (IOException e) {
e.printStackTrace();
}
}
firstItem = info;
try {
read.close();
} catch (IOException e3) {
e3.printStackTrace();
}
return firstItem;
}
private String getsecondItem() {
File details = new File(myFile);
String info = "";
BufferedReader reader = null;
if(!details.exists()){
try {
details.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}}
try {
reader = new BufferedReader (new FileReader(myFile));
} catch (FileNotFoundException e3) {
e3.printStackTrace();
}
for (int i = modelStartLine; i < modelStartLine + 1; i++) {
try {
info= reader.readLine();
} catch (IOException e) {
e.printStackTrace();}
modelName = info;} try {
reader.close();
} catch (IOException e3) {
e3.printStackTrace();
}
return secondItem;
}
不過,我不斷收到兩個相同的值? modelStartLine = 1 and baseStartLine = 2
非常感謝! – RayCharles