Example usage for org.apache.commons.csv CSVRecord getComment

List of usage examples for org.apache.commons.csv CSVRecord getComment

Introduction

In this page you can find the example usage for org.apache.commons.csv CSVRecord getComment.

Prototype

public String getComment() 

Source Link

Document

Returns the comment for this record, if any.

Usage

From source file:com.itemanalysis.jmetrik.data.JmetrikFileImporterTest.java

@Test
public void readJmetrikFileTest() {
    System.out.println("JmetrikFileImporterTest: Reading *.jmetrik file");
    CSVParser parser = null;//from   w w  w  .j a v a  2  s.  c om
    Reader reader = null;

    try {
        File dataFile = FileUtils.toFile(this.getClass().getResource("/data/example-import-file.jmetrik"));
        reader = new InputStreamReader(new BOMInputStream(new FileInputStream(dataFile)), "UTF-8");

        parser = new CSVParser(reader, CSVFormat.DEFAULT.withCommentMarker('#'));
        Iterator<CSVRecord> iter = parser.iterator();
        CSVRecord temp = null;

        boolean readAttributes = false;
        boolean readData = false;
        int attCount = 0;

        while (iter.hasNext()) {
            temp = iter.next();

            if ("VERSION".equals(temp.getComment())) {
                System.out.println("VERSION: " + temp.get(0));

            } else if ("METADATA".equals(temp.getComment())) {
                System.out.println("CASES: " + temp.get(0));
            } else if ("ATTRIBUTES".equals(temp.getComment())) {
                readAttributes = true;
            } else if ("DATA".equals(temp.getComment())) {
                readAttributes = false;
                readData = true;
            }

            if (readAttributes) {
                System.out.print("ATTRIBUTE-" + attCount + ": ");
                Iterator<String> innerIter = temp.iterator();
                while (innerIter.hasNext()) {
                    System.out.print(innerIter.next());
                    if (innerIter.hasNext()) {
                        System.out.print(",");
                    }
                }
                System.out.println();
                attCount++;
            }

            if (readData) {
                Iterator<String> innerIter = temp.iterator();
                while (innerIter.hasNext()) {
                    System.out.print(innerIter.next());
                    if (innerIter.hasNext()) {
                        System.out.print(",");
                    }
                }
                System.out.println();
            }

        }

    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        try {
            parser.close();
            reader.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}