A logo showing the text blog.marcnuri.com
Español
Home»Java»How to convert File to Path and Path to File in Java

Recent Posts

  • Windows 11: How to enable the built-in Administrator account
  • Bash: How to wait until a file exists
  • Gatsby 5: How to query data from multiple GraphQL sources
  • Bash: How to iterate through a list of strings
  • Kubernetes Client for Java: How to set up the underlying HTTP client

Categories

  • Front-end
  • Java
  • JavaScript
  • Legacy
  • Operations
  • Personal
  • Pet projects
  • Tools

Archives

  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • December 2019
  • October 2019
  • September 2019
  • July 2019
  • March 2019
  • November 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • December 2017
  • July 2017
  • December 2015
  • November 2015
  • November 2008
  • November 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007

How to convert File to Path and Path to File in Java

2023-08-22 in Java tagged Java / Quickie by Marc Nuri | Last updated: 2023-08-22
Versión en Español

Java introduced the Path interface in Java 7 (1.7). It is part of the Non-blocking IO (java.nio) package which was introduced with the J2SE 1.4 release to complement the standard I/O API.

The File class is part of the standard I/O API (java.io). As Java programmers we will often need to convert from one to the other.

The following sections show you how.

How to convert File to Path

The File class has a method toPath() that returns a Path object representing the path of the file.

File file = new File("/path/to/file.txt");
Path path = file.toPath();

How to convert Path to File

The Path interface has a method toFile() that returns a File object representing the path of the file.

Path path = Paths.get("/path/to/file.txt");
File file = path.toFile();
Twitter iconFacebook iconLinkedIn iconPinterest iconEmail icon

Post navigation

← JUnit 5 - How to disable or ignore testsHow to get the temporary directory path in Java →
© 2007 - 2023 Marc Nuri