Connect between java.io.File and java.nio.file.Files in Java

Description

The following code shows how to connect between java.io.File and java.nio.file.Files.

Example


//from www  .j a v a  2s .  co  m
import java.io.File;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {

  public static void main(String[] args) throws Exception {
    Path path = Paths.get(new URI("file:///C:/home/docs/users.txt"));
    File file = new File("C:\\home\\docs\\users.txt");
    Path toPath = file.toPath();
    System.out.println(toPath.equals(path));
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    I/O »




Binary File
Byte Array
CharSet
Checksum
Console
Create Copy Move Delete
Directory
Drive
Encode Decode
File Attribute
File Lock
File System
GZIP
Jar File
NIO Buffer
Path
Scanner
StreamTokenizer
Temporary File
Text File
Zip