How to construct a file path in Java

In this tutorial, we will show you three Java examples to construct a file path :

  1. File.separator or System.getProperty(“file.separator”) (Recommended)
  2. File file = new File(workingDir, filename); (Recommended)
  3. Create the file separator manually. (Not recommend, just for fun)

1. File.separator

Classic Java example to construct a file path, using File.separator or System.getProperty("file.separator"). Both will check the OS and returns the file separator correctly, for example,

  1. Windows = \
  2. *nix or Mac = /
FilePathExample1.java

package com.mkyong.file;

import java.io.File;
import java.io.IOException;

public class FilePathExample1 {
	public static void main(String[] args) {
	  try {

		String filename = "newFile.txt";
		String workingDirectory = System.getProperty("user.dir");
			
		//****************//
			
		String absoluteFilePath = "";
			
		//absoluteFilePath = workingDirectory + System.getProperty("file.separator") + filename;
		absoluteFilePath = workingDirectory + File.separator + filename;

		System.out.println("Final filepath : " + absoluteFilePath);
			
		//****************//
			
		File file = new File(absoluteFilePath);

		if (file.createNewFile()) {
			System.out.println("File is created!");
		} else {
			System.out.println("File is already existed!");
		}

	  } catch (IOException e) {
		e.printStackTrace();
	  }
	}
}

Output


Final filepath : /Users/mkyong/Documents/workspace/maven/fileUtils/newFile.txt
File is created!

2. new File()

Some developers are using new File() API to construct the file path.

FilePathExample2.java

package com.mkyong.file;

import java.io.File;
import java.io.IOException;

public class FilePathExample2 {
	public static void main(String[] args) {
	  try {

		String filename = "newFile.txt";
		String workingDirectory = System.getProperty("user.dir");
			
		//****************//
			
		File file = new File(workingDirectory, filename);
			
		//****************//
						
		System.out.println("Final filepath : " + file.getAbsolutePath());
		if (file.createNewFile()) {
			System.out.println("File is created!");
		} else {
			System.out.println("File is already existed!");
		}

	  } catch (IOException e) {
		e.printStackTrace();
	  }
	}
}

Output


Final filepath : /Users/mkyong/Documents/workspace/maven/fileUtils/newFile.txt
File is created!

3. Manual file separator

Check the system OS and create file path manually, just for fun, not recommend.

FilePathExample3.java

package com.mkyong.file;

import java.io.File;
import java.io.IOException;

public class FilePathExample3 {
	public static void main(String[] args) {
	  try {

		String filename = "testing.txt";
		String workingDir = System.getProperty("user.dir");
			
		String absoluteFilePath = "";
			
		//****************//
			
		String your_os = System.getProperty("os.name").toLowerCase();
			
		if (your_os.indexOf("win") >= 0) {
				
			//if windows
			absoluteFilePath = workingDir + "\\" + filename;
				
		} else if (your_os.indexOf("nix") >= 0 || 
                           your_os.indexOf("nux") >= 0 || 
                           your_os.indexOf("mac") >= 0) {
				
			//if unix or mac 
			absoluteFilePath = workingDir + "/" + filename;
				
		}else{
				
			//unknow os?
			absoluteFilePath = workingDir + "/" + filename;
				
		}

		System.out.println("Final filepath : " + absoluteFilePath);
			
		//****************//
			
		File file = new File(absoluteFilePath);

		if (file.createNewFile()) {
			System.out.println("Done");
		} else {
			System.out.println("File already exists!");
		}

	  } catch (IOException e) {
		e.printStackTrace();
	  }
	}
}

Output


Final filepath : /Users/mkyong/Documents/workspace/maven/fileUtils/newFile.txt
File is created!

References

  1. File JavaDoc
  2. How to get current working directory
  3. How to detect os in Java
  4. What os or arch value can I use?

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

9 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
manisg
13 years ago

your tutorial is good…but require more description..

RAJAT BHATIA
11 years ago

String workingDirectory = System.getProperty(“user.dir”);

String absoluteFilePath = workingDirectory + File.separator ;

System.out.println(absoluteFilePath);

— Construct file path in 2 lines.. awesome.. thanks

Ausgur Trolio
11 years ago

Just apply that: System.getProperty(“file.separator”) and forget the rest and this particular tutorial which is not relevant at all.

mkyong
11 years ago
Reply to  Ausgur Trolio

Thanks for your input, article is updated with more ways to construct a file path.

Patrick
12 years ago

perfect way

Moutaz
12 years ago

Or
System.getProperty(“file.separator”)

Nice Work!

Chinna Rego
13 years ago

Easy and better examples.it is used improve my programming knowledge..thank u

kdt
16 years ago

This can be even more simple 🙂
String filename = “testing.txt”;
String workingDir = System.getProperty(“user.dir”);
File file = new File(workingDir, filename);