In Java, path.toFile() returns a java.io.File object for the same location. Use it when an older API, such as a library method or a Swing dialog, still expects a File.
The conversion only copies the path text; nothing is checked on disk. The opposite direction is file.toPath(), so you can move between the two worlds freely.
An instance method of the java.nio.file.Path interface, available since Java 7. Call it on a Path object, as in path.toFile(...), after import java.nio.file.Path;.
Syntax
Call it on a path of the default file system:
Java
Path p = Paths.get("docs", "report.txt");
File f = p.toFile(); // java.io.File for docs/report.txt
Path back = f.toPath(); // equal to p
Call
What it does
p.toFile()
A File for the same path text.
file.toPath()
Back from File to Path.
p.toFile().getName()
File methods return Strings instead of Paths.
Since Java 7; a default method of the interface from Java 11. Prefer the Files class for new code and convert only at the edges.
Outputs below are shown for Linux and macOS. On Windows, Java prints \ between names instead of /; the note above each output says when a result really differs.
Example 1 — Convert a Path and Read It with File Methods
Turn a relative path into a File, call a few File methods, and convert back.
Java
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathToFileBasic {
public static void main(String[] args) {
Path p = Paths.get("docs", "report.txt");
File f = p.toFile();
System.out.println("getName(): " + f.getName());
System.out.println("getParent(): " + f.getParent());
System.out.println("getPath(): " + f.getPath());
System.out.println("back to an equal Path: " + f.toPath().equals(p));
}
}
Output
Run with java PathToFileBasic. Shown for Linux and macOS. On Windows getPath() prints docs\report.txt; the other lines are the same.
getName(): report.txt
getParent(): docs
getPath(): docs/report.txt
back to an equal Path: true
How It Works
Same text, older type.toFile() wraps the path text in a File; nothing is created and the file does not need to exist.
File methods return Strings.getName() and getParent() mirror getFileName() and getParent() of Path, but give text, not paths.
A loss-free round trip.f.toPath() produces a Path equal to the original, so converting at the boundary of an old API costs nothing.
Example 2 — Pass Paths to a Helper That Only Accepts File
An older helper method takes a java.io.File. Keep using Path in your own code and convert with toFile() right at the call.
Java
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathToFileLegacy {
// An older helper that only accepts java.io.File
static String describe(File f) {
return f.getName() + " | parent=" + f.getParent() + " | absolute=" + f.isAbsolute();
}
public static void main(String[] args) {
Path[] paths = {
Paths.get("report.txt"),
Paths.get("docs", "report.txt"),
Paths.get("/srv/app/config.yml")
};
for (Path p : paths) {
System.out.println(describe(p.toFile()));
}
}
}
Output
Run with java PathToFileLegacy. Shown for Linux and macOS. On Windows the last line is config.yml | parent=\srv\app | absolute=false, because a path without a drive letter is not absolute there.
Convert at the boundary. The program builds every location as a Path and calls toFile() only in the argument of describe, the one place that needs the old type.
File mirrors Path, as text.getName(), getParent(), and isAbsolute() answer the same questions as the Path methods, but getParent() returns a String, or null for a bare name.
No disk access. None of these files exist and none need to: toFile() and the three File getters only look at the path text.
Example 3 — Get a Backup Name for a File Object
Old code hands you a File. Switch to Path for the path arithmetic, then return a File again.
Java
import java.io.File;
import java.nio.file.Path;
public class PathToFileRoundTrip {
public static void main(String[] args) {
File old = new File("data", "users.csv");
Path p = old.toPath();
Path backup = p.resolveSibling("users.bak");
File result = backup.toFile();
System.out.println("File to Path: " + p);
System.out.println("sibling: " + backup);
System.out.println("back to File: " + result.getName() + " in " + result.getParent());
System.out.println("separator: " + (File.separatorChar == '/' ? "slash" : "backslash"));
}
}
Output
Run with java PathToFileRoundTrip. Shown for Linux and macOS. On Windows the paths print as data\users.csv and data\users.bak, and the last line is separator: backslash.
File to Path: data/users.csv
sibling: data/users.bak
back to File: users.bak in data
separator: slash
How It Works
Into the Path world.old.toPath() turns the legacy File into a Path, so modern methods such as resolveSibling become available.
Path does the naming.resolveSibling("users.bak") keeps the data folder and swaps the file name, which is clumsy to do with File alone.
Back for the old API.toFile() hands a File back; File.separatorChar reveals the separator that both classes use on this system.
Frequently Asked Questions
Call path.toFile(). It returns a java.io.File for the same location.
Call file.toPath(). It has existed since Java 7.
The path belongs to another file system provider, for example a ZIP file system. A java.io.File can only represent files of the default file system.
Use Path with the Files class. Convert with toFile() only when an older API requires a File.
No. It only converts the path. Call exists() on the File or Files.exists on the Path to check.