utils
Class FileManager

java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by javax.servlet.http.HttpServlet
          extended by utils.FileManager
All Implemented Interfaces:
Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

public class FileManager
extends javax.servlet.http.HttpServlet
implements javax.servlet.Servlet

Servlet implementation class for Servlet: FileManager

Author:
patrick lawler
See Also:
Serialized Form

Nested Class Summary
 class FileManager.NodeInfo
          An object to hold information for a node.
 class FileManager.NodeInfoComparator
          Comparator that compares NodeInfo objects.
 
Constructor Summary
FileManager()
           
 
Method Summary
 void copy(File src, File dest)
          Copies the given File src to the given File dest.
protected  void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
           
protected  void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
           
 File generateUniqueFile(File parent, String ext)
          Given a parent directory File and a file extension String generates and returns a File with a unique filename.
 String getFileExtension(String filename)
          Given a String filename, returns the String extension.
 String getFileText(File file)
          Given a File, reads the text from the file as a String and returns the string.
 String getUniqueFileNamePrefix(File parent)
          Get a file name prefix that has not been used yet
 void writeFile(File file, String data, boolean overwrite)
          Given a File the file to write to and the String data to write to that file and a boolean overwrite, indicating whether the file should be overwritten if it exists, writes the data to the file specified.
 void writeFile(String path, String data, boolean overwrite)
          Given a String path to the file to write to and the String data to write to that file and a boolean overwrite, indicating whether the file should be overwritten if it exists, writes the data to the file specified.
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPut, doTrace, getLastModified, service, service
 
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.servlet.Servlet
destroy, getServletConfig, getServletInfo, init, service
 

Constructor Detail

FileManager

public FileManager()
Method Detail

doGet

protected void doGet(javax.servlet.http.HttpServletRequest request,
                     javax.servlet.http.HttpServletResponse response)
              throws javax.servlet.ServletException,
                     IOException
Overrides:
doGet in class javax.servlet.http.HttpServlet
Throws:
javax.servlet.ServletException
IOException

doPost

protected void doPost(javax.servlet.http.HttpServletRequest request,
                      javax.servlet.http.HttpServletResponse response)
               throws javax.servlet.ServletException,
                      IOException
Overrides:
doPost in class javax.servlet.http.HttpServlet
Throws:
javax.servlet.ServletException
IOException

writeFile

public void writeFile(File file,
                      String data,
                      boolean overwrite)
               throws IOException
Given a File the file to write to and the String data to write to that file and a boolean overwrite, indicating whether the file should be overwritten if it exists, writes the data to the file specified.

Parameters:
File - file
String - data
boolean - overwrite
Throws:
IOException
IOException

writeFile

public void writeFile(String path,
                      String data,
                      boolean overwrite)
               throws IOException
Given a String path to the file to write to and the String data to write to that file and a boolean overwrite, indicating whether the file should be overwritten if it exists, writes the data to the file specified.

Parameters:
File - file
String - data
boolean - overwrite
Throws:
IOException
IOException

getFileText

public String getFileText(File file)
                   throws IOException
Given a File, reads the text from the file as a String and returns the string.

Parameters:
File - file
Returns:
String text
Throws:
IOException
IOException

getFileExtension

public String getFileExtension(String filename)
Given a String filename, returns the String extension. If the filename has no extension, returns the filename.

Parameters:
filename -
Returns:
String - extension

generateUniqueFile

public File generateUniqueFile(File parent,
                               String ext)
Given a parent directory File and a file extension String generates and returns a File with a unique filename.

Parameters:
parent -
ext -
Returns:
File

getUniqueFileNamePrefix

public String getUniqueFileNamePrefix(File parent)
Get a file name prefix that has not been used yet

Parameters:
parent - the directory where we want to search files
Returns:
a String containing a file name prefix that is not being used by any other files. e.g. 'node_2'

copy

public void copy(File src,
                 File dest)
          throws FileNotFoundException,
                 IOException
Copies the given File src to the given File dest. If they are directories, recursively copies the contents of the directories.

Parameters:
File - src
File - dest
Throws:
FileNotFoundException
IOException


Copyright © 2012. All Rights Reserved.