Creating and Reading Directories

Creating and Reading Directories

Listing a File System’s Root Directories

ListDir.java

import java.nio.file.*;

public class ListDir {

    public static void main(String[] args) {

        Iterable<Path> dirs = FileSystems.getDefault().getRootDirectories();

        for (Path name : dirs) {

            System.err.println(name);

        }

    }

}
Creating a Directory

CreateDir.java

import java.util.*;
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.*;

public class CreateDir {

    public static void main(String[] args) {

        Path file = Paths.get("test-dir");

        Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxr-x---");

        FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms);

        try {

            Files.createDirectory(file, attr);

        } catch (IOException x) {

            System.out.println("I/O exception: " + x);

        }

    }
}

CreateDirs.java

import java.io.*;
import java.nio.file.*;

public class CreateDirs {

    public static void main(String[] args) {

        Path paths = Paths.get("foo/bar/test");

        try {

            Files.createDirectories(paths);

        } catch (IOException x) {

            System.out.println("I/O exception: " + x);

        }

    }

}
Creating a Temporary Directory

CreateTempDir.java

create in pwd

import java.io.*;
import java.nio.file.*;

public class CreateTempDir {

    public static void main(String[] args) {

        Path path = Paths.get(".");

        String prefix = null;

        try {

            Files.createTempDirectory(path, prefix);

        } catch (IOException x) {

            System.out.println("I/O exception: " + x);

        }

    }

}

CreateTempDir2.java

create in /tmp/

import java.io.*;
import java.nio.file.*;

public class CreateTempDir2 {

    public static void main(String[] args) {

        String prefix = "temp";

        try {

            Files.createTempDirectory(prefix);

        } catch (IOException x) {

            System.out.println("I/O exception: " + x);

        }

    }

}
Listing a Directory’s Contents

ListSourceFiles.java

import java.nio.file.*;
import java.io.*;

public class ListSourceFiles {

    public static void main(String[] args) {

        Path dir = Paths.get("/tmp");

        try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {

            for (Path file: stream) {

                System.out.println(file.getFileName());

            } 

        } catch (IOException | DirectoryIteratorException x) {

            System.err.println(x);

        }

    }

}
Filtering a Directory Listing by Using Globbing

FilterDirByGlob.java

import java.nio.file.*;
import java.io.*;

public class FilterDirByGlob {

    public static void main(String[] args) {

        Path dir = Paths.get(".");

        try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.{java,class,jar}")) {

            for (Path entry: stream) {

                System.out.println(entry.getFileName());

            }

        } catch (IOException x) {

            System.err.println(x);

        }

    }

}
Writing Your Own Directory Filter

DirectoryFilter.java

use lambda expression

import java.io.*;
import java.nio.file.*;

public class DirectoryFilter {

    public static void main(String[] args) {

        /*DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {

            public boolean accept(Path file) throws IOException {
            
 				return Files.isDirectory(file);

            }
            
        };*/

        Path dir = Paths.get(".");

        // try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, filter)) {
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir,p -> Files.isDirectory(p))) {

            for (Path entry: stream) {

                System.out.println(entry.getFileName());

            }

        } catch (IOException x) {

            System.err.println(x);

        }

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值