edu.stanford.hci.r3.util.files
Class FileUtils

java.lang.Object
  extended by edu.stanford.hci.r3.util.files.FileUtils

public class FileUtils
extends java.lang.Object

Utilities for manipulating Paths, Files, Directories, etc.

This software is distributed under the BSD License.


Nested Class Summary
static class FileUtils.FileChooserType
          Either show a save or open dialog.
 
Constructor Summary
FileUtils()
           
 
Method Summary
static void copy(java.io.File srcFileOrDir, java.io.File targetFileOrDir)
           
static void copy(java.io.File sourceFileOrDir, java.io.File targetFileOrDir, boolean visibleFilesOnly)
          A flexible copy function.
static javax.swing.JFileChooser createNewFileChooser(java.lang.String[] extensions)
           
static void downloadUrlToFile(java.net.URL url, java.io.File result)
           
static java.lang.String getCurrentTimeForUseInAFileName()
           
static java.lang.String getCurrentTimeForUseInASortableFileName()
           
static boolean isHiddenOrDotFile(java.io.File possiblyHiddenFile)
           
static java.util.List<java.io.File> listVisibleDirs(java.io.File path)
          Return only directories (that are children of the given path) that are not hidden.
static java.util.List<java.io.File> listVisibleFiles(java.io.File path, java.lang.String... extensionFilter)
          Return only files (that are children of the given path) that are not hidden.
static java.util.List<java.io.File> listVisibleFilesRecursively(java.io.File path)
           
static java.util.List<java.io.File> listVisibleFilesRecursively(java.io.File path, java.lang.String[] extensionFilter)
           
static java.lang.StringBuilder readFileIntoStringBuffer(java.io.File f)
          Reads an entire file into the StringBuilder (faster than StringBuffer).
static java.lang.StringBuilder readFileIntoStringBuffer(java.io.File f, boolean separateWithNewLines)
          Includes the workaround for bug in setting the user.dir: http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=4117557
static java.io.File showDirectoryChooser(java.awt.Component parent, java.lang.String title)
          Only allows directory selection.
static java.io.File showFileChooser(java.io.File initialPath, FileUtils.FileChooserType type, java.awt.Component parent, java.lang.String[] extensions, java.lang.String title)
           
static void sortByLastModified(java.util.List<java.io.File> files, SortDirection direction)
           
static void sortPhotosByCaptureDate(java.util.List<java.io.File> files, SortDirection direction)
          For JPEGs....
static void writeStringToFile(java.lang.String string, java.io.File destFile)
          Includes workaround for bug: http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=4117557
static void writeStringToFileOnlyIfNew(java.lang.String string, java.io.File file)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUtils

public FileUtils()
Method Detail

copy

public static void copy(java.io.File srcFileOrDir,
                        java.io.File targetFileOrDir)

copy

public static void copy(java.io.File sourceFileOrDir,
                        java.io.File targetFileOrDir,
                        boolean visibleFilesOnly)
                 throws java.io.IOException
A flexible copy function. It will do slightly different things depending on what is passed into the parameters. It can copy a file to another file, into a directory, or a directory into another directory.

Parameters:
sourceFileOrDir -
targetFileOrDir -
b -
Throws:
java.io.IOException

createNewFileChooser

public static javax.swing.JFileChooser createNewFileChooser(java.lang.String[] extensions)
Parameters:
extensions - should NOT have a . in front of them... i.e., xml, and NOT .xml
Returns:

downloadUrlToFile

public static void downloadUrlToFile(java.net.URL url,
                                     java.io.File result)
                              throws java.io.IOException
Parameters:
url -
result -
Throws:
java.io.IOException

getCurrentTimeForUseInAFileName

public static java.lang.String getCurrentTimeForUseInAFileName()
Returns:
The current time, with symbols replaced with underscores, so that we can use it in file names. This is great for logs that have to be tagged with dates.

getCurrentTimeForUseInASortableFileName

public static java.lang.String getCurrentTimeForUseInASortableFileName()
Returns:

isHiddenOrDotFile

public static boolean isHiddenOrDotFile(java.io.File possiblyHiddenFile)
Parameters:
possiblyHiddenFile -
Returns:
if the file is hidden (either hidden flag, or name starts with a dot)

listVisibleDirs

public static java.util.List<java.io.File> listVisibleDirs(java.io.File path)
Return only directories (that are children of the given path) that are not hidden.

Parameters:
path -
Returns:

listVisibleFiles

public static java.util.List<java.io.File> listVisibleFiles(java.io.File path,
                                                            java.lang.String... extensionFilter)
Return only files (that are children of the given path) that are not hidden. TODO/BUG: Doesn't use FileExcludeHiddenFilter??

Parameters:
path -
extensionFilter -

listVisibleFilesRecursively

public static java.util.List<java.io.File> listVisibleFilesRecursively(java.io.File path)
Parameters:
path -
Returns:

listVisibleFilesRecursively

public static java.util.List<java.io.File> listVisibleFilesRecursively(java.io.File path,
                                                                       java.lang.String[] extensionFilter)
Parameters:
path -
extensionFilter -
Returns:
a List of Files (guaranteed to be files, because if it's a dir, it will drill down)

readFileIntoStringBuffer

public static java.lang.StringBuilder readFileIntoStringBuffer(java.io.File f)
Reads an entire file into the StringBuilder (faster than StringBuffer).

Parameters:
f -
Returns:
the StringBuilder containing all the data

readFileIntoStringBuffer

public static java.lang.StringBuilder readFileIntoStringBuffer(java.io.File f,
                                                               boolean separateWithNewLines)
Includes the workaround for bug in setting the user.dir: http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=4117557

Parameters:
f - turn this file into a big string buffer (StringBuilder for efficiency)
separateWithNewLines -
Returns:

showDirectoryChooser

public static java.io.File showDirectoryChooser(java.awt.Component parent,
                                                java.lang.String title)
Only allows directory selection.

Returns:
the chosen directory, or null if the user canceled.

showFileChooser

public static java.io.File showFileChooser(java.io.File initialPath,
                                           FileUtils.FileChooserType type,
                                           java.awt.Component parent,
                                           java.lang.String[] extensions,
                                           java.lang.String title)
Parameters:
initialPath -
type -
parent -
extensions -
title -
Returns:

sortByLastModified

public static void sortByLastModified(java.util.List<java.io.File> files,
                                      SortDirection direction)
Parameters:
files -

sortPhotosByCaptureDate

public static void sortPhotosByCaptureDate(java.util.List<java.io.File> files,
                                           SortDirection direction)
For JPEGs.... This is a little bit slow, as it takes 4.3 seconds for about 670 files. Can we speed this up through a cache? After implementing the cache, it takes about 875 ms to sort 670 files. This is because we only read each file once, and save the timestamp in memory.

Parameters:
files -
direction -

writeStringToFile

public static void writeStringToFile(java.lang.String string,
                                     java.io.File destFile)
Includes workaround for bug: http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=4117557

Parameters:
string -
destFile -

writeStringToFileOnlyIfNew

public static void writeStringToFileOnlyIfNew(java.lang.String string,
                                              java.io.File file)
Parameters:
string -
file -

Copyright 2006 Stanford University