0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Files class createDirectories method

Last updated at Posted at 2023-02-06

Path(directory).resolve(Path(file/directory))で
特定のdirectoryのfile/directoryを参照することができる
createDirectoriesは、parent directoryがない場合にmkdirしてsubdirをmkdir
createDirectoryは、parent directoryがない場合 NoSuchFileException
Files.copy, Files.moveで、copy /moveができる

public class Outer {
    public static void main(String[] args) throws Exception{
        String dir = "C:\\dirtest";
        File f = new File(dir);
        Path p = Paths.get(dir, "subDir", "sub2Dir");
        Path parentDir = Paths.get(dir);
        System.out.println(p);
        method2(f,"",null);
        Files.createDirectories(p);
        Path pf = Paths.get("test.java");
        Path pf2 = p.resolve(pf);
        Path pf2bk = p.resolve(Paths.get("testbk.java"));
        if(!Files.exists(pf2)) {
            Files.createFile(pf2);
        } 
        method2(f,"",null);
        if(Files.exists(pf2)) {
            if(!Files.exists(pf2bk)) {
                Files.copy(pf2,pf2bk);
            }
        }
        Path pf2mv = p.resolve(Paths.get("testmv.java"));
        if(Files.exists(pf2) && !Files.exists(pf2mv)) {
            Files.move(pf2,pf2mv);
        }
        method2(f,"",null);
        try {
            Files.createDirectory(p);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        try {
            if(Files.exists(pf2mv))  Files.delete(pf2mv);
            if(Files.exists(pf2bk))  Files.delete(pf2bk);
            if(Files.exists(p))  Files.delete(p);
            if(Files.exists(p.getParent()))  Files.delete(p.getParent());
        } catch (DirectoryNotEmptyException ex) {
            ex.printStackTrace();
        }
        method2(f,"",null);
        Path wrongPath = Paths.get(dir, "xxx", "yyy");
        try {
            Files.createDirectory(wrongPath);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    static void method2(File pf, String indent, FileFilter ff) {
        System.out.println("----------------------------------------------");
        method(pf, indent, ff);
    }
    static void method(File pf, String indent, FileFilter ff) {
        class SampleClass {
            File[]  method() {
                if(ff == null) return pf.listFiles();
                else  return pf.listFiles(ff);
            }
        }
        File[] fs = new SampleClass().method();
        for(File f:fs) {
            System.out.print(indent + f.getName());
            if(f.isDirectory())  System.out.println("   is directory.");
            else  System.out.println("    is file");
            if(f.isDirectory())  method(f, indent + " ",ff);
        }
    }
}
C:\dirtest\subDir\sub2Dir
----------------------------------------------
cook.jpg    is file
cookcpy.jpg    is file
iii    is file
sample.ser    is file
showa.txt    is file
test.txt    is file
testmakefile.txt    is file
testwritefile.txt    is file
----------------------------------------------
cook.jpg    is file
cookcpy.jpg    is file
iii    is file
sample.ser    is file
showa.txt    is file
subDir   is directory.
 sub2Dir   is directory.
  test.java    is file
test.txt    is file
testmakefile.txt    is file
testwritefile.txt    is file
----------------------------------------------
cook.jpg    is file
cookcpy.jpg    is file
iii    is file
sample.ser    is file
showa.txt    is file
subDir   is directory.
 sub2Dir   is directory.
  testbk.java    is file
  testmv.java    is file
test.txt    is file
testmakefile.txt    is file
testwritefile.txt    is file
java.nio.file.FileAlreadyExistsException: C:\dirtest\subDir\sub2Dir
	at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:87)
	at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
	at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
	at java.base/sun.nio.fs.WindowsFileSystemProvider.createDirectory(WindowsFileSystemProvider.java:509)
	at java.base/java.nio.file.Files.createDirectory(Files.java:690)
	at com.mycompany.mavenproject1.Outer.main(Outer.java:41)
----------------------------------------------
cook.jpg    is file
cookcpy.jpg    is file
iii    is file
sample.ser    is file
showa.txt    is file
test.txt    is file
testmakefile.txt    is file
testwritefile.txt    is file
java.nio.file.NoSuchFileException: C:\dirtest\xxx\yyy
	at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:85)
	at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
	at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
	at java.base/sun.nio.fs.WindowsFileSystemProvider.createDirectory(WindowsFileSystemProvider.java:509)
	at java.base/java.nio.file.Files.createDirectory(Files.java:690)
	at com.mycompany.mavenproject1.Outer.main(Outer.java:56)
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?