Java

Creating a new Directory and File using Java NIO

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.

CreateDirectory.java

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();
        }
    }
}

CreateFile.java

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();
        }
    }
}

Related Posts

Table Of Contents

;