2011-04-26 20 views
0

我有一個文本文件,例如:file.txt,我想讀一行,例如第7行,有沒有什麼方法直接讀第7行而不讀其他行?我想從這項工作中節省內存。從j2me的txt文件中讀取專線

+0

你現在用什麼代碼來讀取文件? – gnat 2011-12-09 12:42:08

+0

您可以檢查一個字符(或標籤)並跳過,直到下一個字符(或標籤)。這也會加速它 – Vincent 2011-04-27 12:17:05

+0

解析成字符串需要大量的內存,這種方法效率不高 – 2011-04-27 13:44:03

回答

0

由於JME被裁減的方式,你不能這樣做。你將不得不閱讀整個文件。只有其他方式,但可能不太適合讀取文件,將其存儲在RecordStore每行的新條目中,但它是否真的值得...

0

我認爲這是可能的,但是您需要使用哈希表可能會導致更多的堆使用。

無論如何,首先,文本文件的內容應該存儲在一個char數組中。然後,第二,char數組的內容必須移動到散列表。

文本文件中的每一行用新行隔開。在char數組中,新行(可能)被翻譯爲'\ n'。連接數組中的字符,直到到達新的行字符。連接字符(減號'\ n')將形成第一行中的字符串。這裏也應該有一個計數器,它應該已經初始化爲0(或1,無論你喜歡什麼)。將文本保存到散列表;該值將是已創建的字符串,並且該鍵將是計數器。事後增加計數器。對數組的其餘部分重複此過程,直到達到文件結尾。

使用哈希表,現在可以在第7行獲取字符串,而無需通過其他行。那麼,基本上,每行都被讀過一次。但是,至少,你不需要遍歷每一行一旦被存儲在散列表中。

就像我之前說的那樣,這樣做可能會增加堆的使用,特別是如果文本文件非常大。

[順便說一句,對於遲到的回答感到抱歉。這是我第一次來這裏(我的意思是我剛剛註冊,並回答了這個問題):d]

0

共同守則

private String readLine(InputStream _inStream, int lineNum) 
      throws IOException { 
    if (null == _inStream) { 
     throw new IOException("Inputstream null."); 
    } 
    if (lineNum < 0) { 
     return ("Cannot read line a number " + lineNum); 
    } 

    final StringBuffer buf = new StringBuffer(); 
    byte c; 

    int curLine = 1; 
    while (((c = (byte) _inStream.read()) != -1)) { 
     //System.out.println((char)c); 
     if (c == '\n') { 
      ++curLine; 
      if (curLine > lineNum) { 
       break; 
      } else if (curLine < lineNum) { 
       continue; 
      } 
     } else if (curLine != lineNum) { 
      continue; 
     } 
     buf.append((char) c); 
    } 

    if (0 == buf.length()) { 
     return null; 
    } else { 
     return buf.toString().trim(); 
    } 
} 

private String readLineWithSkip(InputStream _inStream, long skipCharacters) 
      throws IOException { 
    if (null == _inStream) { 
     throw new IOException("Inputstream null."); 
    } 
    if (skipCharacters < 1) { 
     return ("Cannot skip stream of " + skipCharacters + " characters"); 
    } 

    final StringBuffer buf = new StringBuffer(); 
    byte c; 

    _inStream.skip(skipCharacters); 
    while ((c = (byte) _inStream.read()) != '\n') { 
     //System.out.println((char)c); 
     buf.append((char) c); 
    } 

    if (0 == buf.length()) { 
     return null; 
    } else { 
     return buf.toString().trim(); 
    } 
} 

InputStream inStream = null; 
int fileLength = 39; 
int skipCharacters = 10; 
int lineNumber = 3; 
String myLine = "No line read."; 
try { 
    inStream = Class.class.getResourceAsStream("/test.txt"); 
    if (null != inStream) { 
     inStream.mark(fileLength); 

    //For Approach II 
     myLine = readLine(inStream, lineNumber); 

     inStream.reset(); 
    //For Approach I 
     myLine = readLineWithSkip(inStream, skipCharacters); 
    } 
} catch (SecurityException se) { 
    se.printStackTrace(); 

} catch (IOException ioe) { 
    ioe.printStackTrace(); 

} finally { 
    try { 
     inStream.close(); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } catch (NullPointerException e) { 
     e.printStackTrace(); 
    } 
    inStream = null; 

    System.out.println(myLine); 
} 

方法一:地圖的行號,沒有累積字符

  1. 運行通過代碼的文件,與過去系統字符的行數的0號位置的地圖,該行該文件(用作skip()值)全部爲+2('\ r \ n \')。您可以將此映射表存儲在同一文件的開始或結尾處。
  2. 使用方法readLineWithSkip(inStream, skipCharacters)運行上面的通用代碼;只有明智地評論其他方法調用。

考慮要點:

  1. 跳至所期望的位置中InputStream的
  2. 具有解析文件,並存儲映射表的開銷。

方法II:讀每一行直到第N行被讀

  1. 的運行與方法的readLine(插播廣告, LINENUMBER)上述共同的代碼;只有明智地評論其他方法調用。

要考慮的問題:

  1. 慢,因爲它,直到它達到所需的行
  2. 解析文件,並沒有映射表中存儲的任何開銷進行讀取每個字符。
+0

我的回答有用嗎? – Vimal 2011-12-12 22:31:52

0

我想進一步簡化閱讀字符的問題,而不需要任何字符與行號的映射。

... 
        Form form=new Form("filename"); 
        InputStream fin=fconn.openDataInputStream(); 
        StringBuffer buf =new StringBuffer(); 
        int c; 
        int counter=-1; 
        while((c=fin.read())!=-1) 
        { 
         counter++; 
         if(counter==23) 
         { 
          form.append(buf.toString()); 
          buf=null;counter=0; 
          continue; 
         } 

         buf.append((char)c); 
        } 
        if(counter<23 && counter>=0) // write the skipped chars between the last number read and the end of file 
        { 
        form.append(buf.toString()); 
        buf=null;counter=0; 
        } 
        fin.close(); 
        ... 

希望這可以幫助別人。