public final class IoUtils extends Object
| Modifier and Type | Field and Description |
|---|---|
static int |
DEFAULT_COPY_SIZE
Size of preferred work buffer when reading / writing data to / from streams
|
static CopyOption[] |
EMPTY_COPY_OPTIONS |
static FileAttribute<?>[] |
EMPTY_FILE_ATTRIBUTES |
static LinkOption[] |
EMPTY_LINK_OPTIONS |
static OpenOption[] |
EMPTY_OPEN_OPTIONS |
static String |
EOL
The local O/S line separator
|
static List<String> |
WINDOWS_EXECUTABLE_EXTENSIONS |
static Set<StandardOpenOption> |
WRITEABLE_OPEN_OPTIONS
A
Set of StandardOpenOption-s that indicate an intent
to create/modify a file |
| Modifier and Type | Method and Description |
|---|---|
static String |
appendPathComponent(String prefix,
String component) |
static Boolean |
checkFileExists(Path path,
LinkOption... options)
Checks if a file exists - Note: according to the
Java tutorial - Checking a File or Directory:
The methods in the Path class are syntactic, meaning that they operate
on the Path instance.
|
static IOException |
closeQuietly(Closeable... closeables)
Closes a bunch of resources suppressing any
IOExceptions their
Closeable.close() method may have thrown |
static long |
copy(InputStream source,
OutputStream sink) |
static long |
copy(InputStream source,
OutputStream sink,
int bufferSize) |
static Path |
ensureDirectory(Path path,
LinkOption... options) |
static boolean |
followLinks(LinkOption... options) |
static byte[] |
getEOLBytes() |
static String |
getFileOwner(Path path,
LinkOption... options)
Get file owner.
|
static LinkOption[] |
getLinkOptions(boolean followLinks) |
static Set<PosixFilePermission> |
getPermissions(Path path,
LinkOption... options)
If the "posix" view is supported, then it returns
Files.getPosixFilePermissions(Path, LinkOption...), otherwise
uses the getPermissionsFromFile(File) method |
static Set<PosixFilePermission> |
getPermissionsFromFile(File f) |
static boolean |
isExecutable(File f) |
static boolean |
isWindowsExecutable(String fileName) |
static int |
read(InputStream input,
byte[] buffer)
Read as many bytes as possible until EOF or achieved required length
|
static int |
read(InputStream input,
byte[] buffer,
int offset,
int length)
Read as many bytes as possible until EOF or achieved required length
|
static List<String> |
readAllLines(BufferedReader reader)
Reads all lines until no more available
|
static List<String> |
readAllLines(BufferedReader reader,
int lineCountHint)
Reads all lines until no more available
|
static List<String> |
readAllLines(InputStream stream)
Reads all lines until no more available
|
static List<String> |
readAllLines(Reader reader) |
static List<String> |
readAllLines(URL url)
Reads all lines until no more available
|
static void |
readFully(InputStream input,
byte[] buffer)
Read the requested number of bytes or fail if there are not enough left.
|
static void |
readFully(InputStream input,
byte[] buffer,
int offset,
int length)
Read the requested number of bytes or fail if there are not enough left.
|
static void |
setPermissions(Path path,
Set<PosixFilePermission> perms)
If the "posix" view is supported, then it invokes
Files.setPosixFilePermissions(Path, Set), otherwise
uses the setPermissionsToFile(File, Collection) method |
static void |
setPermissionsToFile(File f,
Collection<PosixFilePermission> perms) |
static byte[] |
toByteArray(InputStream inStream) |
static PosixFilePermission |
validateExcludedPermissions(Collection<PosixFilePermission> perms,
Collection<PosixFilePermission> excluded) |
public static final OpenOption[] EMPTY_OPEN_OPTIONS
public static final CopyOption[] EMPTY_COPY_OPTIONS
public static final LinkOption[] EMPTY_LINK_OPTIONS
public static final FileAttribute<?>[] EMPTY_FILE_ATTRIBUTES
public static final int DEFAULT_COPY_SIZE
public static final String EOL
public static final Set<StandardOpenOption> WRITEABLE_OPEN_OPTIONS
Set of StandardOpenOption-s that indicate an intent
to create/modify a filepublic static byte[] getEOLBytes()
EOLpublic static LinkOption[] getLinkOptions(boolean followLinks)
public static long copy(InputStream source, OutputStream sink) throws IOException
IOExceptionpublic static long copy(InputStream source, OutputStream sink, int bufferSize) throws IOException
IOExceptionpublic static IOException closeQuietly(Closeable... closeables)
IOExceptions their
Closeable.close() method may have throwncloseables - The Closeables to closeIOException that occurred during closing
of a resource - if more than one exception occurred, they are added as
suppressed exceptions to the first oneThrowable.getSuppressed()public static boolean isWindowsExecutable(String fileName)
fileName - The file name to be evaluated - ignored if null/emptytrue if the file ends in one of the WINDOWS_EXECUTABLE_EXTENSIONSpublic static Set<PosixFilePermission> getPermissions(Path path, LinkOption... options) throws IOException
Files.getPosixFilePermissions(Path, LinkOption...), otherwise
uses the getPermissionsFromFile(File) methodpath - The Pathoptions - The LinkOptions to use when querying the permissionsSet of PosixFilePermissionIOException - If failed to access the file system in order to
retrieve the permissionspublic static Set<PosixFilePermission> getPermissionsFromFile(File f)
f - The File to be checkedSet of PosixFilePermissions based on whether
the file is readable/writable/executable. If so, then all the
relevant permissions are set (i.e., owner, group and others)public static boolean isExecutable(File f)
public static void setPermissions(Path path, Set<PosixFilePermission> perms) throws IOException
Files.setPosixFilePermissions(Path, Set), otherwise
uses the setPermissionsToFile(File, Collection) methodpath - The Pathperms - The Set of PosixFilePermissionsIOException - If failed to access the file systempublic static void setPermissionsToFile(File f, Collection<PosixFilePermission> perms)
f - The Fileperms - A Collection of PosixFilePermissions to set on it.
Note: the file is set to readable/writable/executable not only by the
owner if any of relevant the owner/group/others permission is setpublic static String getFileOwner(Path path, LinkOption... options) throws IOException
Get file owner.
path - The Pathoptions - The LinkOptions to use when querying the ownerIOException - If failed to access the file systemFiles.getOwner(Path, LinkOption...)public static Boolean checkFileExists(Path path, LinkOption... options)
Checks if a file exists - Note: according to the Java tutorial - Checking a File or Directory:
The methods in the Path class are syntactic, meaning that they operate on the Path instance. But eventually you must access the file system to verify that a particular Path exists, or does not exist. You can do so with the exists(Path, LinkOption...) and the notExists(Path, LinkOption...) methods. Note that !Files.exists(path) is not equivalent to Files.notExists(path). When you are testing a file's existence, three results are possible: - The file is verified to exist. - The file is verified to not exist. - The file's status is unknown. This result can occur when the program does not have access to the file. If both exists and notExists return false, the existence of the file cannot be verified.
path - The Path to be testedoptions - The LinkOptions to useBoolean.TRUE/Boolean.FALSE or null
according to the file status as explained abovepublic static void readFully(InputStream input, byte[] buffer) throws IOException
input - where to read input frombuffer - destinationIOException - if there is a problem reading the fileEOFException - if the number of bytes read was incorrectpublic static void readFully(InputStream input, byte[] buffer, int offset, int length) throws IOException
input - where to read input frombuffer - destinationoffset - initial offset into bufferlength - length to read, must be ≥ 0IOException - if there is a problem reading the fileEOFException - if the number of bytes read was incorrectpublic static int read(InputStream input, byte[] buffer) throws IOException
input - where to read input frombuffer - destinationIOException - if a read error occurspublic static int read(InputStream input, byte[] buffer, int offset, int length) throws IOException
input - where to read input frombuffer - destinationoffset - initial offset into bufferlength - length to read - ignored if non-positiveIOException - if a read error occurspublic static PosixFilePermission validateExcludedPermissions(Collection<PosixFilePermission> perms, Collection<PosixFilePermission> excluded)
perms - The current PosixFilePermissions - ignored if null/emptyexcluded - The permissions not allowed to exist - ignored if null/emptyPosixFilePermission - null
if no violating permission foundpublic static Path ensureDirectory(Path path, LinkOption... options)
path - The Path to checkoptions - The LinkOptions to use when checking if path is a directoryUnsupportedOperationException - if input path not a directorypublic static boolean followLinks(LinkOption... options)
options - The LinkOptions - OK if null/emptytrue if the link options are null/empty or do
not contain LinkOption.NOFOLLOW_LINKS, false otherwise
(i.e., the array is not empty and contains the special value)public static byte[] toByteArray(InputStream inStream) throws IOException
IOExceptionpublic static List<String> readAllLines(URL url) throws IOException
url - The URL to read fromList of lines in the same order as it was readIOException - If failed to read the linesreadAllLines(InputStream)public static List<String> readAllLines(InputStream stream) throws IOException
stream - The InputStream - Note: assumed to
contain UTF-8 encoded dataList of lines in the same order as it was readIOException - If failed to read the linesreadAllLines(Reader)public static List<String> readAllLines(Reader reader) throws IOException
IOExceptionpublic static List<String> readAllLines(BufferedReader reader) throws IOException
reader - The BufferedReader to read all linesList of lines in the same order as it was readIOException - If failed to read the linesreadAllLines(BufferedReader, int)public static List<String> readAllLines(BufferedReader reader, int lineCountHint) throws IOException
reader - The BufferedReader to read all lineslineCountHint - A hint as to the expected number of lines - non-positive
means unknown - in which case some initial default value will be used to
initialize the list used to accumulate the lines.List of lines in the same order as it was readIOException - If failed to read the linesCopyright © 2008–2017 The Apache Software Foundation. All rights reserved.