Convert a Path to a File - Java File Path IO

Java examples for File Path IO:Path

Introduction

A Path can also be converted to a File object using the toFile() method.

This a bridge between Path and File.

Demo Code

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
  public static void main(String[] args) {
    Path path = Paths.get("/folder1/folder2/folder4", "test.txt");

    // convert path to File object
    File path_to_file = path.toFile();
    Path file_to_path = path_to_file.toPath();
    System.out.println("Path to file name: " + path_to_file.getName());
    System.out.println("File to path: " + file_to_path.toString());
  }//from w w w . jav a 2  s.c o  m
}

Result


Related Tutorials