MDi JLib
1.7.4

com.microdeveloper.util.io
Class Files

java.lang.Object
  extended by com.microdeveloper.util.io.Files

public class Files
extends Object

Utilities for managing disk files.

Since:
1.0
Version:
1.1
Author:
MicroDeveloper, Inc.

Method Summary
static boolean appendText(String filename, String text)
           
static void arrayToFile(String[] s, String fileName)
          Replaced with toTextFile().
static void byteArrayToFile(byte[] data, String filename)
          Given a byte array construct a file.
static void copy(InputStream in, OutputStream out)
           
static void delete(String fileName)
          Delete a specific file.
static void deleteAllFilesInDir(String directoryName)
          Delete all files in a directory.
static void deleteDirectory(File dir)
          Delete a directory structure.
static String[] fileToArray(String fileName)
          All of a text file is read in and stored in a String array.
static byte[] fileToByteArray(String filename)
          Read a file from the disk into a byte array.
static byte[] fileToByteArrayE(String filename)
          Read a file from the disk into a byte array.
static HashSet fileToHashSet(String FileName)
          All of a text file is read in and unique elements are stored in a HashSet.
static List fileToList(String fileName)
          All of a text file is read in and stored in a Vector.
static String[] fileToUniqueArray(String FileName)
          All of a text file is read in and unique Strings are stored in a String array.
static Vector fileToVector(String fileName)
          All of a text file is read in and stored in a Vector.
static String[] getAllFilesInDirectory(String directory)
          Return a string array containing the names of all the files (not directories) in the current directory.
static String[] getDirectory(File f)
          Return a string array containing the current directory.
static String[] getDirectoryDetails(File f)
          Return a string array containing the current directory with timestamps and file sizes.
static String getResourceAsString(Object obj, String filename)
          Pass in an object and a relative filename and the file will be opened relative to where the class came from.
static Object readObject(String fileName)
          Read one object from a file.
static void rename(String source, String dest)
          Change the file name.
static void toTextFile(List list, String fileName)
          Each element of a List has the toString() method performed on it and written to a text file.
static void toTextFile(String[] s, String fileName)
          Each element of a string array is written to a text file.
static boolean writeObject(String fileName, Object obj)
          Write one object to a file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getResourceAsString

public static String getResourceAsString(Object obj,
                                         String filename)
                                  throws Exception
Pass in an object and a relative filename and the file will be opened relative to where the class came from.

Suppose you have a class called com.cows.Moo. And in the constructor you want to load your essay on cows conveniently located at com/cows/data/mooing_cow.txt. This file sits in the subdirectory which sits in the same directory as the com.cows.Moo class! But is your class being used from a jar file? Over the internet? Worry no more! This method will load your file through the same pipe that the class was loaded. In this example, call getResourceAsString( this , "data/mooing_cow.txt" );

Parameters:
obj - The address for the file you are looking for is relative to this object.

filename - The address for the file you are looking for relative to the given object.

Returns:
null if the file cannot be loaded.

Throws:
Exception

fileToVector

public static Vector fileToVector(String fileName)
                           throws IOException
All of a text file is read in and stored in a Vector.

Parameters:
fileName - The name of the text file to read in.

Returns:
The Vector where each object in the Vector contains one line from the text file.

Throws:
IOException -


fileToList

public static List fileToList(String fileName)
                       throws IOException
All of a text file is read in and stored in a Vector.

Parameters:
fileName - The name of the text file to read in.

Returns:
The Vector where each object in the Vector contains one line from the text file.

Throws:
IOException -


fileToArray

public static String[] fileToArray(String fileName)
                            throws IOException
All of a text file is read in and stored in a String array.

Parameters:
fileName - The name of the text file to read in.

Returns:
The String array where each String contains one line from the text file.

Throws:
IOException -


arrayToFile

public static void arrayToFile(String[] s,
                               String fileName)
                        throws IOException
Replaced with toTextFile().

Throws:
IOException

toTextFile

public static void toTextFile(List list,
                              String fileName)
                       throws IOException
Each element of a List has the toString() method performed on it and written to a text file.

Parameters:
list - The collection of objects to be written out.

fileName - The name of the text file to be created.

Throws:
IOException

toTextFile

public static void toTextFile(String[] s,
                              String fileName)
                       throws IOException
Each element of a string array is written to a text file.

Parameters:
s - The collection of objects to be written out.

fileName - The name of the text file to be created.

Throws:
IOException

delete

public static void delete(String fileName)
Delete a specific file.

If the file does not exist, nothing is done.


deleteAllFilesInDir

public static void deleteAllFilesInDir(String directoryName)
Delete all files in a directory.

All subdirectories are ignored.


deleteDirectory

public static void deleteDirectory(File dir)
                            throws IOException
Delete a directory structure.

Parameters:
dir - Directory to delete.
Throws:
IOException

rename

public static void rename(String source,
                          String dest)
Change the file name.

If dest already exists, it is deleted.


writeObject

public static boolean writeObject(String fileName,
                                  Object obj)
Write one object to a file.

If the file already exists, it is overwritten.

Returns:
true if file write successful.


readObject

public static Object readObject(String fileName)
Read one object from a file.

Returns:
object or null if file read unsuccessful.


fileToByteArrayE

public static byte[] fileToByteArrayE(String filename)
                               throws IOException
Read a file from the disk into a byte array.

Will throw an IOException if one is encountered.

Parameters:
filename - The name of the file to read.

Returns:
A byte array that is the exact same size as the file.

Throws:
IOException

fileToByteArray

public static byte[] fileToByteArray(String filename)
Read a file from the disk into a byte array.

If an IOException is encountered, a message is printed to STDOUT and null is returned.

Parameters:
filename - The name of the file to read.

Returns:
A byte array that is the exact same size as the file.


byteArrayToFile

public static void byteArrayToFile(byte[] data,
                                   String filename)
                            throws IOException
Given a byte array construct a file.

Parameters:
data - byte array
filename - filename (path) to output to
Throws:
IOException - thrown if the IO cannot be written

getDirectory

public static String[] getDirectory(File f)
Return a string array containing the current directory.

Directories will have angle brackets.

If there is a parent directory, the first entry will be a .. directory.


getAllFilesInDirectory

public static String[] getAllFilesInDirectory(String directory)
Return a string array containing the names of all the files (not directories) in the current directory.

If an invalid directory is passed in, or if the directory contains no valid files, an array of length zero is returned.


getDirectoryDetails

public static String[] getDirectoryDetails(File f)
Return a string array containing the current directory with timestamps and file sizes.

Directories will have angle brackets.

If there is a parent directory, the first entry will be a .. directory.


copy

public static void copy(InputStream in,
                        OutputStream out)
                 throws Exception
Throws:
Exception

appendText

public static boolean appendText(String filename,
                                 String text)

fileToUniqueArray

public static String[] fileToUniqueArray(String FileName)
                                  throws IOException
All of a text file is read in and unique Strings are stored in a String array.

Parameters:
FileName - The name of the text file to read in.

Returns:
The String array where each String contains one unique line from the text file.

Throws:
IOException -


fileToHashSet

public static HashSet fileToHashSet(String FileName)
                             throws IOException
All of a text file is read in and unique elements are stored in a HashSet.

Parameters:
FileName - The name of the text file to read in.

Returns:
The HashSet where each element contains one unique line from the text file.

Throws:
IOException -


MDi JLib
1.7.4

Copyright©2001-2007 MicroDeveloper, Inc. All Rights Reserved.