我有一個日誌文本文件,由若干個標籤,如下列:解析文本文件標記爲XML - Java的
<log level="info" id="12144" ....> <msg> test msg </msg> </log>
我要分析此文件,因此我認爲,因爲它看起來像XML我可以將xml(<?xml version="1.0"?
>)和一個根(<file>
)標記添加到文件中,然後使用DOM解析器對其進行解析。
但目前,我面臨的問題是在文件的開頭添加文本,因爲所有可用的選項都要覆蓋,或者將原始文件複製到臨時位置,然後添加目標文本適用於我,因爲我正在處理大於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();
您是否有任何建議在開始時添加文本或解析文本文件,並提取元素?
感謝,
顯示您的代碼。 – VHS