Class IO
- java.lang.Object
-
- org.sosy_lab.common.io.IO
-
public final class IO extends Object
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
appendToFile(Path file, Charset charset, Object content)
Appends content to a file (without overwriting the file, but creating it if necessary).static void
checkReadableFile(Path path)
Verifies if a file exists, is a normal file and is readable.static Charset
getNativeCharset()
Return theCharset
that is used by the underlying platform, i.e., configured as the OS default.static boolean
mayUseColorForOutput()
Determine whether it is advisable to use color (via escape sequences) for output on stdout/stderr.static Writer
openOutputFile(Path file, Charset charset, OpenOption... options)
Open a buffered Writer to a file.static char[]
toCharArray(CharSource source)
Read the full content of aCharSource
to a char array.static StringBuilder
toStringBuilder(CharSource source)
Read the full content of aCharSource
to a newStringBuilder
.static void
writeFile(Path file, Charset charset, Object content)
Writes content to a file.static void
writeGZIPFile(Path file, Charset charset, Object content)
Writes content to a file compressed in GZIP format.
-
-
-
Method Detail
-
getNativeCharset
public static Charset getNativeCharset()
Return theCharset
that is used by the underlying platform, i.e., configured as the OS default. On modern systems this is typicallyCharsets.UTF_8
with the exception of Windows.The native encoding should typically used when outputting something to the console (which is handled by Java automatically) and is also the common agreement when processes are communicating via pipes, i.e., for
Process.getInputStream()
,Process.getOutputStream()
, andProcess.getErrorStream()
.Note that Java 17 also provides
Console#charset()
which should always return the same value, this method is a convenience method for simplifying code that has to work with both old and new Java versions.Before Java 18 the method
Charset.defaultCharset()
also returns the native charset, but it was changed to return UTF-8 in Java 18.If the charset of the JVM is overwritten by setting some system properties like
file.encoding
this method may return the specified charset instead of the "real" native charset (just likeCharset.defaultCharset()
already did).More information can be found in JEP 400.
-
mayUseColorForOutput
public static boolean mayUseColorForOutput()
Determine whether it is advisable to use color (via escape sequences) for output on stdout/stderr. This method checks for output redirection, the OS, and the NO_COLOR environment variable.
-
toStringBuilder
public static StringBuilder toStringBuilder(CharSource source) throws IOException
Read the full content of aCharSource
to a newStringBuilder
.- Throws:
IOException
-
toCharArray
public static char[] toCharArray(CharSource source) throws IOException
Read the full content of aCharSource
to a char array.- Throws:
IOException
-
writeFile
public static void writeFile(Path file, Charset charset, Object content) throws IOException
Writes content to a file.- Parameters:
file
- The file.content
- The content which shall be written.- Throws:
IOException
-
writeGZIPFile
public static void writeGZIPFile(Path file, Charset charset, Object content) throws IOException
Writes content to a file compressed in GZIP format.- Parameters:
file
- The file.content
- The content which shall be written.- Throws:
IOException
-
openOutputFile
public static Writer openOutputFile(Path file, Charset charset, OpenOption... options) throws IOException
Open a buffered Writer to a file. This method creates necessary parent directories beforehand.- Throws:
IOException
-
appendToFile
public static void appendToFile(Path file, Charset charset, Object content) throws IOException
Appends content to a file (without overwriting the file, but creating it if necessary).- Parameters:
file
- The file.content
- The content which will be written to the end of the file.- Throws:
IOException
-
checkReadableFile
public static void checkReadableFile(Path path) throws FileNotFoundException
Verifies if a file exists, is a normal file and is readable. If this is not the case, a FileNotFoundException with a nice message is thrown.- Parameters:
path
- The file to check.- Throws:
FileNotFoundException
- If one of the conditions is not true.
-
-