Construct a File Path in Java example
In this example we will see how to create a File Path in Java. The idea is very simple. You want your program to be able to create a File and store it in a particular File Path in your system. Let’s say that your program wants to create and store a file in your Home directory. The problem is that the file path will different across operating systems, mostly because different operating systems use different file separators in their file paths. In Windows we would write “dir\file
” and in a UNIX system “dir/file
” .
There are basically two ways to construct a program that works well in both operating systems. The first one is setting the File Path manually, by discovering the operating system that the program runs in, and then depending on the case palce a “\” or a “/” between the diretcory and the File name. Let’s see the code of the first way :
Setting the File Path manually:
package com.javacodegeeks.java.core; import java.io.File; import java.io.IOException; public class JavaFilePathExample { public static void main(String[] args) { try { String filename = "newfile.txt"; String createFilePath = ""; String userHomeDirectory = System.getProperty("user.home"); String user_operatingSystem = System.getProperty("os.name").toLowerCase(); if (user_operatingSystem.contains("windows")) { createFilePath = userHomeDirectory + "\\" + filename; } else if (user_operatingSystem.contains("nix") || user_operatingSystem.contains("nux")) { createFilePath = userHomeDirectory + "/" + filename; } else { createFilePath = userHomeDirectory + "{smth_else}" + filename; } System.out.println("File path to create : " + createFilePath); File file = new File(createFilePath); if (file.createNewFile()) { System.out.println("New File created in the home directory"); } else { System.out.println("The File already exists"); } } catch (IOException e) { e.printStackTrace(); } } }
Output on Windows:
File path to create : C:\Users\nikos7\newfile.txt
New File created in the home directory
Output on Linux:
File path to create : /home/nikos/newfile.txt
New File created in the home directory
Setting the File Path using File.separator:
In the second example we see how you can use the system property File.separator
to construct the File Path you want without considering the operating system and take special care for each case.
package com.javacodegeeks.java.core; import java.io.File; import java.io.IOException; public class JavaFilePathExample { public static void main(String[] args) { try { String filename = "newfile.txt"; String createFilePath = ""; String userHomeDirectory = System.getProperty("user.home"); createFilePath = userHomeDirectory + File.separator + filename; System.out.println("File path to create : " + createFilePath); File file = new File(createFilePath); if (file.createNewFile()) { System.out.println("New File created in the home directory"); } else { System.out.println("The File already exists"); } } catch (IOException e) { e.printStackTrace(); } } }
Output on Windows:
File path to create : C:\Users\nikos7\newfile.txt
New File created in the home directory
Output on Linux:
FFile path to create : /home/nikos/newfile.txt
New File created in the home directory
This was an example on how to create and store File Paths across different operating systems in Java.