JAVA/JAVA

NIO 파일, 폴더 생성

lovineff 2020. 6. 4. 17:38
 	// Create Folder
        Path newFolderPath = Paths.get("D:\\Desktop\\test");
        if(Files.exists(newFolderPath) && Files.isDirectory(newFolderPath)){
            System.out.println("폴더가 존재합니다.");
        }else{
            Files.createDirectory(newFolderPath);
            System.out.println("폴더가 생성되었습니다.");
        }

        // Create File
        Path newPath = Paths.get("D:\\Desktop\\새 폴더 (6)\\isTest.txt");
        if(Files.exists(newPath)){
            System.out.println("파일이 존재합니다.");
        }else{
            Files.createFile(newPath);
            System.out.println("파일이 생성되었습니다.");
        }

        // Folder File List 1
        Path path = Paths.get("D:\\Desktop\\새 폴더 (6)");
        DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path);
        directoryStream.forEach(ds ->{
            if(Files.isDirectory(ds)){
            }else{
            }
        });

        // Folder Files List 2
        start = System.currentTimeMillis();
        Files.list(path).parallel().forEach(ds ->{
            if(Files.isDirectory(ds)){
            }else{
            }
        });
        // Folder File List 1 보다 속도가 훨씬 빠름.(병렬처리)