1
0

Javaでファイル・パスを取得するサンプル

Posted at
import java.io.File;
import java.io.FileInputStream;

public class Main {
    public static void main(String[] args) {
        String pathname = "helloworld//src//main//resources//ValidationMessages.properties";
        try {
           File file = new File(pathname);

            System.out.println("[01] getPath():[" + file.getPath() + "]");
            System.out.println("[02] getAbsolutePath():[" + file.getAbsolutePath() + "]");
            System.out.println("[03] getParent():[" + file.getParent() + "]");
            System.out.println("[04] getName():[" + file.getName() + "]");
   
            FileInputStream fis = new FileInputStream(file);
            // ファイルの読み込み処理
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

出力例

[01] getPath():[helloworld\src\main\resources\ValidationMessages.properties]
[02] getAbsolutePath():[C:\Users\rurur\Downloads\sts-4.17.1.RELEASE\helloworld\src\main\resources\ValidationMessages.properties]
[03] getParent():[helloworld\src\main\resources]
[04] getName():[ValidationMessages.properties]
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0