public class FileUtils extends Object
Modifier and Type | Class | Description |
---|---|---|
protected static interface |
FileUtils.FileCallback |
Callback handler for
EachFile(java.io.File[], nl.nikhef.jgridstart.osutils.FileUtils.FileCallback) |
Constructor | Description |
---|---|
FileUtils() |
Modifier and Type | Method | Description |
---|---|---|
static boolean |
chmod(File file,
boolean read,
boolean write,
boolean exec,
boolean ownerOnly) |
Change file permissions
|
static boolean |
CopyFile(File in,
File out) |
Copy a file from one place to another, retaining permisions.
|
static void |
CopyFiles(File[] fromFiles,
File toPath) |
Copies a list of files to a directory.
|
static File |
createTempDir(String prefix) |
|
static File |
createTempDir(String prefix,
File directory) |
Create a temporary directory with read-only permissions.
|
protected static void |
EachFile(File[] fromFiles,
FileUtils.FileCallback callback) |
Runs a callback on each file specified.
|
static int |
Exec(String[] cmd) |
Execute a command and return the exit code
|
static int |
Exec(String[] cmd,
String input,
StringBuffer output) |
Execute a command, enter input on stdin, and return the exit code while storing stdout and stderr.
|
static File[] |
listFilesOnly(File path) |
List ordinary files in a directory.
|
static void |
MoveFiles(File[] fromFiles,
File toPath) |
Moves a list of files to a directory.
|
static String |
readFile(File file) |
Return the contents of a text file as String
|
static void |
recursiveDelete(File what) |
Helper method: remove directory recursively.
|
static void |
writeFile(File file,
String data) |
Write a String to a text file, possibly overwriting it.
|
protected static Logger logger
public static boolean CopyFile(File in, File out) throws IOException
This calls an external copy program on the operating system to make sure it is properly copied and permissions are retained.
It is strongly advised to make sure the destination file does not exist already.
true
if file was copied, false
if no files was copiedIOException
- on error, as far as it can be detected from the utilitiespublic static File[] listFilesOnly(File path)
path
- Directory to list files inFile.listFiles()
public static void CopyFiles(File[] fromFiles, File toPath) throws IOException
When an error occurs, files already copied are removed again and an IOException is thrown.
It is discouraged to have files with the same name existing already in the destination path.
IOException
public static void MoveFiles(File[] fromFiles, File toPath) throws IOException
When an error occurs, files already moved are put back and an IOException is thrown.
It is discouraged to have files with the same name existing already in the destination path.
IOException
public static void recursiveDelete(File what)
Beware, symlinks might traverse into unwanted territory!
protected static void EachFile(File[] fromFiles, FileUtils.FileCallback callback) throws IOException
Implements a rollback mechanism: when the callback throws an IOException,
the previous operations are undone by means of the FileUtils.FileCallback.reverseAction(java.io.File)
method.
IOException
public static String readFile(File file) throws IOException
IOException
public static void writeFile(File file, String data) throws IOException
IOException
public static boolean chmod(File file, boolean read, boolean write, boolean exec, boolean ownerOnly)
Not supported natively until java 1.6. Bad Java.
Note that the ownerOnly argument differs from Java. When ownerOnly
is
true for Java's File.setReadable(boolean, boolean)
, File.setWritable(boolean, boolean)
or
File.setExecutable(), the other/group permissions are left as they are.
This method resets them instead. When ownerOnly is false, behaviour is
as Java's.
file
- File to set permissions onread
- if reading should be allowedwrite
- if writing should be allowedexec
- if executing should be allowedownerOnly
- true to set group&world permissions to none,
false to set them all alikepublic static File createTempDir(String prefix, File directory) throws IOException
TODO Current code contains a race-condition. Please see Sun bug 4735419 for a better solution.
IOException
public static File createTempDir(String prefix) throws IOException
IOException
public static int Exec(String[] cmd, String input, StringBuffer output) throws IOException
The process must stop waiting for input by itself, and not rely on its stdin being closed. This doesn't work on Windows, so the process will never terminate.
cmd
- command to runinput
- String to feed to process's stdinoutput
- String to which stdout and stderr is appended, or nullIOException
public static int Exec(String[] cmd) throws IOException
IOException
Copyright © 2010-2018 Nikhef / Stichting FOM. All Rights Reserved.