Package edu.uiuc.ncsa.security.core.util
Class FileUtil
- java.lang.Object
-
- edu.uiuc.ncsa.security.core.util.FileUtil
-
public class FileUtil extends Object
Utility do to certain common file operations. These are mostly aimed at smaller files. It uses NIO when possible.Created by Jeff Gaynor
on 1/26/21 at 7:10 AM
-
-
Constructor Summary
Constructors Constructor Description FileUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description protected static File
checkFile(String fileName)
This just checks various things about the file, such as does it exist, is it a file, can it be read? so the user gets a better experience about what might be wrong.static String
readFileAsBinary(String fileName)
Read a (possibly binary) file and convert the contents to a base64 escaped string.static List<String>
readFileAsLines(InputStream inputStream)
Reads anInputStream
as a set of lines.static List<String>
readFileAsLines(String fileName)
Read a (text) file in as a list of strings, one per line.static String
readFileAsString(InputStream inputStream)
Reads anInputStream
as a single string.static String
readFileAsString(String fileName)
Read a (text) file in as a long string.static void
writeFileAsBinary(String filename, String contents)
Compliment toreadFileAsBinary(String)
.static void
writeStringToFile(String filename, String contents)
Compliment toreadFileAsString(String)
, which writes the string to a file.
-
-
-
Method Detail
-
checkFile
protected static File checkFile(String fileName) throws FileNotFoundException, IllegalAccessException
This just checks various things about the file, such as does it exist, is it a file, can it be read? so the user gets a better experience about what might be wrong.- Parameters:
fileName
-- Returns:
- Throws:
FileNotFoundException
IllegalAccessException
-
readFileAsBinary
public static String readFileAsBinary(String fileName) throws Throwable
Read a (possibly binary) file and convert the contents to a base64 escaped string.- Parameters:
fileName
-- Returns:
- Throws:
Throwable
-
writeFileAsBinary
public static void writeFileAsBinary(String filename, String contents) throws Throwable
Compliment toreadFileAsBinary(String)
. This will take a base64 encoded string, decode it to a byte array and write the result to a file.- Parameters:
filename
-contents
-- Throws:
Throwable
-
readFileAsString
public static String readFileAsString(String fileName) throws Throwable
Read a (text) file in as a long string.- Parameters:
fileName
-- Returns:
- Throws:
Throwable
-
readFileAsString
public static String readFileAsString(InputStream inputStream) throws Throwable
Reads anInputStream
as a single string. This is useful when reading a resource as an input stream.- Parameters:
inputStream
-- Returns:
- Throws:
Throwable
-
readFileAsLines
public static List<String> readFileAsLines(InputStream inputStream) throws Throwable
Reads anInputStream
as a set of lines. This is useful when reading a resource as an input stream.- Parameters:
inputStream
-- Returns:
- Throws:
Throwable
-
readFileAsLines
public static List<String> readFileAsLines(String fileName) throws Throwable
Read a (text) file in as a list of strings, one per line.- Parameters:
fileName
-- Returns:
- Throws:
Throwable
-
writeStringToFile
public static void writeStringToFile(String filename, String contents) throws Throwable
Compliment toreadFileAsString(String)
, which writes the string to a file.- Parameters:
filename
-contents
-- Throws:
Throwable
-
-