DataFile 1.4

com.infomata.data
Class DataFile

java.lang.Object
  extended bycom.infomata.data.DataFile

public class DataFile
extends java.lang.Object

Main class for generating or reading data files written in widely accepted data format. Currently supported data formats are tab separate (TabFormat) and CSV (CSVFormat), but more data formats can easily be added by implementing DataFormat

USAGE:

 // Creating a reader for CSV file using ISO-8859-1
 
 DataFile read = DataFile.createReader("8859_1");
 read.setDataFormat(new CSVFormat());
 
 try
 {
 
     read.open(new File("/data/test.csv"));
 
     for (DataRow row = read.next(); row != null; row = read.next())
     {
         String text = row.getString(0);
         int number1 = row.getInt(1, 0);
         double number2 = row.getDouble(2);
         // use the retrieved data ...
     }
 }
 
 finally
 {
     read.close();
 }
 
 // Creating a writer for data file with European encoding
 // ISO-8859-2 (European) using tab separated format.
 // (rewrite existing file)
 
 DataFile write = DataFile.createWriter("8859_2", false);
 write.setDataFormat(new TabFormat());
 
 try
 {
 
     write.open(new File("/data/test.txt"));
 
     for (DataRow row = write.next(); row != null; row = write.next())
     {
         row.add("some German text");
         row.add(123);
         row.add(13323.23d);
     }
 }
 
 finally
 {
     write.close();
 }
 

Version:
$Revision: 1.3 $
Author:
Sam Kim

Method Summary
 void close()
          Closes a data file.
 boolean containsHeader()
          Checks if header indicator has been turned on using containsHeader(boolean).
 void containsHeader(boolean header)
          Sets the indicator for header row (must be first line of data file).
static DataFile createReader(java.lang.String enc)
          Creates a reader instance of a data file.
static DataFile createWriter(java.lang.String enc, boolean append)
          Creates a writer instance of DataFile used for writing a data file.
 void finalize()
          Finalization method that closes the file descriptor.
 java.util.List getHeaderList()
          Retrieves the list of header names found in the first row of the data file in the sequence they appear.
 java.util.Iterator getHeaderNames()
          Deprecated. use getHeaderList() instead.
 DataRow next()
          Retrieves the next row for reading or writing data
 void open(java.io.File file)
          Opens a data file for reading or writing
 void open(java.net.URL file)
           
 void setDataFormat(DataFormat format)
          Sets the data format to be read or written.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

createReader

public static final DataFile createReader(java.lang.String enc)
Creates a reader instance of a data file.

Parameters:
enc - Java character encoding in which the data file is written.
Returns:
a reader instance of the DataFile.

createWriter

public static final DataFile createWriter(java.lang.String enc,
                                          boolean append)
Creates a writer instance of DataFile used for writing a data file.

Parameters:
enc - Java encoding to use while writing the data file containing none-English characters.
append - True if appending to existing data file. False, if writing to a new file or overwriting current file.
Returns:
a writer instance of DataFile.

containsHeader

public final void containsHeader(boolean header)
Sets the indicator for header row (must be first line of data file).

Parameters:
header - True if the file contains a header row. False, otherwise.

containsHeader

public final boolean containsHeader()
Checks if header indicator has been turned on using containsHeader(boolean). Data file reader lacks the logic to determine if the first row is a header row, automatically, so the existance of header row must be indicated prior to reading the data file.

Returns:
True, if header file indicator has been set to true. False, otherwise.

setDataFormat

public final void setDataFormat(DataFormat format)
Sets the data format to be read or written.

Parameters:
format - an instance of a class that implements DataFormat interface.
See Also:
DataFormat

finalize

public void finalize()
Finalization method that closes the file descriptor. This would work only if JVM is current (1.3 or later?).


close

public final void close()
Closes a data file.


open

public final void open(java.io.File file)
                throws java.io.IOException
Opens a data file for reading or writing

Parameters:
file - data file
Throws:
java.io.IOException - if an error occurs

open

public final void open(java.net.URL file)
                throws java.io.IOException
Throws:
java.io.IOException

next

public final DataRow next()
                   throws java.io.IOException
Retrieves the next row for reading or writing data

Returns:
a reference to next data row.
Throws:
java.io.IOException - if an error occurs

getHeaderNames

public final java.util.Iterator getHeaderNames()
Deprecated. use getHeaderList() instead.

Retrieves the list of header names found in the first row of the data file. In order for this to work, the header row indicator must be set prior to opening the data file. Note: the header names are not guaranteed to be the order they appear in the first row of the data file.

Returns:
header names (not guaranteed in the order in which they appear in file).

getHeaderList

public final java.util.List getHeaderList()
Retrieves the list of header names found in the first row of the data file in the sequence they appear. In order for this to work, the header row indicator must be set prior to opening the data file.

Returns:
header names (in sequence);

DataFile 1.4

Copyright © 2004 Infomata. All Rights Reserved.