Managing temporary files and directories - Java File Path IO

Java examples for File Path IO:Temporary File

Description

Managing temporary files and directories

Demo Code

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

public class Main {
  public static void main(String[] args) {
    try {/*from  www  .jav  a 2s . c om*/
      Path rootDirectory = FileSystems.getDefault().getPath("C:/home/docs");
      Path tempDirectory = Files.createTempDirectory(rootDirectory, "");
      System.out.println("Temporary directory createdsuccessfully!");

      String dirPath = tempDirectory.toString();
      System.out.println(dirPath);
      Path tempFile = Files.createTempFile(tempDirectory, "", "");
      System.out.println("Temporary file createdsuccessfully!");

      String filePath = tempFile.toString();
      System.out.println(filePath);
    } catch (IOException e) {
      System.out.println("IO Exception.");
    }

  }
}

Related Tutorials