2013-05-03 66 views
0

嗨文本文件中指定行,我需要從文本文件中讀取3號線例如我需要閱讀從Android中

我寫了這個代碼,但是,它的讀取第一行:

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

試試這個:

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"); 
+0

謝謝你的工作 – user2324084 2013-05-03 14:09:34