简体   繁体   中英

Reading through a File with a Buffered Reader

Background

I am writing a xml converter that takes input from textfiles and translates them to xml. In the text file each record is represented by a line and each field is represented with a tab between them. So in the text file two records would look like:

fieldA     fieldB     fieldC
fieldA     fieldB     fieldC

Problem

I am loading the text file into a bufferedReader and using the StAX implementation WoodStox to create the XML. I can see that I am getting the correct record data from my getColumnValue method. But for some reason WoodStox is writing the first record over and over, rather than taking the data that is being delivered each time through the while loop. Since I know inputs (from getColumnValue) are coming in correct, I can only conclude that the problem lies with Woodstock but so far I haven't been able to understand why...

Code:

while ((strRead = buffer.readLine()) != null) {

    String recordInputs[] = strRead.split("\t");
    writer.writeStartElement("Record");

    writer.writeStartElement("FIELDA");
    writer.writeCharacters(getColumnValue("BSTYPE", tableColumns, recordInputs));
    writer.writeEndElement();

    writer.writeStartElement("FIELDB");
    writer.writeCharacters(getColumnValue("BSDDT", tableColumns, recordInputs));
    writer.writeEndElement();

    writer.writeStartElement("FIELDC");
    writer.writeCharacters(getColumnValue("BSACTIVE", tableColumns, recordInputs));
    writer.writeEndElement();

    writer.writeEndElement();
}

writer.writeEndElement();
writer.writeEndDocument();

writer.flush();
writer.close();

Since you didn't provide a Minimal, Complete, and Verifiable example , here it is:

String input = "1\t2\t3\r\n" +
               "4\t5\t6\r\n";
XMLStreamWriter writer = XMLOutputFactory.newFactory().createXMLStreamWriter(System.out);
System.out.println(writer.getClass());
writer.writeStartDocument();
writer.writeStartElement("Root");
try (BufferedReader in = new BufferedReader(new StringReader(input))) {
    for (String strRead; (strRead = in.readLine()) != null; ) {
        String recordInputs[] = strRead.split("\t");
        writer.writeStartElement("Record");

        writer.writeStartElement("FIELDA");
        writer.writeCharacters(recordInputs[0]);
        writer.writeEndElement();

        writer.writeStartElement("FIELDB");
        writer.writeCharacters(recordInputs[1]);
        writer.writeEndElement();

        writer.writeStartElement("FIELDC");
        writer.writeCharacters(recordInputs[2]);
        writer.writeEndElement();

        writer.writeEndElement();
    }
}
writer.writeEndElement();
writer.writeEndDocument();
writer.flush(); // Don't close System.out

When run on Java 8 without Woodstox, I get (formatted for readability):

class com.sun.xml.internal.stream.writers.XMLStreamWriterImpl

<?xml version="1.0" ?>
<Root>
    <Record><FIELDA>1</FIELDA><FIELDB>2</FIELDB><FIELDC>3</FIELDC></Record>
    <Record><FIELDA>4</FIELDA><FIELDB>5</FIELDB><FIELDC>6</FIELDC></Record>
</Root>

As you can see, it's using the built-in Java implementation, and values are good.

When run with woodstox-core-asl-4.2.0.jar and stax2-api-3.1.1.jar on the classpath, I get (formatted for readability):

class com.ctc.wstx.sw.SimpleNsStreamWriter

<?xml version='1.0' encoding='UTF-8'?>
<Root>
    <Record><FIELDA>1</FIELDA><FIELDB>2</FIELDB><FIELDC>3</FIELDC></Record>
    <Record><FIELDA>4</FIELDA><FIELDB>5</FIELDB><FIELDC>6</FIELDC></Record>
</Root>

As you can see, it's using the Woodstox implementation, and values are still good.

Since you didn't include the logic of getColumnValue() , or a verifiable example, or the version of Woodstox you're using, there nothing else we can do to help you.

I never found a good answer to why this didn't work but I did manage to fix it.

Non Working:

writer.writeCharacters(getColumnValue("BSTYPE", tableColumns, customer));

Working:

String bsType = getColumnValue("BSTYPE", tableColumns, customer);
writer.writeCharacters(csType);

Like I previously said getColumnValue() returns a string so I have no idea why that change fixes the problem, but it does.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM