Class IO


  • public final class IO
    extends Object
    Provides helper functions for file access, in addition to Files and MoreFiles.
    • Method Detail

      • getNativeCharset

        public static Charset getNativeCharset()
        Return the Charset that is used by the underlying platform, i.e., configured as the OS default. On modern systems this is typically Charsets.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(), and Process.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 like Charset.defaultCharset() already did).

        More information can be found in JEP 400.

      • 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.