我有一个文本文件,例如:file.txt,我想读一行,例如第7行,有没有办法直接读取第7行而不读取其他行?我想从这项工作中节省内存。
答案 0 :(得分:0)
由于JME被削减的方式,你无法做到这一点。你必须阅读整个文件。只有其他方式,但可能不太合适的是读取文件,将其存储在每行的RecordStore新条目中,但是它真的值得......
答案 1 :(得分:0)
我认为有可能你需要使用一个可能导致更多堆使用的哈希表。
无论如何,首先,文本文件的内容应该存储在char数组中。然后,第二,必须将char数组的内容移动到哈希表。
文本文件中的每一行都用新行分隔。在char数组中,新行(可能)被翻译为' \ n'。连接数组中的字符,直到到达新行字符。连接的字符(减去' \ n')将在第一行中形成字符串。此处还应该有一个计数器应该初始化为0(或1,无论你喜欢什么)。将文本保存到哈希表;该值将是已创建的字符串,密钥将是计数器。之后递增计数器。对数组的其余部分重复此过程,直到到达文件末尾。
使用散列表,您现在可以在第7行获取字符串,而无需通过其他行。好吧,基本上,每行都已读过一次。但是,至少,一旦它们存储在哈希表中,你就不必遍历每一行。
就像我之前所说的那样,这样做可能会增加堆使用量,尤其是在文本文件非常大的情况下。
[顺便说一下,很抱歉很晚才回复。这是我第一次来这里(我的意思是我刚刚注册并回答了这个问题):D]
答案 2 :(得分: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);
}
方法I: 映射没有累积字符的行号
要考虑的要点:
方法II: 读取每一行直到读取第N行
要考虑的要点:
答案 3 :(得分: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();
...
希望这会有所帮助。