Java me 从j2me中的txt文件中读取特殊行
我有一个文本文件,例如:file.txt,我想读一行,例如第7行,有没有办法直接读第7行而不读其他行?我想从这项工作中节省内存。因为JME被削减的方式,你不能这样做。您必须阅读整个文件。另一种可能不太合适的方法是读取文件,每行存储在RecordStore new entry中,但它真的值得吗…我认为这是可能的,但是您需要使用哈希表,这可能会导致更多的堆使用 无论如何,首先,文本文件的内容应该存储在char数组中。其次,必须将char数组的内容移动到哈希表中 文本文件中的每一行都用新行分隔。在char数组中,新行可能被转换为“\n”。连接数组中的字符,直到到达新行字符。连在一起的字符减去“\n”将构成第一行中的字符串。这里还应该有一个计数器,它应该被初始化为0或1,无论您喜欢什么。将文本保存到哈希表中;值将是已创建的字符串,键将是计数器。随后递增计数器。对阵列的其余部分重复此过程,直到到达文件末尾 使用哈希表,您现在可以在第7行获取字符串,而无需遍历其他行。基本上,每一行都读过一次。但是,至少,在将每一行存储在哈希表中之后,您不必遍历它们 正如我前面所说的,这样做可能会增加堆的使用率,特别是在文本文件非常大的情况下 [顺便说一句,很抱歉回复太晚。这是我第一次来这里,我的意思是我刚刚注册并回答了这个问题:D]通用代码 方法一:映射没有累积字符的行号 通过一个代码运行该文件,该代码将行号与该行中的最后一个字符从文件的第0个位置映射为每行的跳过值all+2'\r\n\。可以将此映射表存储在同一文件的开头或结尾。 使用ReadLineWithSkipnStream方法运行上述通用代码, skipCharacters;仅对其他方法调用进行明智的注释。 需要考虑的要点: 跳转到输入流中所需的位置 具有解析文件和存储映射表的开销。 方法二:读取每一行,直到读取第n行 使用readLineinStream方法运行上述通用代码, 行号;仅对其他方法调用进行明智的注释。 需要考虑的要点: 慢,因为它必须读取每个字符,直到达到所需的行 没有解析文件的开销,也没有存储映射表。Java me 从j2me中的txt文件中读取特殊行,java-me,file-processing,memory-optimization,Java Me,File Processing,Memory Optimization,我有一个文本文件,例如:file.txt,我想读一行,例如第7行,有没有办法直接读第7行而不读其他行?我想从这项工作中节省内存。因为JME被削减的方式,你不能这样做。您必须阅读整个文件。另一种可能不太合适的方法是读取文件,每行存储在RecordStore new entry中,但它真的值得吗…我认为这是可能的,但是您需要使用哈希表,这可能会导致更多的堆使用 无论如何,首先,文本文件的内容应该存储在char数组中。其次,必须将char数组的内容移动到哈希表中 文本文件中的每一行都用新行分隔。在ch
我想进一步简化读取字符的问题,而不需要任何带有行号的字符映射
...
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();
...
希望这对其他人有帮助。您可以检查字符或制表符,然后跳到下一个字符或制表符。这将加快解析到字符串的速度,占用大量内存,此方法无效您现在使用什么代码读取文件?
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);
}
...
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();
...