The newer version of the Java I/O system is called NIO (short for New I/O), it supports a buffer-oriented channel-based approach to I/O operations. The NIO system offers enchanced support for file-handling and file system features. However better NIO developers seem to still like the older I/O, but I will give a brief introduction and some examples in this section.
Firstly lets see the NIO classes
Package | Purpose |
java.nio | top-level for the NIO system, encapsulates various types of buffers that contain data operated upon the NIO system |
java,nio.channels | channels are open I/O connections |
java.nio.channels.spi | support for service providers for channels |
java.nio.charset | encapsulates character set, also support encoders and decoders that convert characters to bytes and bytes to characetrs. |
java.nio.charset.spi | supports service providers for character sets |
java.nio.file | provides support for files |
java.nio.file.attribute | provides support for file attributes |
java.nio.file.spi | supports service providers for file system |
The NIO system is built on item items, buffers and channels, you open a channel and use buffers to hold the data
Two other entities used by the NIO system are charsets and selectors
From Java 7 enchancements were added Path Interface, Files Class and File Attribute Interfaces and the FileSystem, Filesystems and FileStore Classes
NIO file attributes example | Path filepath = Paths.get("src/NIOTest1.java"); System.out.println("File Name: " + filepath.getName(1)); System.out.println("Path: " + filepath); System.out.println("Absolute Path: " + filepath.toAbsolutePath()); System.out.println("Parent: " + filepath.getParent()); if(Files.exists(filepath)) System.out.println("File exists"); else System.out.println("File does not exist"); try { if(Files.isHidden(filepath)) System.out.println("File is hidden"); else System.out.println("File is not hidden"); } catch(IOException e) { System.out.println("I/O Error: " + e); } Files.isWritable(filepath); System.out.println("File is writeable"); Files.isReadable(filepath); System.out.println("File is readable"); |
NIO writing and reading a file example | // Writing to a file using a try-with-resources block. try ( FileChannel fChan = (FileChannel) Files.newByteChannel(Paths.get("NIOtest.txt"), StandardOpenOption.WRITE, StandardOpenOption.CREATE) ) { // Create a buffer. ByteBuffer mBuf = ByteBuffer.allocate(26); // Write some bytes to the buffer. for(int i=0; i < 26; i++) mBuf.put((byte)('A' + i)); // Reset the buffer so that it can be written. mBuf.rewind(); // Write the buffer to the output file. fChan.write(mBuf); } catch(InvalidPathException e) { System.out.println("Path Error " + e); } catch (IOException e) { System.out.println("I/O Error: " + e); System.exit(1); } // Read a file using a try-with-resources block. try ( FileChannel fChan = (FileChannel) Files.newByteChannel(Paths.get("NIOtest.txt")) ) { // Get the size of the file. long fSize = fChan.size(); // Now, map the file into a buffer. MappedByteBuffer mBuf = fChan.map(FileChannel.MapMode.READ_ONLY, 0, fSize); // Read and display bytes from buffer. for(int i=0; i < fSize; i++) System.out.print((char)mBuf.get()); System.out.println(); } catch(InvalidPathException e) { System.out.println("Path Error " + e); } catch (IOException e) { System.out.println("I/O Error " + e); } |
NIO using walkFileTree example | // Create a custom version of SimpleFileVisitor that overrides the visitFile( ) method. class MyFileVisitor extends SimpleFileVisitor<Path> { public FileVisitResult visitFile(Path path, BasicFileAttributes attribs) throws IOException { System.out.println(path); return FileVisitResult.CONTINUE; } } Main ---------------------------------------------- String dirname = "src"; System.out.println("Directory tree starting with " + dirname + ":\n"); try { Files.walkFileTree(Paths.get(dirname), new MyFileVisitor()); } catch (IOException exc) { System.out.println("I/O Error"); } |
There are many more areas in NIO to explore and I will leave you to learn and play around with the NIO system.