File Handling in Java using NIO package
Let us first study what NIO is and how can we use it for File Handling in Java. So basically, Java has provided us with another I/O system called the NIO which stands for NEW I/O. There are three standard fundamental components of the NIO, they are the
- Channels and Buffers
- Selectors
- Non-blocking I/O
There are quite a few File Handling methods in Java using the NIO package. Let us glide through a few along with a small example of its implementation.
- File.exists(): Checks whether the file (path of the file) exists in the System or not.
- File.createDirectory() : Creates a new directory from the file path instance.
- File.copy() : Copies a file from one path to another.
- Files.move() : Moves the file to another destination.
- Files.delete(): Deletes the file from the system when specified in the file path.
Create a file in Java using NIO package
import java.io.IOException; import java.nio.file.Files; //importing the nio package import java.nio.file.Path; import java.nio.file.Paths; public class Create { //Creating a file public static void main() { Path path = Paths.get("E:NEWfile.txt"); //initializing the Path object try { //creating a file Path createdFilePath = Files.createFile(path); //Created a file path System.out.println("Created a file at : "+createdFilePath); //printing the path of the new file that has been created } catch (IOException e) { e.printStackTrace(); } } }
Move a file from one directory to another in Java using NIO package
import java.io.IOException; import java.nio.file.Files; // importing the nio package import java.nio.file.Path; import java.nio.file.Paths; public class Move { //Moving a file public static void main() { Path sourcePath = Paths.get("data/logging-copy.properties"); //defining the source of the file Path destinationPath = Paths.get("data/subdir/logging-moved.properties"); //defining the destination of the file try { Files.move(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { e.printStackTrace(); //moving file failed. } }
Copying a file and printing its location
package com.test.examples; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; /** * Java Files copy file example * * */ public class FilesCopyFileExample { public static void main(String[] args) { Path sourcePath = Paths.get("D:/data/sourceFile.txt"); Path targetPath = Paths.get("D:/data/DestinationFile.txt"); try { Path path = Files.copy(sourcePath, targetPath,StandardCopyOption.REPLACE_EXISTING);//copy with REPLACE_EXISTING option System.out.println("Target file Path : "+path); System.out.println("Copied Content : \n"+new String(Files.readAllBytes(path))); } catch (Exception e) { e.printStackTrace(); } } }
Moving a file using the NIO package of Java
package com.test.examples; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; /** * Java Files move file example * * */ public class FilesMoveFileExample { public static void main(String[] args) { Path sourcePath = Paths.get("D:/data/sourceFile.txt"); Path targetPath = Paths.get("D:/data/DestinationFile.txt"); try { Path path = Files.move(sourcePath, targetPath,StandardCopyOption.REPLACE_EXISTING);//move with REPLACE_EXISTING option System.out.println("Target file Path : "+path); System.out.println("Moved Content : \n"+new String(Files.readAllBytes(path))); } catch (Exception e) { e.printStackTrace(); } } }
Deleting a file using the Java NIO package
// Java program to demonstrate delete using Files class import java.io.IOException; import java.nio.file.*; public class Test { public static void main(String[] args) { try { Files.deleteIfExists(Paths.get("C:\\Users\\AyushSanghavi\\Desktop\\ TestData.txt")); } catch(NoSuchFileException e) { System.out.println("No such file/directory exists"); } catch(DirectoryNotEmptyException e) { System.out.println("Directory is not empty."); } catch(IOException e) { System.out.println("permissions denied."); } System.out.println("Deletion is successful."); } }
Let me know if you need more File Handling programs using the NIO package in JAVA in the comments sections below.
Check these:
Leave a Reply