Skip to content

Class CSVWriter

Ori Roth edited this page Apr 2, 2017 · 3 revisions

Synopsis of Class CSVWriter

public class CSVWriter { 
    /*
     * Forge (1)
     */
        CSVWriter(String fileName) throws IOException; 
    /*
     * Type (5)
     */
        void write(CSVLine cl) throws IOException; 
        void writeflush(CSVLine cl) throws IOException; 
        void close() throws IOException; 
        String header(); 
        String fileName; 
} 

Input types: CSVLine.

Exception types: IOException.

Code

// SSDLPedia
package il.ac.technion.cs.ssdl.csv;

import static il.ac.technion.cs.ssdl.utils.DBC.require;
import il.ac.technion.cs.ssdl.stereotypes.Instantiable;

import java.io.FileWriter;
import java.io.IOException;

/**
 * A class to generate a CSV (comma separated values) file, by writing into it a
 * sequence of CSVLine records.
 * 
 * Author: Yossi Gil, 2008/06/20
 */
@Instantiable public class CSVWriter {
    /**
     * Create a new instance, writing into a given named file
     * 
     * fileName the name of the output file
     * IOException in case the file cannot be created or opened for
     *             writing
     */
    public CSVWriter(final String fileName) throws IOException {
        w = new FileWriter(this.fileName = fileName);
    }
    
    /**
     * Write a new CSV line into the file. All CSV lines written into the file
     * must have the exact same header.
     * 
     * cl the CSV line to be written
     * IOException in case the writing was not successful
     */
    public void write(final CSVLine cl) throws IOException {
        require(header == null || header.equals(cl.header()));
        if (header == null)
            writeln(header = cl.header());
        writeln(cl.line());
    }
    
    /**
     * Same as #write(CSVLine), except that the file buffer is flushed
     * after successful write.
     * 
     * cl the CSV line to be written
     * IOException in case the writing was not successful
     */
    public void writeflush(final CSVLine cl) throws IOException {
        write(cl);
        w.flush();
    }
    
    /**
     * Close the file after completion. No further writes are allowed.
     * 
     * IOException in case the file writer could not be closed
     */
    public void close() throws IOException {
        w.close();
    }
    
    /**
     * Find the common header of all CSV lines written to this file.
     * 
     * Return: the header of the first line written (all further lines must
     *         agree)
     */
    public String header() {
        return header;
    }
    
    private void writeln(final String s) throws IOException {
        w.write(s);
        w.write("\n");
    }
    
    private String header = null;
    /**
     * The name of the file into which records are written.
     */
    public String fileName;
    private final FileWriter w;
}

Metrics

Metric Value Acronym Explanation
LOC 85 Lines Of Code Total number of lines in the code
SCC 18 SemiColons Count Total number of semicolon tokens found in the code.
NOT 234 Number Of Tokens Comments, whitespace and text which cannot be made into a token not included.
VCC 1775 Visible Characters Count The total number of non-white (i.e., not space, tab, newline, carriage return, form feed) characters.
CCC 787 Code Characters Count Total number of non-white characters in tokens. White space characters in string and character literals are not counted.
UIC 30 Unique Identifiers Count The number of different identifiers found in the code
WHC 2 Weighted Horizontal Complexity A heuritistic on horizontal complexity

Statistics

Statistic Value
Average token length 3.4
Tokens/line 2.8
Visible characters/line 21
Code characters/line 9.3
Semicolons/tokens 7%
Comment text percentage 55%

Tokens by type

Token Kind Occurrences
KEYWORD 38
OPERATOR 7
LITERAL 1
ID 85
PUNCTUATION 103
COMMENT 8
OTHER 109
Clone this wiki locally