Concat堆栈从文本文件的线在一条线

时间:2015-06-22 13:38:32

标签: java eclipse string file-io tags

这是示例文本文件内容

<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>标记

2 个答案:

答案 0 :(得分:1)

我能想象的最简单的方法是逐行读取文件并在找到结束标记后输出一个新行。根据您的描述,此实用程序方法甚至不需要知道该文件是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();

答案 1 :(得分:0)

这是一个Folded类,可根据需要折叠String个序列。

private static class Folded implements Iterator<String> {

    // The Iterator I am folding.
    final Iterator<String> it;
    // What the start of a fold looks like.
    final String start;
    // What the end of a fold should look like.
    final String end;

    private Folded(Iterator<String> it, String start, String end) {
        this.it = it;
        this.start = start;
        this.end = end;
    }

    @Override
    public boolean hasNext() {
        return it.hasNext();
    }

    @Override
    public String next() {
        // Get the next String.
        StringBuilder n = new StringBuilder(it.next());
        // If it's a "start"
        if (n.toString().equals(start)) {
            // Add ...
            String add = it.next();
            do {
                // ... next ...
                n.append(add);
                // Until end seen.
            } while (it.hasNext() && !(add = it.next()).equals(end));
            // Last
            n.append(add);
        }
        return n.toString();
    }
}

public void test() throws FileNotFoundException {
    String test = "<TRAN>\n"
            + "<LCL_STRT_TMST>2012-05-01T16:06:30.033</LCL_STRT_TMST>\n"
            + "<LCL_END_TMST>2012-05-01T16:06:30.033</LCL_END_TMST>\n"
            + "<TME_ZONE>-3</TME_ZONE>\n"
            + "<APPL>80</APPL>\n"
            + "<TRAN_TYPE>Exception in ECOMP request</TRAN_TYPE>\n"
            + "<TRAN_ID>20120501160624502879807</TRAN_ID>\n"
            + "<TRAN_PRIT>1</TRAN_PRIT>\n"
            + "<DUNS_NBR>142307417:1 US</DUNS_NBR>\n"
            + "<PRCS_STAT>500</PRCS_STAT>\n"
            + "<PRCS_MSG>Transaction 20120501160624502879807 caused GFServerException while writing SOAP response:  - Unexpected exception caught (java.lang.NullPointerException)</PRCS_MSG>\n"
            + "</TRAN>\n"
            + "<TRAN>\n"
            + "<LCL_STRT_TMST>2012-05-01T16:06:37.283</LCL_STRT_TMST>\n"
            + "<LCL_END_TMST>2012-05-01T16:06:37.283</LCL_END_TMST>\n"
            + "<TME_ZONE>-3</TME_ZONE>\n"
            + "<APPL>80</APPL>\n"
            + "<TRAN_TYPE>Exception in EBIR request</TRAN_TYPE>\n"
            + "<TRAN_ID>20120501160636283855855</TRAN_ID>\n"
            + "<TRAN_PRIT>1</TRAN_PRIT>\n"
            + "<DUNS_NBR>142307417:1 US</DUNS_NBR>\n"
            + "<PRCS_STAT>500</PRCS_STAT>\n"
            + "<PRCS_MSG>Transaction 20120501160636283855855 caused GFServerException while writing SOAP response:  - Unexpected exception caught (java.lang.NullPointerException)</PRCS_MSG>\n"
            + "</TRAN>";
    // Make it into a list.
    ArrayList<String> list = new ArrayList<String>(Arrays.asList(test.split("\n")));
    Folded f = new Folded(list.iterator(), "<TRAN>", "</TRAN>");
    for (String folded : Iterables.in(f)) {
        System.out.println(folded);
    }
}

您可以像这样Iterator<String>制作File

Iterator<String> lines = new BufferedReader(new FileReader("test.xml"))
  .lines()
  .iterator();

请注意,我的Iterables.in会创建一个Iterable来迭代Iterator - 我在这里只是为了演示而使用它。