Creating a new Directory and File using Java NIO
Jun 27, 2020
Make sure to subscribe to our newsletter and be the first to know the news.
Jun 27, 2020
Make sure to subscribe to our newsletter and be the first to know the news.
In this tutorial we are going to learn how to create a new directory and file using Java NIO classes of Path and Files, if you are not aware of the methods available with these classes click here.
import java.nio.*;
import java.nio.file.*;
public class CreateDirectory
{
public static void main(String[] args)
{
try
{
Path path=Paths.get("D:\\Zartab\\Code with Z\\Java NIO");
Path p=Files.createDirectory(path);
System.out.println("Directory has been created at "+p.toString());
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
import java.nio.*;
import java.nio.file.*;
public class CreateFile
{
public static void main(String[] args)
{
try
{
Path path=Paths.get("data.txt");
Path p=Files.createFile(path);
System.out.println("File has been created at "+p.toString());
}
catch(Exception e)
{
e.printStackTrace();
}
}
}