de.dfki.lt.tools.tokenizer
Class FileTools

java.lang.Object
  extended byde.dfki.lt.tools.tokenizer.FileTools

public class FileTools
extends java.lang.Object

FileTools provides static methods to work on files and stream.

Version:
$Id: FileTools.java,v 1.6 2005/04/12 08:47:37 steffen Exp $
Author:
Joerg Steffen, DFKI

Method Summary
static void copyFile(java.io.File source, java.io.File target)
          This simply copies a source file to a target file.
static java.util.List getFilesFromDir(java.lang.String aDirectory, java.lang.String aSuffix)
          This recursivly collects all filenames in the directory aDirectory with suffix aSuffix and returns them in a List.
static java.nio.ByteBuffer readFile(java.io.File aFile)
          New NIO based method to read the contents of a file as byte[] array.
static java.lang.String readFileAsString(java.io.File aFile, java.lang.String aCharSet)
          New NIO based method to read a file as a String with the given charset encoding.
static java.lang.String readInputStream(java.io.InputStream is)
          Read some input stream and return its content as a string.
static void readInputStream(java.io.OutputStream os, java.io.InputStream is)
          Reads some input stream and writes it into an output stream.
static byte[] readInputStreamToByteArray(java.io.InputStream is)
          Read some input stream and return its content as byte array.
static byte[] readUrlToByteArray(java.net.URL url)
          Read a url content to a byte array.
static java.lang.String readUrlToString(java.net.URL url)
          Read a url content to a string.
static void saveStream(java.io.InputStream inputStream, java.io.File file)
          Write an input stream to a file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

saveStream

public static void saveStream(java.io.InputStream inputStream,
                              java.io.File file)
                       throws java.io.IOException
Write an input stream to a file. Fail, if filename already exists.

Parameters:
inputStream - some stream to be saved
file - the target file
Throws:
java.io.IOException - when file can't be saved

readUrlToString

public static java.lang.String readUrlToString(java.net.URL url)
                                        throws java.io.IOException
Read a url content to a string.

Parameters:
url - some url
Returns:
the content as a string
Throws:
java.io.IOException - thrown when ressource cannot be opened for reading

readUrlToByteArray

public static byte[] readUrlToByteArray(java.net.URL url)
                                 throws java.io.IOException
Read a url content to a byte array.

Parameters:
url - some url
Returns:
the content as a byte array
Throws:
java.io.IOException - thrown when ressource cannot be opened for reading

readInputStream

public static void readInputStream(java.io.OutputStream os,
                                   java.io.InputStream is)
                            throws java.io.IOException
Reads some input stream and writes it into an output stream. The method applies some efficient buffering in byte arrays and is the basis for all read...-methods in this class.

Parameters:
os - some output stream.
is - some input stream.
Throws:
java.io.IOException - thrown when reading or writing fails

readInputStream

public static java.lang.String readInputStream(java.io.InputStream is)
                                        throws java.io.IOException
Read some input stream and return its content as a string.

Parameters:
is - the input stream
Returns:
the content of the stream as string
Throws:
java.io.IOException

readInputStreamToByteArray

public static byte[] readInputStreamToByteArray(java.io.InputStream is)
                                         throws java.io.IOException
Read some input stream and return its content as byte array.

Parameters:
is - the input stream
Returns:
the content of the stream as byte array
Throws:
java.io.IOException

getFilesFromDir

public static java.util.List getFilesFromDir(java.lang.String aDirectory,
                                             java.lang.String aSuffix)
This recursivly collects all filenames in the directory aDirectory with suffix aSuffix and returns them in a List.

Parameters:
aDirectory - a String with the directory name
aSuffix - a String with a filename suffix
Returns:
a List with the filenames

copyFile

public static void copyFile(java.io.File source,
                            java.io.File target)
                     throws java.io.IOException
This simply copies a source file to a target file.

Parameters:
source - the source File to copy
target - the target File
Throws:
java.io.IOException

readFile

public static java.nio.ByteBuffer readFile(java.io.File aFile)
                                    throws java.io.FileNotFoundException,
                                           java.io.IOException
New NIO based method to read the contents of a file as byte[] array. Only files up to size Integer.MAX_INT can be read. The ByteBuffer is rewinded when returned.

Throws:
java.io.FileNotFoundException
java.io.IOException

readFileAsString

public static java.lang.String readFileAsString(java.io.File aFile,
                                                java.lang.String aCharSet)
                                         throws java.io.FileNotFoundException,
                                                java.io.IOException,
                                                java.nio.charset.UnsupportedCharsetException
New NIO based method to read a file as a String with the given charset encoding.

Parameters:
aCharSet - the charset to use for conversion, if null ISO-8859-15 is used
Throws:
java.io.FileNotFoundException
java.io.IOException
java.nio.charset.UnsupportedCharsetException