Top Description Inners Fields Constructors Methods
org.glassfish.pfl.basic.tools.file

public Class FileWrapper

extends Object
implements Closeable
Class Inheritance
All Implemented Interfaces
java.io.Closeable, java.lang.AutoCloseable
Imports
java.io.BufferedReader, .BufferedWriter, .Closeable, .File, .FileInputStream, .FileOutputStream, .IOException, .InputStreamReader, .OutputStreamWriter

File wrapper for text files. Makes it really easy to open, close, delete, read, and write text files.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum
public static enum

Field Summary

Modifier and TypeField and Description
private final File
private FileInputStream
private FileOutputStream
private InputStreamReader
private OutputStreamWriter
private BufferedReader
private FileWrapper.FileState
private BufferedWriter

Constructor Summary

AccessConstructor and Description
public
FileWrapper(final File
File to wrap
file
)

Create a new FileWrapper for the given File.

public
public
FileWrapper(File root, String str)

Method Summary

Modifier and TypeMethod and Description
public boolean
public void
close()

Implements java.io.Closeable.close.

Close the file, and set its state to CLOSED.

public void
copyTo(FileWrapper
The FileWrapper to copy data to.
target
,
byte[]
The buffer to use for copying the files.
buffer
)

Copy this file to target using buffer to hold data.

public void
public String
public FileWrapper.FileState
public String
pack-priv 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.

public void
open(final FileWrapper.OpenMode
READ or WRITE mode.
mode
)

Open the (text) file for I/O.

public byte[]
public String

Returns:

The String just read.
readLine
()

Read the next line from the text file.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public void
writeAll(byte[] data)

public void
writeLine(final String
The line to write.
line
)

Write the line to the end of the file, including a newline.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

fileback to summary
private final File file
fisback to summary
private FileInputStream fis
fosback to summary
private FileOutputStream fos
isrback to summary
private InputStreamReader isr
oswback to summary
private OutputStreamWriter osw
readerback to summary
private BufferedReader reader
stateback to summary
private FileWrapper.FileState state
writerback to summary
private BufferedWriter writer

Constructor Detail

FileWrapperback to summary
public FileWrapper(final 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

File to wrap

FileWrapperback to summary
public FileWrapper(String str)
FileWrapperback to summary
public FileWrapper(File root, String str)

Method Detail

canWriteback to summary
public boolean canWrite()
closeback to summary
public void close()

Implements java.io.Closeable.close.

Close the file, and set its state to CLOSED. This method does not throw any exceptions.

Annotations
@Override
copyToback to summary
public void copyTo(FileWrapper target, byte[] buffer) throws IOException

Copy this file to target using buffer to hold data. Does not assume we are using text files.

Parameters
target:FileWrapper

The FileWrapper to copy data to.

buffer:byte[]

The buffer to use for copying the files.

Exceptions
IOException:
for IO exceptions.
deleteback to summary
public void delete()
getAbsoluteNameback to summary
public String getAbsoluteName()
getFileStateback to summary
public FileWrapper.FileState getFileState()
getNameback to summary
public String getName()
isYoungerThanback to summary
pack-priv 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.

openback to summary
public void open(final FileWrapper.OpenMode mode) throws 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:FileWrapper.OpenMode

READ or WRITE mode.

Exceptions
IOException:
for IO exceptions.
readAllback to summary
public byte[] readAll() throws IOException
readLineback to summary
public String readLine() throws 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:String

The String just read.

Exceptions
IOException:
for IO errors.
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Annotations
@Override
writeAllback to summary
public void writeAll(byte[] data) throws IOException
writeLineback to summary
public void writeLine(final String line) throws IOException

Write the line to the end of the file, including a newline. File state must be FileState OPEN_FOR_WRITE.

Parameters
line:String

The line to write.

Exceptions
IOException:
for IO errors.
org.glassfish.pfl.basic.tools.file back to summary

public final Enum FileWrapper.FileState

extends Enum<FileWrapper.FileState>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final FileWrapper.FileState
public static final FileWrapper.FileState
public static final FileWrapper.FileState

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static FileWrapper.FileState
public static FileWrapper.FileState[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

CLOSEDback to summary
public static final FileWrapper.FileState CLOSED
OPEN_FOR_READback to summary
public static final FileWrapper.FileState OPEN_FOR_READ
OPEN_FOR_WRITEback to summary
public static final FileWrapper.FileState OPEN_FOR_WRITE

Constructor Detail

FileStateback to summary
private FileState()

Method Detail

valueOfback to summary
public static FileWrapper.FileState valueOf(String name)
valuesback to summary
public static FileWrapper.FileState[] values()
org.glassfish.pfl.basic.tools.file back to summary

public final Enum FileWrapper.OpenMode

extends Enum<FileWrapper.OpenMode>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final FileWrapper.OpenMode
public static final FileWrapper.OpenMode
public static final FileWrapper.OpenMode

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static FileWrapper.OpenMode
public static FileWrapper.OpenMode[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

READback to summary
public static final FileWrapper.OpenMode READ
WRITEback to summary
public static final FileWrapper.OpenMode WRITE
WRITE_EMPTYback to summary
public static final FileWrapper.OpenMode WRITE_EMPTY

Constructor Detail

OpenModeback to summary
private OpenMode()

Method Detail

valueOfback to summary
public static FileWrapper.OpenMode valueOf(String name)
valuesback to summary
public static FileWrapper.OpenMode[] values()