Top Description Fields Constructors Methods
org.apache.commons.io

public Class FileSystemUtils

extends Object
Class Inheritance
Imports
java.io.BufferedReader, .IOException, .InputStream, .InputStreamReader, .OutputStream, java.util.ArrayList, .Arrays, .List, .StringTokenizer

General File System utilities.

This class provides static utility methods for general file system functions not provided via the JDK File class.

The current functions provided are:

Authors
Frank W. Zammetti, Stephen Colebourne, Thomas Ledoux, James Urie, Magnus Grimsell
Since
Commons IO 1.1
Version
$Id: FileSystemUtils.java 453889 2006-10-07 11:56:25Z scolebourne $

Field Summary

Modifier and TypeField and Description
private static final int
INIT_PROBLEM

Operating system state flag for error.

private static final FileSystemUtils
INSTANCE

Singleton instance, used mainly for testing.

private static final int
OS

The operating system flag.

private static final int
OTHER

Operating system state flag for neither Unix nor Windows.

private static final int
POSIX_UNIX

Operating system state flag for Posix flavour Unix.

private static final int
UNIX

Operating system state flag for Unix.

private static final int
WINDOWS

Operating system state flag for Windows.

Constructor Summary

AccessConstructor and Description
public
FileSystemUtils()

Instances should NOT be constructed in standard programming.

Method Summary

Modifier and TypeMethod and Description
public static long

Returns:

the amount of free drive space on the drive or volume
freeSpace
(String
the path to get free space for, not null, not empty on Unix
path
)

Deprecated Use freeSpaceKb(String) Deprecated from 1.3, may be removed in 2.0
Returns the free space on a drive or volume by invoking the command line.
public static long

Returns:

the amount of free drive space on the drive or volume in kilobytes
freeSpaceKb
(String
the path to get free space for, not null, not empty on Unix
path
)

Returns the free space on a drive or volume in kilobytes by invoking the command line.

pack-priv long

Returns:

the amount of free drive space on the drive or volume
freeSpaceOS
(String
the path to get free space for, not null, not empty on Unix
path
,
int
the operating system code
os
,
boolean
whether to normalize to kilobytes
kb
)

Returns the free space on a drive or volume in a cross-platform manner.

pack-priv long

Returns:

the amount of free drive space on the volume
freeSpaceUnix
(String
the path to get free space for
path
,
boolean
whether to normalize to kilobytes
kb
,
boolean
whether to use the posix standard format flag
posix
)

Find free space on the *nix platform using the 'df' command.

pack-priv long

Returns:

the amount of free drive space on the drive
freeSpaceWindows
(String
the path to get free space for, including the colon
path
)

Find free space on the Windows platform using the 'dir' command.

pack-priv Process

Returns:

the process
openProcess
(String[]
the command line parameters
cmdAttribs
)

Opens the process to the operating system.

pack-priv long

Returns:

the number of bytes
parseBytes
(String
the free space string
freeSpace
,
String
the path
path
)

Parses the bytes from a string.

pack-priv long

Returns:

the number of bytes
parseDir
(String
the line to parse
line
,
String
the path that was sent
path
)

Parses the Windows dir response last line

pack-priv List<E>

Returns:

the parsed data
performCommand
(String[]
the command line parameters
cmdAttribs
,
int
The maximum limit for the lines returned
max
)

Performs the os command.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

INIT_PROBLEMback to summary
private static final int INIT_PROBLEM

Operating system state flag for error.

INSTANCEback to summary
private static final FileSystemUtils INSTANCE

Singleton instance, used mainly for testing.

OSback to summary
private static final int OS

The operating system flag.

OTHERback to summary
private static final int OTHER

Operating system state flag for neither Unix nor Windows.

POSIX_UNIXback to summary
private static final int POSIX_UNIX

Operating system state flag for Posix flavour Unix.

UNIXback to summary
private static final int UNIX

Operating system state flag for Unix.

WINDOWSback to summary
private static final int WINDOWS

Operating system state flag for Windows.

Constructor Detail

FileSystemUtilsback to summary
public FileSystemUtils()

Instances should NOT be constructed in standard programming.

Method Detail

freeSpaceback to summary
public static long freeSpace(String path) throws IOException

Deprecated

Use freeSpaceKb(String) Deprecated from 1.3, may be removed in 2.0

Returns the free space on a drive or volume by invoking the command line. This method does not normalize the result, and typically returns bytes on Windows, 512 byte units on OS X and kilobytes on Unix. As this is not very useful, this method is deprecated in favour of freeSpaceKb(String) which returns a result in kilobytes.

