我需要从android中的文本文件中读取特定行

时间:2013-05-03 13:15:57

标签: android text-files readline

您好我需要从文本文件中读取第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();
    }
}

1 个答案:

答案 0 :(得分: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");