org.glassfish.pfl.basic.tools.file
Class FileWrapper

java.lang.Object
  extended by org.glassfish.pfl.basic.tools.file.FileWrapper
All Implemented Interfaces:
Closeable

public class FileWrapper
extends Object
implements Closeable

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


Nested Class Summary
static class FileWrapper.FileState
           
static class FileWrapper.OpenMode
           
 
Constructor Summary
FileWrapper(File file)
          Create a new FileWrapper for the given File.
FileWrapper(File root, String str)
           
FileWrapper(String str)
           
 
Method Summary
 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()
           
 String getAbsoluteName()
           
 FileWrapper.FileState getFileState()
           
 String getName()
           
 void open(FileWrapper.OpenMode mode)
          Open the (text) file for I/O.
 byte[] readAll()
           
 String readLine()
          Read the next line from the text file.
 String toString()
           
 void writeAll(byte[] data)
           
 void writeLine(String line)
          Write the line to the end of the file, including a newline.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FileWrapper

public FileWrapper(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(String str)

FileWrapper

public FileWrapper(File root,
                   String str)
Method Detail

canWrite

public boolean canWrite()

toString

public String toString()
Overrides:
toString in class Object

delete

public void delete()

getName

public String getName()

getAbsoluteName

public String getAbsoluteName()

readAll

public byte[] readAll()
               throws IOException
Throws:
IOException

writeAll

public void writeAll(byte[] data)
              throws IOException
Throws:
IOException

readLine

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:
The String just read.
Throws:
IOException - for IO errors.

writeLine

public void writeLine(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 - 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 interface Closeable

open

public void open(FileWrapper.OpenMode mode)
          throws IOException
Open the (text) file for I/O. There are two modes:

Parameters:
mode - READ or WRITE mode.
Throws:
IOException - for IO exceptions.

getFileState

public FileWrapper.FileState getFileState()

copyTo

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 - The FileWrapper to copy data to.
buffer - The buffer to use for copying the files.
Throws:
IOException - for IO exceptions.


Copyright © 2013 Oracle. All Rights Reserved.