Package de.julielab.java.utilities
Class FileUtilities
- java.lang.Object
-
- de.julielab.java.utilities.FileUtilities
-
public class FileUtilities extends Object
This class is a collection of useful file-related static methods. Refer to the JavaDoc of the methods for more details.- Author:
- faessler
-
-
Constructor Summary
Constructors Constructor Description FileUtilities()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidaddFileToJarOutputStream(File source, StringBuilder rootPath, JarOutputStream target)Adds a file entry to a JarOutputStream.static voidcreateJarFile(File outputFile, File... files)Creates a JAR file with the given files as content.static InputStreamfindResource(String name)Tries to find a resource by the given name.static BufferedInputStreamgetInputStreamFromFile(File file)Returns anInputStreamfor file.static BufferedOutputStreamgetOutputStreamToFile(File file)Returns anOutputStreamfor file.static BufferedReadergetReaderFromFile(File file)Returns a reader from the file file where the file may be a regular file or gzipped.static BufferedWritergetWriterToFile(File file)Returns a writer to the file file where the destination file may will be gzipped if file has the extension .gz or .gzip.
-
-
-
Method Detail
-
getInputStreamFromFile
public static BufferedInputStream getInputStreamFromFile(File file) throws IOException
Returns anInputStreamfor file. Automatically wraps in anBufferedInputStreamand also in anGZIPInputStreamif the file name ends with .gz or .gzip.- Parameters:
file- The file to read.- Returns:
- A buffered input stream.
- Throws:
IOException- If there is an error during reading.
-
getOutputStreamToFile
public static BufferedOutputStream getOutputStreamToFile(File file) throws IOException
Returns anOutputStreamfor file. Automatically wraps in anBufferedOutputStreamand also in anGZIPOutputStreamif the file name ends with .gz or .gzip.- Parameters:
file- The file to write.- Returns:
- A buffered output stream.
- Throws:
IOException- If there is an error during stream creation.
-
getReaderFromFile
public static BufferedReader getReaderFromFile(File file) throws IOException
Returns a reader from the file file where the file may be a regular file or gzipped. The gzip format is recognized by the file extensions .gz or .gzip.- Parameters:
file- The file to read.- Returns:
- A reader for file.
- Throws:
IOException- If opening the file fails.
-
getWriterToFile
public static BufferedWriter getWriterToFile(File file) throws IOException
Returns a writer to the file file where the destination file may will be gzipped if file has the extension .gz or .gzip.- Parameters:
file- The file to write.- Returns:
- A writer for file.
- Throws:
IOException- If opening the file fails.
-
createJarFile
public static void createJarFile(File outputFile, File... files) throws IOException
Creates a JAR file with the given files as content.- Parameters:
outputFile- The destination where the JAR file should be written.files- The files to be included into the JAR file.- Throws:
IOException- If writing the JAR file fails.
-
addFileToJarOutputStream
public static void addFileToJarOutputStream(File source, StringBuilder rootPath, JarOutputStream target) throws IOException
Adds a file entry to a JarOutputStream. Source: https://stackoverflow.com/questions/1281229/how-to-use-jaroutputstream-to-create-a-jar-file- Parameters:
source- The file that should be added into the JAR.rootPath-target- The JAR file to addsourceto.- Throws:
IOException- If adding the file fails.
-
findResource
public static InputStream findResource(String name) throws IOException
Tries to find a resource by the given name. The name may be a path to a regular file, an URI or a classpath resource.- Parameters:
name- The resource name to find.- Returns:
- The input stream from the found resource or null if the resource could not be found.
- Throws:
IOException- If reading the resource fails.
-
-