LoginSignup
0
0

More than 1 year has passed since last update.

Path interface

Last updated at Posted at 2023-02-06

File classとほぼ同様の機能を持つ
Paths.getでインスタンス化する。get("C:","dirtest","test.txt")のような
書き方が可能。それ以外の違いが?
toFile()でfileを生成することができる

public class Outer {
    public static void main(String[] args) {
        Path p = Paths.get("C:\\dirtest\\test.txt");
        System.out.println(p);
        System.out.println(p.getParent());
        System.out.println(p);
        System.out.println(p.getRoot());
        p = Paths.get("C:","dirtest","test.txt");
        System.out.println(p);
        p = Paths.get("C:/dirtest/test.txt");
        System.out.println(p);
        File f = p.toFile();
        System.out.println(f);
        p = f.toPath();
        System.out.println(p);
        p = Paths.get("C:/dirtest/test.txt2zzzz");
        System.out.println(p);
        System.out.println(p.toAbsolutePath());
        p = Paths.get("C:/dirtest/test.txt");
        System.out.println(p);
        try {
            FileReader fr = new FileReader(p.toFile());
            BufferedReader br = new BufferedReader(fr);
            try(br) {
                while(true) {
                    String str = br.readLine();
                    if(str==null) break;
                    System.out.println(str);
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
C:\dirtest\test.txt
C:\dirtest
C:\dirtest\test.txt
C:\
C:\dirtest\test.txt
C:\dirtest\test.txt
C:\dirtest\test.txt
C:\dirtest\test.txt
C:\dirtest\test.txt2zzzz
C:\dirtest\test.txt2zzzz
C:\dirtest\test.txt
hello my
friend bye
0
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
0
0