Note that some OS's are NOT currently supported, including OS/390, OpenVMS and and SunOS 5. (SunOS is supported by freeSpaceKb.)

FileSystemUtils.freeSpace("C:");       // Windows
FileSystemUtils.freeSpace("/volume");  // *nix
The free space is calculated via the command line. It uses 'dir /-c' on Windows and 'df' on *nix.
Parameters
path:String

the path to get free space for, not null, not empty on Unix

Returns:long

the amount of free drive space on the drive or volume

Exceptions
IOException:
if an error occurs when finding the free space
IllegalArgumentException:
if the path is invalid
IllegalStateException:
if an error occurred in initialisation
Since
Commons IO 1.1, enhanced OS support in 1.2 and 1.3
freeSpaceKbback to summary
public static long freeSpaceKb(String path) throws IOException

Returns the free space on a drive or volume in kilobytes by invoking the command line.

FileSystemUtils.freeSpaceKb("C:");       // Windows
FileSystemUtils.freeSpaceKb("/volume");  // *nix
The free space is calculated via the command line. It uses 'dir /-c' on Windows, 'df -kP' on AIX/HP-UX and 'df -k' on other Unix.

In order to work, you must be running Windows, or have a implementation of Unix df that supports GNU format when passed -k (or -kP). If you are going to rely on this code, please check that it works on your OS by running some simple tests to compare the command line with the output from this class. If your operating system isn't supported, please raise a JIRA call detailing the exact result from df -k and as much other detail as possible, thanks.

Parameters
path:String

the path to get free space for, not null, not empty on Unix

Returns:long

the amount of free drive space on the drive or volume in kilobytes

Exceptions
IOException:
if an error occurs when finding the free space
IllegalArgumentException:
if the path is invalid
IllegalStateException:
if an error occurred in initialisation
Since
Commons IO 1.2, enhanced OS support in 1.3
freeSpaceOSback to summary
pack-priv long freeSpaceOS(String path, int os, boolean kb) throws IOException

Returns the free space on a drive or volume in a cross-platform manner. Note that some OS's are NOT currently supported, including OS/390.

FileSystemUtils.freeSpace("C:");  // Windows
FileSystemUtils.freeSpace("/volume");  // *nix
The free space is calculated via the command line. It uses 'dir /-c' on Windows and 'df' on *nix.
Parameters
path:String

the path to get free space for, not null, not empty on Unix

os:int

the operating system code

kb:boolean

whether to normalize to kilobytes

Returns:long

the amount of free drive space on the drive or volume

Exceptions
IOException:
if an error occurs when finding the free space
IllegalArgumentException:
if the path is invalid
IllegalStateException:
if an error occurred in initialisation
freeSpaceUnixback to summary
pack-priv long freeSpaceUnix(String path, boolean kb, boolean posix) throws IOException

Find free space on the *nix platform using the 'df' command.

Parameters
path:String

the path to get free space for

kb:boolean

whether to normalize to kilobytes

posix:boolean

whether to use the posix standard format flag

Returns:long

the amount of free drive space on the volume

Exceptions
IOException:
if an error occurs
freeSpaceWindowsback to summary
pack-priv long freeSpaceWindows(String path) throws IOException

Find free space on the Windows platform using the 'dir' command.

Parameters
path:String

the path to get free space for, including the colon

Returns:long

the amount of free drive space on the drive

Exceptions
IOException:
if an error occurs
openProcessback to summary
pack-priv Process openProcess(String[] cmdAttribs) throws IOException

Opens the process to the operating system.

Parameters
cmdAttribs:String[]

the command line parameters

Returns:Process

the process

Exceptions
IOException:
if an error occurs
parseBytesback to summary
pack-priv long parseBytes(String freeSpace, String path) throws IOException

Parses the bytes from a string.

Parameters
freeSpace:String

the free space string

path:String

the path

Returns:long

the number of bytes

Exceptions
IOException:
if an error occurs
parseDirback to summary
pack-priv long parseDir(String line, String path) throws IOException

Parses the Windows dir response last line

Parameters
line:String

the line to parse

path:String

the path that was sent

Returns:long

the number of bytes

Exceptions
IOException:
if an error occurs
performCommandback to summary
pack-priv List<E> performCommand(String[] cmdAttribs, int max) throws IOException

Performs the os command.

Parameters
cmdAttribs:String[]

the command line parameters

max:int

The maximum limit for the lines returned

Returns:List<E>

the parsed data

Exceptions
IOException:
if an error occurs