【发布时间】:2017-03-09 18:31:29
【问题描述】:
我有一个日志文本文件,其中包含如下几个标签:
<log level="info" id="12144" ....> <msg> test msg </msg> </log>
我想解析这个文件,因此我认为既然它看起来像 XML,我可以将 xml (<?xml version="1.0"?>) 和根 (<file>) 标记添加到文件中,然后使用 DOM Parser 解析它。
但目前,我面临在文件开头添加文本的问题,因为所有可用选项都是覆盖,或将原始文件复制到临时位置,然后添加预期的文本,这是不适用的对我来说,因为我正在处理大于 100 MB 的文件。
I use the below code to try to add the text at the beginning, and I tried several solutions or classes other than RandomAccessFile
RandomAccessFile RAF = new RandomAccessFile(file, "rw");
RAF.getChannel().position(0);
RAF.write(("<?xml version=\"1.0\" encoding=\"UTF-8\"?> \r\n <file> \r\n").getBytes());
RAF.close();
您对在开头添加文本或按原样解析文本文件并提取元素有什么建议吗?
谢谢,
【问题讨论】:
-
显示您的代码。