gpt4 book ai didi

java - 将文本文件中的行堆栈连接到一行中

转载 作者:行者123 更新时间:2023-12-01 11:23:53 24 4
gpt4 key购买 nike

这是示例文本文件内容

<TRAN>
<LCL_STRT_TMST>2012-05-01T16:06:30.033</LCL_STRT_TMST>
<LCL_END_TMST>2012-05-01T16:06:30.033</LCL_END_TMST>
<TME_ZONE>-3</TME_ZONE>
<APPL>80</APPL>
<TRAN_TYPE>Exception in ECOMP request</TRAN_TYPE>
<TRAN_ID>20120501160624502879807</TRAN_ID>
<TRAN_PRIT>1</TRAN_PRIT>
<DUNS_NBR>142307417:1 US</DUNS_NBR>
<PRCS_STAT>500</PRCS_STAT>
<PRCS_MSG>Transaction 20120501160624502879807 caused GFServerException while writing SOAP response: - Unexpected exception caught (java.lang.NullPointerException)</PRCS_MSG>
</TRAN>
<TRAN>
<LCL_STRT_TMST>2012-05-01T16:06:37.283</LCL_STRT_TMST>
<LCL_END_TMST>2012-05-01T16:06:37.283</LCL_END_TMST>
<TME_ZONE>-3</TME_ZONE>
<APPL>80</APPL>
<TRAN_TYPE>Exception in EBIR request</TRAN_TYPE>
<TRAN_ID>20120501160636283855855</TRAN_ID>
<TRAN_PRIT>1</TRAN_PRIT>
<DUNS_NBR>142307417:1 US</DUNS_NBR>
<PRCS_STAT>500</PRCS_STAT>
<PRCS_MSG>Transaction 20120501160636283855855 caused GFServerException while writing SOAP response: - Unexpected exception caught (java.lang.NullPointerException)</PRCS_MSG>
</TRAN>

我需要帮助编写一个新文件,其中 <TRAN></TRAN>写在一行中,下一个 <TRAN></TRAN>写在下一行。期望的输出是:

<TRAN><LCL_STRT_TMST>2012-05-01T16:06:30.033</LCL_STRT_TMST><LCL_END_TMST>2012-05-01T16:06:30.033</LCL_END_TMST><TME_ZONE>-3</TME_ZONE><APPL>80</APPL><TRAN_TYPE>Exception in ECOMP request</TRAN_TYPE><TRAN_ID>20120501160624502879807</TRAN_ID><TRAN_PRIT>1</TRAN_PRIT><DUNS_NBR>142307417:1 US</DUNS_NBR><PRCS_STAT>500</PRCS_STAT><PRCS_MSG>Transaction 20120501160624502879807 caused GFServerException while writing SOAP response:  - Unexpected exception caught (java.lang.NullPointerException)</PRCS_MSG></TRAN>
<TRAN><LCL_STRT_TMST>2012-05-01T16:06:37.283</LCL_STRT_TMST><LCL_END_TMST>2012-05-01T16:06:37.283</LCL_END_TMST><TME_ZONE>-3</TME_ZONE><APPL>80</APPL><TRAN_TYPE>Exception in EBIR request</TRAN_TYPE><TRAN_ID>20120501160636283855855</TRAN_ID><TRAN_PRIT>1</TRAN_PRIT><DUNS_NBR>142307417:1 US</DUNS_NBR><PRCS_STAT>500</PRCS_STAT><PRCS_MSG>Transaction 20120501160636283855855 caused GFServerException while writing SOAP response: - Unexpected exception caught (java.lang.NullPointerException)</PRCS_MSG></TRAN>

注意:该文件非常大,包含大量 <TRAN>标签

最佳答案

我能想到的最简单的方法是逐行读取文件并在找到结束标记后输出新行。根据您的描述,此实用方法甚至不需要知道该文件是 XML 还是其他任何内容。

例如下面的类

class Lines{
public static void join(String endLine, Reader reader, Writer writer) throws IOException {
String line;

BufferedReader bufferedReader = new BufferedReader(reader);
while ((line = bufferedReader.readLine()) != null) {
String tLine = line.trim();

writer.write(tLine);
if (tLine.equals(endLine)) {
writer.write(System.lineSeparator());
}
}
}
}

例如,可用于将文件过滤到系统输出中。

InputStream in = ...
OutputStreamWriter writer = new OutputStreamWriter(System.out);
Lines.join("</TRAN>", new InputStreamReader(in), writer);
writer.flush();

关于java - 将文本文件中的行堆栈连接到一行中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30981417/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com