Class FileIoUtil
java.lang.Object
com.github.hypfvieh.util.FileIoUtil
-
Method Summary
Modifier and TypeMethodDescriptiongetTextfileFromUrl(String _url) Retrives a text file from an given URL.getTextfileFromUrl(String _url, Charset _charset) Retrives a text file from an given URL and reads the content with the given charset.getTextfileFromUrl(String _url, Charset _charset, boolean _silent) static StringguessLineTerminatorOfFile(String _file) Tries to find the line termination character(s) of the given file.static PropertiesloadPropertiesFromClasspath(String _propertiesFile) Loads the contents of a properties file located in the Java classpath into aPropertiesobject.static booleanloadPropertiesFromClasspath(String _propertiesFile, Properties _properties) Same asloadPropertiesFromClasspath(String)but does not throw checked exception.static InputStreamopenInputStreamForFile(String _fileNameWithPath, SearchOrder... _searchOrder) Tries to find _fileNameWithPath in either classpath, systempath or absolute path and opens anInputStreamfor it.readFileFrom(String _fileName, Charset _charset, SearchOrder... _searchOrder) Read a file from different sources depending on _searchOrder.static StringreadFileFromClassPath(String _fileName) Reads a file from classpath to String.readFileFromClassPathAsList(String _fileName) Reads a file from classpath to a list of String using default charset and log any exception.readFileFromClassPathAsList(String _fileName, Charset _charset, boolean _silent) Reads a file from classpath to a list of String with the given charset.readFileToList(File _file) Reads a file to a List<String> (each line is one entry in list).readFileToList(String _fileName) Reads a file to a List<String> (each line is one entry in list).static StringreadFileToString(File _file) static StringreadFileToString(String _file) Reads a file and returns it's content as string.static PropertiesreadProperties(File _file) Trys to read a properties file.static PropertiesreadProperties(InputStream _stream) Tries to read a properties file from an inputstream.static booleanreadPropertiesBoolean(Properties _props, String _property) Reads a property as boolean from an properties object.static PropertiesreadPropertiesFromFile(String _fileName, Properties _props) Read properties from given filename (returns emptyPropertiesobject on failure).static StringreadStringFromResources(InputStream _stream, String _charset) Read a Resource-file (eg.readTextFileFromStream(InputStream _input, Charset _charset, boolean _silent) Reads a text file from givenInputStreamusing the givenCharset.static booleanwriteProperties(File _file, Properties _props) Writes a properties Object to file.static booleanwriteProperties(String _propertiesFile, Properties _props) Write a properties object to a given file (will overwrite existing files!).static booleanwriteTextFile(String _fileName, String _fileContent, boolean _append) Write String to file with the java default charset (usually UTF-8).static booleanwriteTextFile(String _fileName, String _fileContent, Charset _charset, boolean _append) Write String to file with the given charset.
-
Method Details
-
readProperties
Trys to read a properties file. Returns null if properties file could not be loaded- Parameters:
_file-- Returns:
- Properties Object or null
-
readProperties
Tries to read a properties file from an inputstream.- Parameters:
_stream-- Returns:
- properties object/null
-
readPropertiesFromFile
Read properties from given filename (returns emptyPropertiesobject on failure).- Parameters:
_fileName- The properties file to read_props- optional properties object, if null a new object created in the method- Returns:
Propertiesobject
-
writeProperties
Writes a properties Object to file. Returns true on success, false otherwise.- Parameters:
_file-_props-- Returns:
- true on success, false otherwise
-
writeProperties
Write a properties object to a given file (will overwrite existing files!).- Parameters:
_propertiesFile- - The filename you want to use_props- - The properties to save- Returns:
- boolean true on success, false on exception
-
readPropertiesBoolean
Reads a property as boolean from an properties object. Returns true if read property matches 1, yes, true, enabled, on, y- Parameters:
_props-_property-- Returns:
-
getTextfileFromUrl
Retrives a text file from an given URL. Url could be remote (like http://) or local (file://) If protocol is not specified by "protocol://" (like "http://" or "file://"), "file://" is assumed.- Parameters:
_url-- Returns:
- fileContent as List or null if file is empty or an error occoured
-
getTextfileFromUrl
Retrives a text file from an given URL and reads the content with the given charset. Url could be remote (like http://) or local (file://) If protocol is not specified by "protocol://" (like "http://" or "file://"), "file://" is assumed.- Parameters:
_url-_charset-- Returns:
- fileContent as List or null if file is empty or an error occurred.
-
getTextfileFromUrl
-
readTextFileFromStream
public static List<String> readTextFileFromStream(InputStream _input, Charset _charset, boolean _silent) Reads a text file from givenInputStreamusing the givenCharset.- Parameters:
_input- stream to read_charset- charset to use_silent- true to disable exception logging, false otherwise- Returns:
- List of string or null on error
-
readFileToString
-
readFileToList
-
readFileToList
-
readFileToString
-
readStringFromResources
Read a Resource-file (eg. Text) to String.- Parameters:
_stream-_charset-- Returns:
- the file as string, or null on error
-
loadPropertiesFromClasspath
Loads the contents of a properties file located in the Java classpath into aPropertiesobject.- Parameters:
_propertiesFile- properties file located in Java classpath- Returns:
- properties object
- Throws:
IOException- if file not found or loading fails for other I/O reasons
-
loadPropertiesFromClasspath
Same asloadPropertiesFromClasspath(String)but does not throw checked exception. The returned boolean indicates if loading was successful. Read properties are stored in the given properties object (should never be null!).- Parameters:
_propertiesFile-_properties-- Returns:
- true if properties could be loaded, false otherwise
-
readFileFromClassPath
-
readFileFromClassPathAsList
-
readFileFromClassPathAsList
public static List<String> readFileFromClassPathAsList(String _fileName, Charset _charset, boolean _silent) Reads a file from classpath to a list of String with the given charset. Will optionally suppress any exception logging.- Parameters:
_fileName- file to read_charset- charset to use for reading_silent- true to suppress error logging, false otherwise- Returns:
- file contents or null on exception
-
writeTextFile
public static boolean writeTextFile(String _fileName, String _fileContent, Charset _charset, boolean _append) Write String to file with the given charset. Optionally appends the data to the file.- Parameters:
_fileName- the file to write_fileContent- the content to write_charset- the charset to use_append- append content to file, if false file will be overwritten if existing- Returns:
- true on successful write, false otherwise
-
writeTextFile
Write String to file with the java default charset (usually UTF-8). Optionally appends the data to the file.- Parameters:
_fileName- the file to write_fileContent- the content to write_append- append content to file, if false file will be overwritten if existing- Returns:
- true on successful write, false otherwise
-
readFileFrom
public static List<String> readFileFrom(String _fileName, Charset _charset, SearchOrder... _searchOrder) Read a file from different sources depending on _searchOrder. Will return the first successfully read file which can be loaded either from custom path, classpath or system path.- Parameters:
_fileName- file to read_charset- charset used for reading_searchOrder- search order- Returns:
- List of String with file content or null if file could not be found
-
openInputStreamForFile
public static InputStream openInputStreamForFile(String _fileNameWithPath, SearchOrder... _searchOrder) Tries to find _fileNameWithPath in either classpath, systempath or absolute path and opens anInputStreamfor it. Search order is specified bySearchOrdervarargs.- Parameters:
_fileNameWithPath-_searchOrder-- Returns:
- InputStream for the file or null if file could not be found
-
guessLineTerminatorOfFile
-