All Packages Class Hierarchy This Package Previous Next Index
Class net.strandberg.file.TableInFile
java.lang.Object
|
+----net.strandberg.file.TableInFile
- public class TableInFile
- extends Object
Class for reading input files as tables assuming they are textfiles
with 8-bit ASCII (Latin-1) and NOT Unicode. Each line can be
accessed as an array of strings assuming they are separated by
separators. The separators can be given in the constructor or,
if not given, is assumed to be the default separator, which is TAB.
The table can be a file having two tab-separated strings on each line, E.g.
favourite color black
favourite dish pea soup
Assuming the name of this file is mytable.tab then the file can then
be accessed as:
TableInFile tfile = new TableInFile("/home/myuser/mytable.tab");
String[][] table = tfile.getTable();
for (int i = 0; i < table.length; i++) {
if (table[i][0].equals("favourite dish")
&& table[i][1].equals("pea soup") )
{
System.out.println("yak");
}
}
This will produce the output "yak".
$Revision: 0.2 $
-
delimitersM
-
-
TableInFile(String)
-
Constructs a new TableInFile which will get its contents from the file
pointed to by the String argument.
-
TableInFile(String, String)
-
Constructs a new TableInFile which will get its contents from the file
pointed to by the first String argument.
-
getTable()
- Gets the contents of this file as an N x M matrix of strings.
-
getTable(int)
- Gets the contents of this file as an N x M matrix of strings.
delimitersM
protected String delimitersM
TableInFile
public TableInFile(String path)
- Constructs a new TableInFile which will get its contents from the file
pointed to by the String argument.
The file is not opened by the constructor.
- Parameters:
- path - the path in the file system to the TableInFile
TableInFile
public TableInFile(String path,
String delimiters)
- Constructs a new TableInFile which will get its contents from the file
pointed to by the first String argument.
The file is not opened by the constructor.
- Parameters:
- path - the path in the file system to the TableInFile
- delimiters - set of delimiters separating columns in one row
(default is TAB).
getTable
public String[][] getTable() throws FileAccessException
- Gets the contents of this file as an N x M matrix of strings.
N is rows and M is the columns. Each column is supposed to be
separated by any of the delimiters given in the constructor
(or TAB if no delimiters were given)
- Returns:
- an N x M matrix of strings representing the Table
- Throws: FileAccessException
- If the file does not exist or cannot
be read etc.
getTable
public String[][] getTable(int expectedColumnWidth) throws FileAccessException, ColumnWidthException
- Gets the contents of this file as an N x M matrix of strings.
N is rows and M is the columns. Each column is supposed to be
separated by any of the delimiters given in the constructor
(or TAB if no delimiters were given). M is equal to expectedColumnWidth
or an exception is thrown.
- Parameters:
- expectedColumnWidth - the column width that the return matrix
MUST have
- Returns:
- an N x expectedColumnWidth matrix of strings representing
the Table
- Throws: FileAccessException
- If the file does not exist or cannot
be read etc.
- Throws: ColumnWidthException
- If the file is NOT a table with
columns that are expectedColumnWidth wide.
All Packages Class Hierarchy This Package Previous Next Index