Class FileWrapper
java.lang.Object
org.glassfish.pfl.basic.tools.file.FileWrapper
- All Implemented Interfaces:
Closeable
,AutoCloseable
File wrapper for text files. Makes it really easy to open, close, delete, read, and write
text files.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
static enum
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final File
private FileInputStream
private FileOutputStream
private InputStreamReader
private OutputStreamWriter
private BufferedReader
private FileWrapper.FileState
private BufferedWriter
-
Constructor Summary
ConstructorsConstructorDescriptionFileWrapper
(File file) Create a new FileWrapper for the given File.FileWrapper
(File root, String str) FileWrapper
(String str) -
Method Summary
Modifier and TypeMethodDescriptionboolean
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()
getName()
(package private) boolean
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()
readLine()
Read the next line from the text file.toString()
void
writeAll
(byte[] data) void
Write the line to the end of the file, including a newline.
-
Field Details
-
file
-
fis
-
isr
-
reader
-
fos
-
osw
-
writer
-
state
-
-
Constructor Details
-
FileWrapper
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
-
FileWrapper
-
-
Method Details
-
canWrite
public boolean canWrite() -
toString
-
isYoungerThan
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
-
getAbsoluteName
-
readAll
- Throws:
IOException
-
writeAll
- Throws:
IOException
-
readLine
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:
IOException
- for IO errors.
-
writeLine
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:
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 interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
open
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:
IOException
- for IO exceptions.
-
getFileState
-
copyTo
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:
IOException
- for IO exceptions.
-