public class

IO

extends Object
java.lang.Object
   ↳ com.pnfsoftware.jeb.util.IO

Class Overview

This utility class contains useful static file manipulation routines.

Summary

Public Constructors
IO()
Public Methods
static boolean compressFolder(String folderPath, String zipfilePath)
Compress a folder and its contents to a zip archive.
static boolean createDirectory(String path)
Create a directory.
static boolean createDirectory(File f)
Create a directory.
static File createTempFolder(String name)
Create a directory in the default temporary-file directory.
static boolean deleteDirectory(File dir)
Delete a directory and its contents recursively.
static boolean deleteDirectory(String path)
Delete a directory and its contents recursively.
static void deleteDirectoryOnExit(File dir)
Delete a folder and all its contents recursively, when the virtual machine terminates.
static String expandPath(String path)
Perform expansion of certain shell artifacts used in path names.
static void extractToFolder(File inputZipFile, File outputFolder)
Extract the contents of a zip archive to a target folder.
static byte getFirstByte(String path)
Get the first byte of a file.
static int getFirstIntLE(String path)
Get the first 4-byte of a file, read as a little-endian integer.
static short getFirstShortLE(String path)
Get the first 2-byte of a file, read as a little-endian short.
static File getParentFile2(File file)
A safer implementation of getParentFile().
static boolean isFile(String path)
Determine if a path refers to an existing file.
static boolean isZIPFile(String path)
Determine whether a file appears to be a ZIP archive.
static List<String> listFiles(String path)
Recursively do a directory listing.
static byte[] readFile(File file)
Read the contents of a binary files.@return
static byte[] readFile(File file, long maxAllowedSize)
Read the contents of a binary file.@return
static byte[] readFile(String path)
Read the contents of a binary file.@return
static byte[] readFileSafe(File file)
Read a binary file safely.
static String readInputLineSafe()
Read a line from the standard input.
static byte[] readInputStream(InputStream in)
Read and reliably return all bytes of an input stream.
static List<String> readLines(File file, Charset encoding)
Read the lines of a text file.
static List<String> readLines(File file)
Read the lines of a UTF8 encoded text file.
static List<String> readLines(InputStream input, Charset encoding)
Read the contents of a stream as a line of strings.
static List<String> readLines(InputStream input)
Read the contents of a stream as a text buffer of UTF-8 encoded strings.@return
static void writeFile(File file, byte[] data, boolean createDirs)
Write data to a file whose containing directory structure may not exist.
static void writeFile(File file, byte[] data)
Write data to a file.
static void writeFile(File file, byte[] data, int offset, int size, boolean createDirs)
Write data to a file whose containing directory structure may not exist.
static void writeFile(File file, byte[] data, int offset, int size)
Write data to a file.
static boolean writeFileSafe(File file, byte[] data, int offset, int size, boolean createDirs)
Write a binary file safely.
static boolean writeFileSafe(File file, byte[] data, boolean createDirs)
Write a binary file safely.
static void writeLines(OutputStream output, List<? extends CharSequence> lines)
Write strings, encoded in UT8.
static void writeLines(OutputStream output, List<? extends CharSequence> lines, Charset encoding)
Write strings.
static boolean writeLines(File file, List<? extends CharSequence> lines, Charset encoding)
Write strings to a text file.
static boolean writeLines(File file, List<? extends CharSequence> lines)
Write strings to a text file, encoded as UTF8.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public IO ()

Public Methods

public static boolean compressFolder (String folderPath, String zipfilePath)

Compress a folder and its contents to a zip archive.

Parameters
folderPath input folder
zipfilePath output zip file path
Returns
  • true on success

public static boolean createDirectory (String path)

Create a directory.

Returns
  • true on success, else false

public static boolean createDirectory (File f)

Create a directory.

public static File createTempFolder (String name)

Create a directory in the default temporary-file directory.

Parameters
name folder name prefix
Returns
  • a File representing the newly created directory, or null on error
Throws
IOException if an IO exception happened

public static boolean deleteDirectory (File dir)

Delete a directory and its contents recursively.

Returns
  • true on success

public static boolean deleteDirectory (String path)

Delete a directory and its contents recursively.

Returns
  • true on success

public static void deleteDirectoryOnExit (File dir)

Delete a folder and all its contents recursively, when the virtual machine terminates.

Parameters
dir folder to be deleted

public static String expandPath (String path)

Perform expansion of certain shell artifacts used in path names.

Currently, the expansions performed are:

  • a leading tilda is expanded to a user's home directory

