Class FileWrapper
- java.lang.Object
-
- org.glassfish.pfl.basic.tools.file.FileWrapper
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
public class FileWrapper extends java.lang.Object implements java.io.Closeable
File wrapper for text files. Makes it really easy to open, close, delete, read, and write text files.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
FileWrapper.FileState
static class
FileWrapper.OpenMode
-
Field Summary
Fields Modifier and Type Field Description private java.io.File
file
private java.io.FileInputStream
fis
private java.io.FileOutputStream
fos
private java.io.InputStreamReader
isr
private java.io.OutputStreamWriter
osw
private java.io.BufferedReader
reader
private FileWrapper.FileState
state
private java.io.BufferedWriter
writer
-
Constructor Summary
Constructors Constructor Description FileWrapper(java.io.File file)
Create a new FileWrapper for the given File.FileWrapper(java.io.File root, java.lang.String str)
FileWrapper(java.lang.String str)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
canWrite()
void
close()
Close the file, and set its state to CLOSED.void
copyTo(FileWrapper target, byte[] buffer)
Copy this file to target using buffer to hold data.void
delete()
java.lang.String
getAbsoluteName()
FileWrapper.FileState
getFileState()
java.lang.String
getName()
(package private) boolean
isYoungerThan(FileWrapper fw)
Returns true if either this FileWrapper does not exist, or if the lastModificationTime of this FileWrapper is earlier than that of fw.void
open(FileWrapper.OpenMode mode)
Open the (text) file for I/O.byte[]
readAll()
java.lang.String
readLine()
Read the next line from the text file.java.lang.String
toString()
void
writeAll(byte[] data)
void
writeLine(java.lang.String line)
Write the line to the end of the file, including a newline.
-
-
-
Field Detail
-
file
private final java.io.File file
-
fis
private java.io.FileInputStream fis
-
isr
private java.io.InputStreamReader isr
-
reader
private java.io.BufferedReader reader
-
fos
private java.io.FileOutputStream fos
-
osw
private java.io.OutputStreamWriter osw
-
writer
private java.io.BufferedWriter writer
-
state
private FileWrapper.FileState state
-
-
Constructor Detail
-
FileWrapper
public FileWrapper(java.io.File file)
Create a new FileWrapper for the given File. Represents the same file in the filesystem as the underlying File object. getBase() return the FileWrapper for the file system root.- Parameters:
file
- File to wrap
-
FileWrapper
public FileWrapper(java.lang.String str)
-
FileWrapper
public FileWrapper(java.io.File root, java.lang.String str)
-
-
Method Detail
-
canWrite
public boolean canWrite()
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
isYoungerThan
boolean isYoungerThan(FileWrapper fw)
Returns true if either this FileWrapper does not exist, or if the lastModificationTime of this FileWrapper is earlier than that of fw.
-
delete
public void delete()
-
getName
public java.lang.String getName()
-
getAbsoluteName
public java.lang.String getAbsoluteName()
-
readAll
public byte[] readAll() throws java.io.IOException
- Throws:
java.io.IOException
-
writeAll
public void writeAll(byte[] data) throws java.io.IOException
- Throws:
java.io.IOException
-
readLine
public java.lang.String readLine() throws java.io.IOException
Read the next line from the text file. File state must be FileState OPEN_FOR_READ. Returns null when at the end of file.- Returns:
- The String just read.
- Throws:
java.io.IOException
- for IO errors.
-
writeLine
public void writeLine(java.lang.String line) throws java.io.IOException
Write the line to the end of the file, including a newline. File state must be FileState OPEN_FOR_WRITE.- Parameters:
line
- The line to write.- Throws:
java.io.IOException
- for IO errors.
-
close
public void close()
Close the file, and set its state to CLOSED. This method does not throw any exceptions.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
-
open
public void open(FileWrapper.OpenMode mode) throws java.io.IOException
Open the (text) file for I/O. There are two modes:- READ. In this mode, the file is prepared for reading, starting from the beginning. end-of-file at the time the file is opened.
- WRITE. In this mode, the file is prepared for writing, starting at the end of the file.
- Parameters:
mode
- READ or WRITE mode.- Throws:
java.io.IOException
- for IO exceptions.
-
getFileState
public FileWrapper.FileState getFileState()
-
copyTo
public void copyTo(FileWrapper target, byte[] buffer) throws java.io.IOException
Copy this file to target using buffer to hold data. Does not assume we are using text files.- Parameters:
target
- The FileWrapper to copy data to.buffer
- The buffer to use for copying the files.- Throws:
java.io.IOException
- for IO exceptions.
-
-