我需要处理一个大文件,大约400K行和200M.但有时我必须自下而上处理.我怎样才能在这里使用迭代器(yield return)?基本上我不喜欢在内存中加载所有内容.我知道在.NET中使用迭代器更有效.
我想读取一个非常大的文件的最后n行,而不是使用Java将整个文件读入任何缓冲区/内存区域.
我查看了JDK API和Apache Commons I/O,但无法找到适合此目的的API.
我在想UNIX中的尾部或更少的方式.我不认为他们加载整个文件,然后显示该文件的最后几行.在Java中也应该有类似的方法来做同样的事情.
我有一个java ee应用程序,我使用servlet打印用log4j创建的日志文件.在读取日志文件时,通常会查找最后一个日志行,因此如果以相反的顺序打印日志文件,则servlet会更有用.我的实际代码是:
response.setContentType("text");
PrintWriter out = response.getWriter();
try {
FileReader logReader = new FileReader("logfile.log");
try {
BufferedReader buffer = new BufferedReader(logReader);
for (String line = buffer.readLine(); line != null; line = buffer.readLine()) {
out.println(line);
}
} finally {
logReader.close();
}
} finally {
out.close();
}
Run Code Online (Sandbox Code Playgroud)
我在互联网上找到的实现涉及使用StringBuffer并在打印之前加载所有文件,是不是有代码轻量级方式寻找到文件的末尾并读取内容直到文件的开头?
我想从结束到开始我的文件的相反方向读取文件,
[1322110800] LOG ROTATION: DAILY
[1322110800] LOG VERSION: 2.0
[1322110800] CURRENT HOST STATE:arsalan.hussain;DOWN;HARD;1;CRITICAL - Host Unreachable (192.168.1.107)
[1322110800] CURRENT HOST STATE: localhost;UP;HARD;1;PING OK - Packet loss = 0%, RTA = 0.06 ms
[1322110800] CURRENT HOST STATE: musewerx-72c7b0;UP;HARD;1;PING OK - Packet loss = 0%, RTA = 0.27 ms
Run Code Online (Sandbox Code Playgroud)
我用代码以这种方式阅读它,
String strpath="/var/nagios.log";
FileReader fr = new FileReader(strpath);
BufferedReader br = new BufferedReader(fr);
String ch;
int time=0;
String Conversion="";
do {
ch = br.readLine();
out.print(ch+"<br/>");
} while (ch != null);
fr.close();
Run Code Online (Sandbox Code Playgroud)
我更喜欢使用缓冲读取器以相反的顺序读取
我正在创建一个日志,我想读取log.txt文件的最后一行,但是一旦读完最后一行,我就无法让BufferedReader停止.
这是我的代码:
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("C:\\testing.txt"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud) 当我想用Java获取纯文本文件的最后一行时,我很困惑.我怎样才能做到这一点?哪个是不使用扫描仪的最佳方式?
谢谢
我试图获取文件的最后一行,但我的输出显示它从未找到它.我也尝试寻找所有行开头的"[",但除非跳转完美,否则程序不会跳过"[".我试图寻找 "\ r \n",System.getProperty( "line.separator"),\ r和\n.最可能是它的一个愚蠢的错误,但如果我拥有它而我找不到它,那么其他人也可能遇到它.
RandomAccessFile raf = new RandomAccessFile(fileLocation, "r");
//Finds the end of the file, and takes a little more
long lastSegment = raf.length() - 5;
//**Looks for the new line character \n?**
//if it cant find it then take 5 more and look again.
while(stringBuffer.lastIndexOf("\n") == -1) {
lastSegment = lastSegment-5;
raf.seek(lastSegment);
//Not sure this is the best way to do it
String seen = raf.readLine();
stringBuffer.append(seen);
}
//Trying to debug it and understand
int location …Run Code Online (Sandbox Code Playgroud) 我有一个文本文件,包含几个条目,如:
hello
there
my
name
is
JoeBloggs
Run Code Online (Sandbox Code Playgroud)
我如何按降序读取最后五个条目,即来自JoeBloggs-那里
我目前只有代码来阅读最后一行:
public class TestLastLineRead {
public static void main(String[] args) throws Exception {
FileInputStream in = new FileInputStream(file.txt);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine = null, tmp;
while ((tmp = br.readLine()) != null) {
strLine = tmp;
}
String lastLine = strLine;
System.out.println(lastLine);
in.close();
}
}
Run Code Online (Sandbox Code Playgroud) 我创建了一个简单的方法来将文本追加到文件中:
void writeFile(String fileName, String... values) {
File file = new File(fileName);
file.getParentFile().mkdirs();
try(BufferedWriter bw = new BufferedWriter(new FileWriter(file, true))) {
for (String value: values) {
bw.write(value);
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我在编写文件之前如何检查文件末尾是否存在新行以确保正确存储时,我感到很遗憾.
此外,在处理较大的文件时,它必须是可扩展的.