Creating files and directories - Java File Path IO

Java examples for File Path IO:Directory Create

Description

Creating files and directories

Demo Code

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

public class Main {
  public static void main(String[] args) {
    try {/*from   www  . j a va 2 s. c om*/
      Path testDirectoryPath = Paths.get("C:/home/test");
      Path testDirectory = Files.createDirectory(testDirectoryPath);
      System.out.println("Directory created successfully!");
      Path newFilePath = FileSystems.getDefault().getPath(
          "C:/home/test/newFile.txt");
      Path testFile = Files.createFile(newFilePath);
      System.out.println("File created successfully!");
    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }
}

Related Tutorials