使用java.util.logging.Logger时如何读取文本文件中的日志
我想从MyLogFile.log中读取使用java.util.logging.Logger时如何读取文本文件中的日志,java,json,web-services,logging,Java,Json,Web Services,Logging,我想从MyLogFile.log中读取字符串数据,我在D:/MyLogFile.log中写入了这些数据 我想从文件中读取JSON数据(web服务)。有人能帮忙吗 到目前为止,我得到的是: import java.io.IOException; import java.util.logging.FileHandler; import java.util.logging.Logger; import java.util.logging.SimpleFormatter; public class L
字符串
数据,我在D:/MyLogFile.log中写入了这些数据
我想从文件中读取JSON数据(web服务)。有人能帮忙吗
到目前为止,我得到的是:
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
public class Log {
public static void main(String[] args) {
Logger logger = Logger.getLogger("MyLog");
FileHandler fh;
try {
// This block configure the logger with handler and formatter
fh = new FileHandler("D:/MyLogFile.log");
logger.addHandler(fh);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
// the following statement is used to log any messages
logger.info("My first log");
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
logger.info("Hi How r u?");
System.out.println("success");
}
}
您正在混合包:
java.util.logging
包用于日志记录,即:编写日志文件。读取文件通常在java.io
的帮助下完成(io表示输入/输出,因此您也可以使用它来编写普通文件)。您正在读取的文件是日志文件还是其他文件并不重要,只要它是可读的
因此,您可以从以下内容开始:
BufferedReader reader = new BufferedReader(new FileReader("D:/MyLogFile.log"));
String line = reader.readLine();
while (line != null) {
// do something
line = reader.readLine();
}