Saturday 26 March 2016

Java NIO (NIO.2 Added to Java SE 7)


In Java 7, there are two important packages added for IO related operations. 

1) java.io.file
2) java.nio.file.attribute

java.nio.file : Let's have a look at classes and interfaces of this package first.


Path: This is an interface which represents a file or directory. It is similar to File class of java.io in this regard however it is quite a lot more powerful than a File.


Paths: This class has static methods for creating Path objects. It acts like a factory of object


Files: It has some static methods that work with path objects.


Similar to File, java.nio.Path represents only a location in the machine. You have to create a fille by using Files.createFile(Path target). 


We create a file using static method of File class which is : Files.createFiles(path);

Important Steps:

1. Path p1=Paths.get("H:\\Tutorials\\JavaNio");

      Path object can be created by calling static method get() of Paths Class
      Remember Path is an interface while Paths is a class

2.  We can also create Path object as : 
     
        Path p2
=Paths.get("H:", "Tutorials", "JavaNio");
        
Path p3= Paths.get("H:\\Tutorials", "JavaNio");

3. To create a file for a FTP path:


        Path ftpPath= Paths.get(URI.create("file:///H:/Tutorial/Tutorials"));

        Path ftpPath= Paths.get("file:///H:/Tutorial/Tutorials");

        If you use the code just agove for FTP protocol path, this will throw an Exception
        java.nio.file.InvalidPathException

4. How the Paths.get actually works, although you don't need to write that in your program.

        Path actualPath=FilesSystems.getDefault().getPath("H:", "Tutorials", "JavaNio");

5. File and Path can be converted to each other, hence you can use IO and NIO together.

        File file= new File("abc.txt");
    Path convertedToPath=file.toPath();
    Path path= Paths.get("H:\\abc.txt");
    File convertedToFile=path.toFile();

T0 Creating Files And Directories in NIO:



package com.bpjoshi.javapriest.iotutorials.nio2;

import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @author Bhagwati Prasad (Joshi)
 */
/*
 * Creating Files and Directories
 * 1: Creating Files , example nio.txt
 * 2: Creating Directories
 * creating a directory under JavaNio eg /books/java
 */
public class JavaNIO2 {
 public static void main(String[] args) {
  //1:
  Path path= Paths.get("H:", "Tutorials", "JavaNio", "nio.txt");
  System.out.println(Files.exists(path)); //Prints false
  try {
   Files.createFile(path);
  } catch (IOException e) {
   e.printStackTrace();
  }
  System.out.println(Files.exists(path));
  
  //2:
  Path path2=Paths.get("H://Tutorials/JavaNio/books/javabooks");
  Path file=Paths.get("H://Tutorials/JavaNio/books/javabooks/java.pdf");
  
  try {
   Files.createDirectories(path2);
   Files.createFile(file);
   //Create file in newly created Directory
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}



That's it for this tutorial. Please visit Java NIO - Part 2 for the next part of Java NIO Tutorial.

Happy Coding!

No comments:

Post a Comment

Please write your views