2011-11-28 7 views
1

안드로이드에서 텍스트 파일을 읽습니다. 나는 33 줄 이하의 더 작거나 더 작은 텍스트 파일을 가지고 있습니다. 큰 경우 33 행은 마지막 33 행을 읽습니다. 33 줄보다 작거나 같으면 모든 줄을 읽습니다. 가장 빠른 방법 것은 내가안드로이드에서 안드로이드는

MAXLINECOUNT=33; 
File f = new File("file.......");     
if (f.exists()) 
{ 
     String readString = new String(); 
    int i,l; 
    i=0; 
    l=0; 
    LineNumberReader reader = new LineNumberReader(new FileReader(new File("/mnt/sdcard/data.txt"))); 
    while ((readString = reader.readLine()) != null) {} 
    l= reader.getLineNumber(); 
    if (l>=MAXLINECOUNT) l=l-(MAXLINECOUNT-3);// i realy need only 30 
    FileInputStream fileIS = new FileInputStream(f); 
    BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS)); 
    while((readString = buf.readLine())!= null){ 
    if (i>l) 
     { 

       process hear ............... 
      } 
    i++; 
+0

희망 http://stackoverflow.com/questions/2312756/in-java-how-to-read-from-a-file-a-specific-line-given-the-line- number – vnshetty

+0

Android에 FileUtils이 없습니다. 찾을 수 없습니다. 텍스트 파일을 문자열로 전송하고 분석 할 수 있습니까? –

+0

아무 것도 안드로이드 프로젝트에서 commons-fileutils를 사용할 수 없습니다 (크기는 예외) –

답변

0

내가 시간에 라인을 읽기, 스트림을 통해하는 java.io.Reader를 사용하여 계속됩니다 파일을 읽을 수있는 가장 빠른 방법을 생각 가지고 어떤이는 감사에게 그것을

확인을하고, 에 의해 사전 배치 지수 (귀하의 경우 33에서) 원하는 크기의 미리 할당 된 배열에 배치 :

(idx++ % 33) 

이런 식으로 배열하면 최대 33 개 마지막 라인을 개최한다. 이 질문의 대답은 당신을 도울 것입니다