org.tridas.io.util
Class FileHelper

java.lang.Object
  extended by org.tridas.io.util.FileHelper

public class FileHelper
extends Object

Helps working with most files.

Author:
daniel

Field Summary
 String envPath
           
 
Constructor Summary
FileHelper()
           
FileHelper(String argRootDataPath)
           
 
Method Summary
 File createFile(String where)
           
 InputStream createInput(String filename)
          Simplified method to open a Java InputStream.
 InputStream createInputRaw(String filename)
          Call createInput() without automatic gzip decompression.
 OutputStream createOutput(String filename)
          Similar to createInput(), this creates a Java OutputStream for a given filename or path.
 BufferedReader createReader(String filename)
          I want to read lines from a file.
 PrintWriter createWriter(String filename)
          I want to print lines to a file.
 String fullPath(String where)
          Prepend the Environment path to the filename (or path) that is passed in.
 File inputFile()
           
static Boolean isBinary(String[] argLines)
          Determine whether a file contains non-printable characters, suggesting that its a binary file.
 byte[] loadBytes(String filename)
           
 String[] loadStrings(String filename)
          Load data from a file and shove it into a String array.
 String[] loadStrings(String filename, String argEncoding)
           
 String[] loadStringsFromDetectedCharset(String argFilename)
           
 File outputFile()
           
 void saveBytes(String filename, byte[] buffer)
          Saves bytes to a file to the Environment path.
 File saveFile(String where)
          Identical to savePath(), but returns a File object.
 String savePath(String where)
          Returns a path inside the applet folder to save to.
 void saveStream(File targetFile, String sourceLocation)
          Identical to the other saveStream(), but writes to a File object, for greater control over the file location.
 void saveStream(String targetFilename, String sourceLocation)
          Save the contents of a stream to a file usually where the jar is located, but can be customized with the FileHelper(String) constructor.
 void saveStrings(String filename, String[] strings)
           
 void saveStrings(String filename, String[] strings, String argEncoding)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

envPath

public final String envPath
Constructor Detail

FileHelper

public FileHelper()

FileHelper

public FileHelper(String argRootDataPath)
Method Detail

outputFile

public File outputFile()

inputFile

public File inputFile()

createWriter

public PrintWriter createWriter(String filename)
I want to print lines to a file.


createReader

public BufferedReader createReader(String filename)
I want to read lines from a file.


loadStringsFromDetectedCharset

public String[] loadStringsFromDetectedCharset(String argFilename)

loadStrings

public String[] loadStrings(String filename)
Load data from a file and shove it into a String array.

Exceptions are handled internally, when an error, occurs, an exception is printed to the console and 'null' is returned, but the program continues running. This is a tradeoff between 1) showing the user that there was a problem but 2) not requiring that all i/o code is contained in try/catch blocks, for the sake of new users (or people who are just trying to get things done in a "scripting" fashion). If you want to handle exceptions, use Java methods for I/O.


loadStrings

public String[] loadStrings(String filename,
                            String argEncoding)
                     throws UnsupportedEncodingException
Throws:
UnsupportedEncodingException

loadBytes

public byte[] loadBytes(String filename)

createInputRaw

public InputStream createInputRaw(String filename)
Call createInput() without automatic gzip decompression.


createInput

public InputStream createInput(String filename)
Simplified method to open a Java InputStream.

This method is useful if you want to use the facilities provided by PApplet to easily open things from the data folder or from a URL, but want an InputStream object so that you can use other Java methods to take more control of how the stream is read.

If the requested item doesn't exist, null is returned

If not online, this will also check to see if the user is asking for a file whose name isn't properly capitalized. This helps prevent issues when a sketch is exported to the web, where case sensitivity matters, as opposed to Windows and the Mac OS default where case sensitivity is preserved but ignored.

It is strongly recommended that libraries use this method to open data files, so that the loading sequence is handled in the same way as functions like loadBytes(), loadImage(), etc.

The filename passed in can be:


saveStrings

public void saveStrings(String filename,
                        String[] strings)

saveStrings

public void saveStrings(String filename,
                        String[] strings,
                        String argEncoding)
                 throws UnsupportedEncodingException
Throws:
UnsupportedEncodingException

saveBytes

public void saveBytes(String filename,
                      byte[] buffer)
Saves bytes to a file to the Environment path. Usually where the jar is located, but can be customized with the FileHelper(String) constructor. The filename can be a relative path, i.e. "hey/more/directories/wowbytes.txt" would save the "hey" directory in the Environment path.


saveStream

public void saveStream(File targetFile,
                       String sourceLocation)
Identical to the other saveStream(), but writes to a File object, for greater control over the file location. Note that unlike other api methods, this will not automatically uncompress gzip files.


saveStream

public void saveStream(String targetFilename,
                       String sourceLocation)
Save the contents of a stream to a file usually where the jar is located, but can be customized with the FileHelper(String) constructor. This is basically saveBytes(blah, loadBytes()), but done in a less confusing manner.


createOutput

public OutputStream createOutput(String filename)
Similar to createInput(), this creates a Java OutputStream for a given filename or path. The file will be created where the jar is located folder, or customized with FileHelper(String).

If the path does not exist, intermediate folders will be created. If an exception occurs, it will be printed to the console, and null will be returned.


fullPath

public String fullPath(String where)
Prepend the Environment path to the filename (or path) that is passed in. Note that when running as an applet inside a web browser, the Environment path will be set to null, because security restrictions prevent applets from accessing that information.


createFile

public File createFile(String where)

savePath

public String savePath(String where)
Returns a path inside the applet folder to save to. Like jarPath(), but creates any in-between folders so that things save properly.


saveFile

public File saveFile(String where)
Identical to savePath(), but returns a File object.


isBinary

public static Boolean isBinary(String[] argLines)
Determine whether a file contains non-printable characters, suggesting that its a binary file.

Parameters:
argLines -
Returns:


Copyright © 2011. All Rights Reserved.