public class FileResource
extends java.lang.Object
FileResource
class represents a file and allows access to its contents a line at
a time, using the method lines
, or a word at a time, using the method
words
. These strings can then be iterated over using a for
loop.
Example usage:
FileResource fr = new FileResource(); for (String s : fr.words()) { // print or process s }
If each line of the file represents separated data values, because its a CSV file, then the user
can get a getCSVParser
object to access that data more directly, using one of the
getCSVParser
methods.
Example CSV usage:
FileResource fr = new FileResource("food.csv"); for (CSVRecord record : fr.getCSVParser()) { // print or process fields in record String name = record.get("Name"); // other processing }
This software is licensed with an Apache 2 license, see http://www.apache.org/licenses/LICENSE-2.0 for details.
Constructor and Description |
---|
FileResource()
Create a
FileResource object that opens the file chosen by the user using a file
selection dialog box. |
FileResource(boolean writable)
Create a
FileResource object that opens the file chosen by the user using a file
selection dialog box, possibly to write to it. |
FileResource(java.io.File file)
Create a
FileResource object that opens a file represented by the File object
passed as a parameter. |
FileResource(java.io.File file,
boolean writable)
Create a
FileResource object that opens a file represented by the File object
passed as a parameter, possibly to write to it. |
FileResource(java.lang.String filename)
Create a
FileResource object that opens a file whose name is passed as a
parameter. |
FileResource(java.lang.String filename,
boolean writable)
Create a
FileResource object that opens a file whose name is passed as a
parameter, possibly to write to it. |
Modifier and Type | Method and Description |
---|---|
java.lang.String |
asString()
Return entire contents of this opened file as one string.
|
java.lang.Iterable<java.lang.String> |
getCSVHeaders(CSVParser parser)
Allows access to the column names of the header row of a CSV file (the first line in the
file) one at a time.
|
CSVParser |
getCSVParser()
Returns a
CSVParser object to access the contents of an open file. |
CSVParser |
getCSVParser(boolean withHeader)
Returns a
CSVParser object to access the contents of an open file, possibly
without a header row. |
CSVParser |
getCSVParser(boolean withHeader,
java.lang.String delimiter)
Returns a
CSVParser object to access the contents of an open file, possibly
without a header row and a different data delimiter than a comma. |
java.lang.Iterable<java.lang.String> |
lines()
Allow access to this opened file one line at a time.
|
java.lang.Iterable<java.lang.String> |
words()
Allow access to this opened file one word at a time, where words are separated by
white-space.
|
void |
write(StorageResource list)
Writes a list of strings to the end of this file, one element per line.
|
void |
write(java.lang.String s)
Writes a string to the end of this file.
|
public FileResource()
FileResource
object that opens the file chosen by the user using a file
selection dialog box.exception
- if no file is selected by the userpublic FileResource(java.io.File file)
FileResource
object that opens a file represented by the File object
passed as a parameter.
Useful, for example, when used in conjunction with the DirectoryResource
class.file
- the file to be represented by this resourceexception
- if the file cannot be accessedpublic FileResource(java.lang.String filename)
FileResource
object that opens a file whose name is passed as a
parameter.
The named file should be on the current class path to be found.filename
- the name of the file to be openedexception
- if the filename cannot be accessedpublic FileResource(boolean writable)
FileResource
object that opens the file chosen by the user using a file
selection dialog box, possibly to write to it.
If the user wants to change the contents of the open file by using the method
write
to add new strings to it, pass true
as the second parameter.
Otherwise it is assumed the user will only iterate over the existing contents of the file.writable
- allow changes to this file only if trueexception
- if no file is selected by the userpublic FileResource(java.io.File file, boolean writable)
FileResource
object that opens a file represented by the File object
passed as a parameter, possibly to write to it.
If the user wants to change the contents of the open file by using the method
write
to add new strings to it, pass true
as the second parameter.
Otherwise it is assumed the user will only iterate over the existing contents of the file.
Useful, for example, when used in conjunction with the DirectoryResource
class.file
- the file to be represented by this resourcewritable
- allow changes to this file only if trueexception
- if the file cannot be accessedpublic FileResource(java.lang.String filename, boolean writable)
FileResource
object that opens a file whose name is passed as a
parameter, possibly to write to it.
If the user wants to change the contents of the open file by using the method
write
to add new strings to it, pass true
as the second parameter.
Otherwise it is assumed the user will only iterate over the existing contents of the file.
The named file should be on the current class path to be found.filename
- the name of the file to be openedwritable
- allow changes to this file only if trueexception
- if the filename cannot be accessedpublic java.lang.Iterable<java.lang.String> lines()
Iterable
that will allow access to contents of opened file one line
at a time.public java.lang.Iterable<java.lang.String> words()
Iterable
that will allow access to contents of opened file one word
at a time.public java.lang.String asString()
String
that is the contents of the open filepublic CSVParser getCSVParser()
CSVParser
object to access the contents of an open file.
Each line of the file should be formatted as data separated by commas and with a header row
to describe the column names.CSVParser
that can provide access to the records in the file one at a
timeexception
- if this file does not represent a CSV formatted datapublic CSVParser getCSVParser(boolean withHeader)
CSVParser
object to access the contents of an open file, possibly
without a header row.
Each line of the file should be formatted as data separated by commas and with/without a
header row to describe the column names.withHeader
- uses first row of data as a header row only if trueCSVParser
that can provide access to the records in the file one at a
timeexception
- if this file does not represent a CSV formatted datapublic CSVParser getCSVParser(boolean withHeader, java.lang.String delimiter)
CSVParser
object to access the contents of an open file, possibly
without a header row and a different data delimiter than a comma.
Each line of the file should be formatted as data separated by the delimiter passed as a
parameter and with/without a header row to describe the column names. This is useful if the
data is separated by some character other than a comma.withHeader
- uses first row of data as a header row only if truedelimiter
- a single character that separates one field of data from anotherCSVParser
that can provide access to the records in the file one at a
timeexception
- if this file does not represent a CSV formatted dataexception
- if delimiter.length() != 1
public java.lang.Iterable<java.lang.String> getCSVHeaders(CSVParser parser)
Iterator
is returned.parser
- the CSVParser
that has been created for this fileIterable
that allows access one header name at a timepublic void write(java.lang.String s)
s
- the string to saved to the filepublic void write(StorageResource list)
list
- the strings to saved to the file