0

嗨,例如,我需要从文本文件中读取第 3 行

我已经编写了这段代码,但是它读取了第一行:

public void GetFrom_Text(){

String []message=null;

BufferedReader br=null;
String name1, faculty1, deparment1, officeNumber1, officeHour1, email1, phone1;
long e;
try{
    String sCurrentLine = null;
    String next=" ";
    br =new BufferedReader(new InputStreamReader(getAssets().open("ab.txt")));
    sCurrentLine=br.readLine();
    if(sCurrentLine!=null )
      message=sCurrentLine.split(",");

    name1=message[0];
    faculty1=message[1];
    deparment1=message[2];
    officeNumber1=message[3];
    phone1=message[4];
    email1=message[5];
     e=db2.insertRecord(faculty1,deparment1 ,name1 , officeNumber1,phone1,email1,"11-12"); 
}
catch (IOException e1){
    e1.printStackTrace();
}finally{
    try {
        if (br !=null)br.close();

    }catch (IOException ex){
        ex.printStackTrace();
    }
}
4

1 回答 1

1

尝试这个:

String sCurrentLine = null;
String next = " ";
br = new BufferedReader(new InputStreamReader(getAssets().open("ab.txt")));

int lineNumber = 0;

while((next = br.readLine()) != null){
    lineNumber++;
    if(lineNumber == 3){
        sCurrentLine = next;
        break;
    }
}

if (sCurrentLine != null) {
    message = sCurrentLine.split(",");
}

name1 = message[0];
faculty1 = message[1];
deparment1 = message[2];
officeNumber1 = message[3];
phone1 = message[4];
email1 = message[5];
e = db2.insertRecord(faculty1, deparment1, name1, officeNumber1, phone1, email1, "11-12");
于 2013-05-03T13:42:51.250 回答