So erstellen Sie einen Dateipfad in Java
In diesem Tutorial zeigen wir Ihnen drei Java-Beispiele zum Erstellen eines Dateipfads:
-
File.separator or System.getProperty(“file.separator”) (Recommended)
-
Datei Datei = neue Datei (WorkingDir, Dateiname); (Empfohlen)
-
Erstellen Sie das Dateitrennzeichen manuell. (Nicht zu empfehlen, nur zum Spaß)
1. File.separator
Klassisches Java-Beispiel zum Erstellen eines Dateipfads mitFile.separator
oderSystem.getProperty("file.separator")
. Beide überprüfen das Betriebssystem und geben das Dateitrennzeichen korrekt zurück, z.
-
Windows =
\
-
* nix oder Mac =
/
FilePathExample1.java
package com.example.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(); } } }
Ausgabe
Final filepath : /Users/example/Documents/workspace/maven/fileUtils/newFile.txt File is created!
2. neue Datei()
Einige Entwickler verwenden die API vonnew File()
, um den Dateipfad zu erstellen.
FilePathExample2.java
package com.example.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(); } } }
Ausgabe
Final filepath : /Users/example/Documents/workspace/maven/fileUtils/newFile.txt File is created!
3. Manuelles Dateitrennzeichen
Überprüfen Sie das Betriebssystem des Systems und erstellen Sie den Dateipfad manuell, nur zum Spaß, nicht zu empfehlen.
FilePathExample3.java
package com.example.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(); } } }
Ausgabe
Final filepath : /Users/example/Documents/workspace/maven/fileUtils/newFile.txt File is created!