Javaでファイルパスを構築する方法
このチュートリアルでは、ファイルパスを構築するための3つのJavaの例を示します。
-
File.separatorまたはSystem.getProperty( “file.separator”)(推奨)
-
ファイルファイル=新しいファイル(workingDir、ファイル名); (推奨)
-
手動でファイル区切りを作成します. (ちょうど楽しみのために、推薦しない)
1. File.separator
File.separator`や `System.getProperty(” file.separator “)`を使って、ファイルパスを構築するための古典的なJavaの例です。どちらもOSをチェックし、ファイルセパレータを正しく返します。たとえば、
-
Windows =
\
-
** nixまたは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(); } } }
出力
Final filepath :/Users/mkyong/Documents/workspace/maven/fileUtils/newFile.txt File is created!
2.新しいファイル()
いくつかの開発者は
new File()
APIを使ってファイルパスを構築しています。
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(); } } }
出力
Final filepath :/Users/mkyong/Documents/workspace/maven/fileUtils/newFile.txt File is created!
マニュアルファイルセパレータ
システムのOSを確認して、手動でファイルパスを作成してください。お楽しみのためだけにはお勧めしません。
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(); } } }
出力
Final filepath :/Users/mkyong/Documents/workspace/maven/fileUtils/newFile.txt File is created!
参考文献
-
リンク://java/how-to-get-the-current-working-directory-in-java/[How to
現在の作業ディレクトリを取得する]。リンク://java/how-to-detect-os-in-java-systemgetpropertyosname/[How to
JavaでOSを検出する]。
http://lopica.sourceforge.net/os.html
[osまたはarch値はどのようにすることができますか
つかいます?]
リンク://タグ/ファイルパス/[ファイルパス]リンク://タグ/io/[io]リンク://タグ/java/[java]