Skip to content

samitkumarpatel-work/java-nio

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

agenda

In this Java Chapter, we'll cover Java File IO functionalities and how they changed throughout different Java versions. First, we'll cover the java.io package from the initial Java version. Next, we'll go over java.nio package introduced in Java 1.4. In the end, we'll cover the java.nio.file package, commonly known as the NIO.2 package

java.io

  • The first File I/O was released with the java 1.0 with java.io package, introducing a File class to access the file system.
  • The File class represents files and directories and provides limited operations on the file system.
    File file = new File("src/test/resources/nio-vs-nio2.txt");
    FileInputStream in = new FileInputStream(file);
    
  • With Old java.io , It was possible to create and delete files, check if they exist, check read/write access, etc.
  • Examples:
    • Byte Stream
    FileInputStream in = new FileInputStream("xanadu.txt");
    FileOutputStream out = new FileOutputStream("outagain.txt");
    int c;
    while ((c = in.read()) != -1) {
      out.write(c);
    }
    
    • Character Stream
    FileReader inputStream = new FileReader("xanadu.txt");
    FileWriter outputStream = new FileWriter("characteroutput.txt");
    int c;
    while ((c = inputStream.read()) != -1) {
      outputStream.write(c);
    }
    
    • Buffer Stream
    inputStream = new BufferedReader(new FileReader("xanadu.txt"));
    outputStream = new BufferedWriter(new FileWriter("characteroutput.txt"));
    
    • Scanning & Formatting
    new Scanner(new BufferedReader(new FileReader("xanadu.txt")));
    
  • The limitation around java.io.File are :
    • Lack of copy method.
    • Bad error handling.
    • A limited set of file attributes.
    • Blocking API – our thread is blocked until the IO operation is complete.

java.nio

  • Was introduce in java 1.4

  • non-blocking I/O API

  • bundled in java.nio package (nio stands for new IO).

  • NIO was introduced to overcome the limitations of the java.io package.

  • This package introduced three core classes:

    • Channel
      • Java NIO Channel is a class that allows us to read and write to a buffer.
      • Channel class is similar to Streams (here we speak of IO Streams, not Java 1.8 Streams) with a couple of differences.
      • Channel is a two-way street while File I/O Streams are usually one-way, and they can read and write asynchronously.
      • There are couple implementations of the Channel class
        • FileChannel for file system read/write,
        • DatagramChannel for read/write over a network using UDP, and
        • SocketChannel for read/write over a network using TCP.
    • Buffer
      • Buffer is a block of memory from which we can read or write data into it.
      • NIO Buffer object wraps a memory block.
      • Buffer class provides a set of functionalities to work with the memory block.
      • To work with Buffer objects, we need to understand three major properties of the Buffer class:
        • capacity,
        • position, and
        • limit
    • Selector.
      • Java NIO Selector allows us to manage multiple channels with a single thread.
  • Limitation:

    • The changes java.nio package introduced is more related to low-level data IO. While they allowed non-blocking API, other aspects remained problematic:
      • Limited support for symbolic links
      • Limited support for file attributes access
      • Missing better file system management tools

java.nio (NIO.2)

  • java.nio 2.0 (JSR 203) was introduced with java 1.7
  • java.nio.file package got introduce also known as NIO.2 package.
  • This package follows an asynchronous approach to non-blocking IO which was not supported in java.nio package.
  • The most significant changes are related to high-level file manipulation. They are added with
    • Files,
    • Path, and
    • Paths classes.
  • along with that there are many more enhancement around NIO 2.0 api like :
    • AsynchronousFileChannel and
    • AsynchronousSocketChannel.

Lets deep dive around:

  • Path (interface)
  • Paths (class)
  • Files (class)
  • AsynchronousFileChannel
  • WatchService (Interface)
  • FileVisitor (Interface)
  • PathMatcher (Functional Interface)
  • During the demo will leverage junit with File I/O to test file system.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages