Javaでファイルのパスを取得する

File.getAbsolutePath() メソッドは、ファイルのフルパス (パス + 名前) の名前を返します。

Example:

File file = File("C:\\Dossier_111\\fichier.txt");
System.out.println("パス: " + file.getAbsolutePath());
フルパスが表示されます: "C:\\Dossier_111\\fichier.txt".

ほとんどの場合、substring() や lastIndexOf() などの関数を使用して、ファイル名のないパスのみを取得する必要があります。

File file = File("C:\\ファイル111\\fichier.txt");
String absolutePath = file.getAbsolutePath();
String filePath = absolutePath.
substring(0,absolutePath.lastIndexOf(File.separator));
System.out.println("パス: " + filePath);
Runtime:

C:\\Folder111

Resources:
tutorialspoint - Java.io.File.getAbsolutePath()メソッド


Commentaires (12)

Connectez-vous pour commenter

Rejoignez la discussion et partagez vos connaissances avec la communauté

JD
Jean Dupont Il y a 2 heures

Excellent tutoriel !

👍 12 Répondre Signaler
CodeurJava ✓ Auteur • Il y a 1 heure

N'hésitez pas si vous avez des questions.