Stenway Developer Network

WSV in Java

Prerequisites

The source code of the Stenway WSV reference library can be found on GitHub

Examples

Create a WSV line

WsvLine line = new WsvLine("Value1", "Value2", "Value3");

Serialize a WSV line

WsvLine line = new WsvLine("Value1", "Value2", "Value3");

String str = line.toString();

Create a WSV document

WsvDocument document = new WsvDocument();
document.addLine("Row1_Value1", "Row1_Value2", "Row1_Value3");
document.addLine("Row2_Value1", "Row2_Value2");

Serialize a WSV document

WsvDocument document = new WsvDocument();
document.addLine("Row1_Value1", "Row1_Value2", "Row1_Value3");
document.addLine("Row2_Value1", "Row2_Value2");

String str = document.toString();

Save as WSV file

WsvDocument document = new WsvDocument();
document.addLine("Row1_Value1", "Row1_Value2", "Row1_Value3");
document.addLine("Row2_Value1", "Row2_Value2");

document.save("Data.wsv");

Specify encoding

WsvDocument document = new WsvDocument();
document.addLine("Row1_Value1", "Row1_Value2", "Row1_Value3");
document.addLine("Row2_Value1", "Row2_Value2");

document.setEncoding(ReliableTxtEncoding.UTF_16);
document.save("Data.wsv");

Load a WSV file

WsvDocument document = WsvDocument.load("Data.wsv");

Parse a WSV document

WsvDocument document = WsvDocument.parse("Value11 Value12\nValue21");

Parse a WSV line

WsvLine line = WsvLine.parse("Value11 Value12 #Comment");

Handle invalid documents

try {
    var document = WsvDocument.load("Data.wsv");
} catch (ReliableTxtException e) {
    ...
} catch (WsvParserException e) {
    ...
}

Change content

WsvDocument document = WsvDocument.load("Table.wsv");

document.getLine(0).setComment(" Export date: 2021-01-31");

document.getLine(3).Values[3] = "Phoenix";

document.addLine(WsvLine.parse("Ethan       Walker      35"));

Streaming

Read a WSV file line by line

try (WsvStreamReader reader = 
        new WsvStreamReader("BigTable.wsv")) {
    WsvLine line = null;
    while ((line = reader.readLine()) != null) {
        ...
    }
}

Write a WSV file line by line

try (WsvStreamWriter writer = 
        new WsvStreamWriter("BigTable.wsv", 
                ReliableTxtEncoding.UTF_16, true)) {
    writer.writeLine("ValueA1", "ValueA2", "ValueA3");
    writer.writeLines(
            new WsvLine("ValueB1", "ValueB2", "ValueB3"),
            new WsvLine("ValueC1", "ValueC2", "ValueC3")
    );
}