File.separator and new File() in Java
In this tutorial, we'll look at three examples for building a path to a file:
- File.separator or System.getProperty("file.separator")
- File file = new File(path, filename)
- Create a path manually (not recommended)
File.separator
This is the classic way to construct a path using File.separator or System.getProperty("file.separator"). Both will check the operating system used and return the file path correctly, for example:
1. Windows = \
2. Unix; Linux or Mac = /
import java.io.File;
import java.io.IOException;
public class FileSeparator {
public static void main(String[] args) {
try {
String filename = "nouveau_fichier.txt";
String currentdirectory= System.getProperty("user.dir");
String absoluteFilePath = "";
absoluteFilePath = currentdirectory + File.separator + filename;
System.out.println("Absolute Path: " + absoluteFilePath);
File file = new File(absoluteFilePath);
if (file.createNewFile()) {
System.out.println("File created!");
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:
Absolute path: C:\Documents and Settings\Poste07\workspace\File\nouveau_fichier.txt
File created!
new File()
Calling new File() also allows you to build the path of the file.
import java.io.File;
import java.io.IOException;
public class NewFile {
public static void main(String[] args) {
try {
String filename = "nouveau_fichier.txt";
String currentdirectory = System.getProperty("user.dir");
File file = new File(currentdirectory, filename);
System.out.println("Path: " + file.getAbsolutePath());
if (file.createNewFile()) {
System.out.println("File created!");
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:
Path: C:\Documents and Settings\Poste07\workspace\File\nouveau_fichier.txt
File already exists.
Build the path manually
This method is to be avoided, here we show you just to show all the ways to build a path.
http://java.sun.com/javase/6/docs/api/java/io/File.html
import java.io.File;Output:
import java.io.IOException;
public class NewFile {
public static void main(String[] args) {
try {
String filename = "nouveau_fichier.txt";
String currentdirectory = System.getProperty("user.dir");
String AbsolutePath = "";
String os = System.getProperty("os.name").toLowerCase();
if (os.indexOf("win") >= 0) {
//if windows
Absolutepath = currentdirectory + "\\" + filename;
} else if (os.indexOf("nix") >= 0 ||
os.indexOf("nux") >= 0 ||
os.indexOf("mac") >= 0) {
//if unix or mac
AbsolutePath = currentDirectory + "/" + filename;
}else{
//os not found
AbsolutePath = currentdirectory + "/" + filename;
}
System.out.println("Path: " + AbsolutePath);
File file = new File(AbsolutePath);
if (file.createNewFile()) {
System.out.println("File created");
} else {
System.out.println("File already existing");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Path: C:\Documents and Settings\Poste07\workspace\File\nouveau_fichier.txtResources:
File already exists.
http://java.sun.com/javase/6/docs/api/java/io/File.html