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.CloseableFile 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 classFileWrapper.FileStatestatic classFileWrapper.OpenMode
-
Field Summary
Fields Modifier and Type Field Description private java.io.Filefileprivate java.io.FileInputStreamfisprivate java.io.FileOutputStreamfosprivate java.io.InputStreamReaderisrprivate java.io.OutputStreamWriteroswprivate java.io.BufferedReaderreaderprivate FileWrapper.FileStatestateprivate java.io.BufferedWriterwriter
-
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 booleancanWrite()voidclose()Close the file, and set its state to CLOSED.voidcopyTo(FileWrapper target, byte[] buffer)Copy this file to target using buffer to hold data.voiddelete()java.lang.StringgetAbsoluteName()FileWrapper.FileStategetFileState()java.lang.StringgetName()(package private) booleanisYoungerThan(FileWrapper fw)Returns true if either this FileWrapper does not exist, or if the lastModificationTime of this FileWrapper is earlier than that of fw.voidopen(FileWrapper.OpenMode mode)Open the (text) file for I/O.byte[]readAll()java.lang.StringreadLine()Read the next line from the text file.java.lang.StringtoString()voidwriteAll(byte[] data)voidwriteLine(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:
toStringin 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.IOExceptionRead 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.IOExceptionWrite 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:
closein interfacejava.lang.AutoCloseable- Specified by:
closein 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.
-
-