public class

FileUtils

extends Object
implements Serializable
java.lang.Object
   ↳ com.microstrategy.web.app.utils.FileUtils

Class Overview

Title: FileUtils class Description: Includes generic functions to use when accessing files and directories. All methods are Static so no class instance is required to access them. Copyright: Copyright (c) 2001 Company: MicroStrategy, Inc.

Summary

Public Constructors
FileUtils()
Public Methods
static String cleanFileName(String s)
Produces a clean form of a file name (typically from an object name, such as the name of a report).
static boolean fileExists(String fileName)
Verifies if the file can be accessed.
static long fileSize(String fileName)
Given the file path and name, searches for the file and returns its size
static FileContentsObjectList getObjectFileContents(String fileName)
Opens the indicated file.
static String getTextFileContents(String fileName, boolean newLine)
Obtains the contents of the requested file
static String getTextFileContents(String fileName)
Opens the indicated file.
static String getTextFileContents(String fileName, boolean newLine, String encoding, String newLineChar)
Obtains the contents of the requested file
static String getTextFileContents(String fileName, boolean newLine, String encoding)
Obtains the contents of the requested file
static String getTextFileContentsWithoutBom(String fileName, boolean newLine)
Obtains the contents of the requested file, if encoded in UTF-8, delete the BOM
static void saveObjectFileContents(String fileName, FileContentsObjectList oInfo, boolean append)
Writes to the indicated file.
static void setObjectFileContents(String fileName, FileContentsObjectList oInfo, boolean append)
Saves the oInfo collection into the indicated file.
static void setObjectFileContents(String fileName, FileContentsObjectList oInfo)
Saves the collection sent as parameter into the indicated file.
static void setTextFileContents(String fileName, String fileContents, boolean append)
Opens the requested file as a new one (overwrite) or appending the contents if requested.
static void setTextFileContents(String fileName, String fileContents, boolean changeLine, boolean append)
Opens the requested file as a new one (overwrite) or appending the contents if requested.
static void setTextFileContents(String fileName, String fileContents)
Saves the string sent as parameter into the indicated file.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public FileUtils ()

Public Methods

public static String cleanFileName (String s)

Produces a clean form of a file name (typically from an object name, such as the name of a report). This is really just a method to replace a predefined set of characters ('%', '.', '/', '\\', ':', '*', '?', '"', '<', '>', '|', ',') in a string with another character ('_').

Parameters
s This is the incoming string that may have special characters in it.
Returns
  • the String with all special characters replaced.

public static boolean fileExists (String fileName)

Verifies if the file can be accessed.

Parameters
fileName Name of the file to search for.
Returns
  • true if the file is found, otherwise returns false

public static long fileSize (String fileName)

Given the file path and name, searches for the file and returns its size

Parameters
fileName path and file name to search for
Returns
  • the size of the file. If the file was not found or there was an error while loading it, returns 0.

public static FileContentsObjectList getObjectFileContents (String fileName)

Opens the indicated file. Reads its contents retrieving them as objects.

Parameters
fileName Name of the file from where the contents are to be extracted.
Returns
  • the Object that includes all the information found on the file.
Throws
WebAppException if any error happens while accessing the file.

public static String getTextFileContents (String fileName, boolean newLine)

Obtains the contents of the requested file

Parameters
fileName name of the file to search for
newLine if it should insert a change of line each time it finds one on the file
Returns
  • a String with the file contents
Throws
WebAppException if an error happened while reading the file.

public static String getTextFileContents (String fileName)

Opens the indicated file. Starts reading it line by line and storing its content on a buffer. When the end of the file is reached, the buffer is copied into a String which is the output of this method. Handles java.io.FileNotFoundException if the file requested is not found. Catches Exception if any other error happens while reading the file contents.

Parameters
fileName Name of the file from where the contents are to be extracted.
Returns
  • String with the file contents on it (including line breaks)
Throws
WebAppException if any error happens while accessing the file.

public static String getTextFileContents (String fileName, boolean newLine, String encoding, String newLineChar)

Obtains the contents of the requested file

Parameters
fileName name of the file to search for
newLine if it should insert a change of line each time it finds one on the file
encoding encoding type
newLineChar the new line separator
Returns
  • a String with the file contents
Throws
WebAppException if an error happened while reading the file.

public static String getTextFileContents (String fileName, boolean newLine, String encoding)

Obtains the contents of the requested file

Parameters
fileName name of the file to search for
newLine if it should insert a change of line each time it finds one on the file
encoding encoding type
Returns
  • a String with the file contents
Throws
WebAppException if an error happened while reading the file.

public static String getTextFileContentsWithoutBom (String fileName, boolean newLine)

Obtains the contents of the requested file, if encoded in UTF-8, delete the BOM

Parameters
fileName name of the file to search for
newLine if it should insert a change of line each time it finds one on the file
Returns
  • a String with the file contents
Throws
WebAppException if an error happened while reading the file.

public static void saveObjectFileContents (String fileName, FileContentsObjectList oInfo, boolean append)

Writes to the indicated file. Receives a FileContentsObjectList collection where all the information to be saved is found. It parses each one of the elements of the list and writes it to the file.

Parameters
fileName Name of the file from where the contents are to be extracted.
oInfo FileContentsObjectList collection with the contents to be written to the file
append Boolean indicating if the file should be appended or overwritten *
Throws
WebAppException if any error happens while accessing the file.

public static void setObjectFileContents (String fileName, FileContentsObjectList oInfo, boolean append)

Saves the oInfo collection into the indicated file. By default it overwrites the file if already existing. Used as interface for saveObjectFileContents.

Parameters
fileName Name of the file where to write.
oInfo FileContentsObjectList instance to write back to the file
append Boolean indicating if the file should be appended or overwritten
Throws
WebAppException if any error happens while accessing the file.

public static void setObjectFileContents (String fileName, FileContentsObjectList oInfo)

Saves the collection sent as parameter into the indicated file. By default it overwrites the file if already existing. Used as interface for saveObjectFileContents.

Parameters
fileName Name of the file where to write.
oInfo FileContentsObjectList instance to write back to the file
Throws
WebAppException if any error happens while accessing the file.

public static void setTextFileContents (String fileName, String fileContents, boolean append)

Opens the requested file as a new one (overwrite) or appending the contents if requested. Writes the string and closes the file. By default changes line after inserting contents.

Parameters
fileName Name of the file where to write.
fileContents String to write to the file.
append Boolean indicating if the file is to be appended or overwritten
Throws
WebAppException if any error happens while accessing the file.

public static void setTextFileContents (String fileName, String fileContents, boolean changeLine, boolean append)

Opens the requested file as a new one (overwrite) or appending the contents if requested. Writes the string and closes the file.

Parameters
fileName Name of the file where to write.
fileContents String to write to the file.
changeLine Boolean indicating if a change of line should be inserted after the contents or not
append Boolean indicating if the file is to be appended or overwritten
Throws
WebAppException if any error happens while accessing the file.

public static void setTextFileContents (String fileName, String fileContents)

Saves the string sent as parameter into the indicated file. By default it overwrites the file if already existing. Used as interface for saveTextFileContents. By default appends an end of line after inserting the contents.

Parameters
fileName Name of the file where to write.
fileContents String to write to the file
Throws
WebAppException if any error happens while accessing the file.