Files I/O NIO Way

Since Java 7 a bit dusty I/O becomes a burden and new package java.nio.file has been introduced in the language. It defines classes to access files and file system. Lets have a look into some of them.

Path

The Path is a programmatic representation of a path in the file system separated by a specific separator. Path might have root . Root is file store hierarchical starting point. It representation is / in a Unix. Windows might have multiply roots, and usually it has C:\ root. You can easily create a Path by using using get methods from the Files class.

Path p1 = Paths . get ( "/tmp/" ); // Path to the Root Path p2 = Paths . get ( "/" ); Path p3 = Paths . get ( "C:\\Windows" ); Path p4 = Paths . get ( "C:" , "Windows" ); // p3 and p4 are identical

Path might be represented as URI:

Path uriPath = Paths . get ( URI . create ( "file:///Users" ));

The longest version for the following code:

// FileSystems.getDefault() - returns default file system Path path = FileSystems . getDefault (). getPath ( "/tmp/" );

All previous examples start from the root. Basically all of them might represent an absolute path (path in the file system). Path interface has 2 methods to convert relative path to the ‘absolute path’. Reference.

Path toAbsolutePath() - Returns a Path object representing the absolute path of this path.

Path toRealPath(LinkOption... options) throws IOException - Returns an absolute path represent the real path of the file located by this object. Options indicating how symbolic links are handled

// FileSystems.getDefault() - returns default file system Path path = Paths . get ( "usr" ); path . toAbsolutePath (); // the unix output might look like '/usr'

If you pass null into the Files.get(...) it will throw java.lang.NullPointerException

Files

New API introduces new Files class. It contains many convenient methods to work with files. Files works with the Path data representation. Lets start look into some of them.

If you have a very small file you can read it into the byte[] or String you can use the readAllBytes(Path) or readAllLines(Path, Charset) methods.

@Test public void crudNioFilesAPI () throws IOException { // "user.dir" - User working directory String workingDirectory = System . getProperty ( "user.dir" ); Path path = Paths . get ( workingDirectory , "test.txt" ); // Creates file 'test.txt' in the 'user.dir' Files . createFile ( path ); // In the real life example it is better to use // TETS_FILE_PAYLOAD.getBytes(StandardCharsets.UTF_8) // You can get system encoding - System.getProperty("file.encoding"); byte [] data = TETS_FILE_PAYLOAD . getBytes (); // Writes payload to the created file Files . write ( path , data ); // Reads data from the file byte [] readData = Files . readAllBytes ( path ); String stringData = new String ( readData ); assertEquals ( "Checks if data are the same" , TETS_FILE_PAYLOAD , stringData ); // Deletes file and returns true if the file exists boolean deleted = Files . deleteIfExists ( path ); assertTrue ( deleted ); }

The following example demonstrates only a tip of iceberg.

Legacy File I/O

The Files class has a number of method to support capabilities between Legacy I/0 and NIO. it provides methods with signature Files.new.... , For example Files.newBufferedWriter , Files.newBufferedReader .

@Test public void crudLegacyFilesAPI () throws IOException { // "user.dir" - User working directory String workingDirectory = System . getProperty ( "user.dir" ); Path path = Paths . get ( workingDirectory , "test.txt" ); // Creates file 'test.txt' in the 'user.dir' Files . createFile ( path ); // Writes payload to file try ( BufferedWriter bw = Files . newBufferedWriter ( path , StandardCharsets . UTF_8 )) { bw . write ( TETS_FILE_PAYLOAD ); } catch ( IOException ioex ) { fail ( ioex . getMessage ()); } // Reads data from the file StringBuilder stringData = new StringBuilder (); try ( BufferedReader br = Files . newBufferedReader ( path , StandardCharsets . UTF_8 )) { String line ; while (( line = br . readLine ()) != null ) { stringData . append ( line ); } } catch ( IOException ioex ) { fail ( ioex . getMessage ()); } assertEquals ( "Checks if data are the same" , TETS_FILE_PAYLOAD , stringData . toString ()); // Deletes file and returns true if the file exists boolean deleted = Files . deleteIfExists ( path ); assertTrue ( deleted ); }

The Path has method toPath() to convert file to path at the same time File contains a similar method toPath() to support Path .

Reference

Path, Absolute path, Relative path conceptions you can find on the wiki.

The Following are useful methods of the Files class.

static FileTime getLastModifiedTime(Path path, LinkOption... options) - Returns a file’s last modified time.

static UserPrincipal getOwner(Path path, LinkOption... options) - Returns the owner of a file.

static Path copy(Path source, Path target, CopyOption... options) - Copy a file to a target file.

You can find more details inside package javadoc.