Returns
  • a full path name, if possible - otherwise, the original path is returned

public static void extractToFolder (File inputZipFile, File outputFolder)

Extract the contents of a zip archive to a target folder.

Parameters
inputZipFile input zip file path
outputFolder output folder directory
Throws
IOException if an error occurred; the current state of extraction within the target folder is not erased

public static byte getFirstByte (String path)

Get the first byte of a file. This method never throws exceptions. If an error occurs, 0 is returned. It is up to the client to handle that special case.

public static int getFirstIntLE (String path)

Get the first 4-byte of a file, read as a little-endian integer. This method never throws exceptions. If an error occurs, 0 is returned. It is up to the client to handle that special case.

Parameters
path file path
Returns
  • the first int, 0 on error

public static short getFirstShortLE (String path)

Get the first 2-byte of a file, read as a little-endian short. This method never throws exceptions. If an error occurs, 0 is returned. It is up to the client to handle that special case.

public static File getParentFile2 (File file)

A safer implementation of getParentFile(). If the input file is a single-level relative path (eg, "abc"), this method will first retrieve the absolute path (relative to the current folder) before trying to retrieve the parent folder.

Parameters
file file or folder
Returns
  • null if there is no parent folder, eg in the case of a a root folder

public static boolean isFile (String path)

Determine if a path refers to an existing file.

public static boolean isZIPFile (String path)

Determine whether a file appears to be a ZIP archive. This method never throws exceptions.

public static List<String> listFiles (String path)

Recursively do a directory listing. The list returned is a list of true files (not directories).

Returns
  • the list of absolute file paths

public static byte[] readFile (File file)

Read the contents of a binary files.@return

Throws
IOException

public static byte[] readFile (File file, long maxAllowedSize)

Read the contents of a binary file.@return

Parameters
maxAllowedSize specify a maximum read size
Throws
IOException on IO error, or if the read size exceeds

public static byte[] readFile (String path)

Read the contents of a binary file.@return

Throws
IOException

public static byte[] readFileSafe (File file)

Read a binary file safely. If any error happens, an empty array is returned.

Parameters
file input file
Returns
  • the bytes in the file, never null (empty array on error)

public static String readInputLineSafe ()

Read a line from the standard input. This method is safe to use; it does not raise, instead returning a null string on error.

public static byte[] readInputStream (InputStream in)

Read and reliably return all bytes of an input stream. The caller is responsible for closing the input stream after reading.@return

Throws
IOException

public static List<String> readLines (File file, Charset encoding)

Read the lines of a text file.

Returns
  • a list of lines without newline chars, null on error

public static List<String> readLines (File file)

Read the lines of a UTF8 encoded text file.

Returns
  • a list of lines without newline chars, null on error

public static List<String> readLines (InputStream input, Charset encoding)

Read the contents of a stream as a line of strings.

Parameters
input the input stream
encoding the stream encoding
Returns
  • a list of strings, without new-line characters
Throws
IOException on IO error

public static List<String> readLines (InputStream input)

Read the contents of a stream as a text buffer of UTF-8 encoded strings.@return

Throws
IOException

public static void writeFile (File file, byte[] data, boolean createDirs)

Write data to a file whose containing directory structure may not exist. Existing file contents will be overwritten.

Throws
IOException

public static void writeFile (File file, byte[] data)

Write data to a file. Existing file contents will be overwritten.

Parameters
file destination file
data binary data to write
Throws
IOException on error

public static void writeFile (File file, byte[] data, int offset, int size, boolean createDirs)

Write data to a file whose containing directory structure may not exist. Existing file contents will be overwritten.

Parameters
createDirs create the intermediate directories if need be
Throws
IOException

public static void writeFile (File file, byte[] data, int offset, int size)

Write data to a file. Existing file contents will be overwritten.

Throws
IOException

public static boolean writeFileSafe (File file, byte[] data, int offset, int size, boolean createDirs)

Write a binary file safely.

public static boolean writeFileSafe (File file, byte[] data, boolean createDirs)

Write a binary file safely.

public static void writeLines (OutputStream output, List<? extends CharSequence> lines)

Write strings, encoded in UT8.

Throws
IOException

public static void writeLines (OutputStream output, List<? extends CharSequence> lines, Charset encoding)

Write strings.

Throws
IOException

public static boolean writeLines (File file, List<? extends CharSequence> lines, Charset encoding)

Write strings to a text file.

Returns
  • success indicator

public static boolean writeLines (File file, List<? extends CharSequence> lines)

Write strings to a text file, encoded as UTF8.

Returns
  • success indicator