A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© syusouki 中级黑马   /  2015-10-16 22:45  /  674 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 syusouki 于 2015-10-16 22:47 编辑

/*public static List<String> readAllLines(Path path,Charset cs):从文件中读取所有的行。
public static Path write(Path path,Iterable<? extends CharSequence> lines, Charset cs, OpenOption... options):将文本写入文件。
public static byte[] readAllBytes(Path path):从文件中读取所有字节。
public static Path write(Path path,byte[] bytes,OpenOption... options):将字节写入文件。
public static long size(Path path):返回一个文件的大小(以字节为单位)。
public static Path move(Path source,Path target,CopyOption...options):将文件移动或重命名为目标文件。
public static long copy(InputStream in,Path target,CopyOption...options):将所有字节从一个输入流复制到文件。
public static InputStream newInputStream(Path path,OpenOption... options):打开一个文件,返回一个输入流从文件中读取。
public static long copy(Path source,OutputStream out):将所有字节从一个文件复制到一个输出流。
public static OutputStream newOutputStream(Path path,OpenOption... options):打开或创建一个文件,返回一个输出流,可用于将字节写入文件。
public static Path copy(Path source,Path target,CopyOption... options):将文件复制到目标文件。
public static Path createDirectories(Path dir,FileAttribute<?>... attrs):创建一个目录,创建所有不存在的父目录。
public static Path createDirectory(Path dir,FileAttribute<?>... attrs):创建一个新目录。
public static Path createFile(Path path,FileAttribute<?>... attrs):创建一个新的空文件
public static void delete(Path path):删除该文件。
public static boolean deleteIfExists(Path path):如果该文件存在,则删除该文件。
public static boolean exists(Path path,LinkOption... options):测试文件是否存在。
public static boolean notExists(Path path,LinkOption...options):测试该路径是否不存在该文件。
public static boolean isExecutable(Path path):测试文件是否是可执行文件。
public static boolean isHidden(Path path):测试文件是否隐藏。
public static boolean isReadable(Path path):测试文件是否可读。
public static boolean isWritable(Path path):测试文件是否可写。
public static boolean isSymbolicLink(Path path):测试文件是否是一个符号链接。
public static boolean isSameFile(Path path,Path path2):测试如果路径定位相同的文件。
public static boolean isDirectory(Path path,LinkOption... options):测试文件是否目录。
public static FileTime getLastModifiedTime(Path path,LinkOption... options):返回文件的最后修改时间。
public static BufferedReader newBufferedReader(Path path,Charset cs):打开一个文件进行读取,返回一个BufferedReader,可以有效的方式从文件中读取文本。
public static BufferedWriter newBufferedWriter(Path path,Charset cs,OpenOption... options):打开或创建一个文件写入,返回BufferedWriter,可用于以有效的方式将文件写入文本。
public static DirectoryStream<Path> newDirectoryStream(Path dir):打开目录,返回一个目录流遍历目录中的所有条目。
public static DirectoryStream<Path> newDirectoryStream(Path dir,String glob):打开目录,返回一个目录流遍历目录中的所有条目。
public static DirectoryStream<Path> newDirectoryStream(Path dir,DirectoryStream.Filter<? super Path> filter):打开目录,返回一个目录流遍历目录中的所有条目。

*/
public class Demo {
        public static void main(String[] args) throws FileNotFoundException,        IOException {
               
                // public static Path write(Path path, Iterable<? extends CharSequence> lines, Charset cs, OpenOption... options):将集合的数据写入到文件
                ArrayList<String> strList = new ArrayList<>();
                strList.add("刘德华");
                strList.add("张学友");
                strList.add("郭富城");
                strList.add("黎明");
               
                Files.write(Paths.get("demo12_List.txt"), strList,Charset.forName("GBK"));
                System.out.println("写入完毕!");

                // public static List<String> readAllLines(Path path,Charset cs):从文件中读取所有的行(将文本文件中的数据读取到集合)。
                Path path = Paths.get("demo12_List.txt");
                List<String> list = Files.readAllLines(path, Charset.forName("GBK"));
                for (String s : list) {
                        System.out.println(s);
                }

                // public static long size(Path path):返回一个文件的大小(以字节为单位)。
                System.out.println(Files.size(Paths.get("D:/workspace/day22/05_copy.wmv")));

                // public static boolean notExists(Path path,LinkOption...options):测试该路径是否不存在该文件。
                System.out.println(Files.notExists(Paths.get("D:/workspace/day22/05_copy.wmv")));

                // public static Path move(Path source,Path target,CopyOption...options):将文件移动或重命名为目标文件。
                // 文件名不同就是重命名,文件父目录不同就是移动
                System.out.println(Files.move(Paths.get("D:/workspace/day21/55_copy.wmv"),(Paths.get("D:/workspace/day22/55_copy.wmv"))));// 移动

                // public static boolean isSameFile(Path path,Path path2):测试是否是同一文件。(文件路径不存在会抛异常)
                System.out.println(Files.isSameFile(Paths.get("D:/workspace/day22/3_copy.wmv"),        Paths.get("D:/workspace/day22/05_copy.wmv")));// False

                Path dir = Paths.get("D:/workspace/day21");
                // getPaths(dir);

                Path yuanPath = Paths.get("D:/workspace/day22/05.wmv");
                Path mudiPath = Paths.get("D:/workspace/day22/05_copy.wmv");
                // copyFile(yuanPath, mudiPath);

        }
        private static void copyFile(Path yuanPath, Path mudiPath)        throws IOException {

                // public static byte[] readAllBytes(Path path):从文件中读取所有字节。
                // public static Path write(Path path,byte[] bytes,OpenOption... options):将字节写入文件。
                byte[] byArray = Files.readAllBytes(yuanPath);
                Files.write(mudiPath, byArray);

                // public static Path copy(Path source,Path target,CopyOption... options):将元文件复制到目标文件。
                Files.copy(yuanPath, mudiPath);

                // public static long copy(InputStream in,Path target,CopyOption...options):将所有字节从一个输入流复制到文件。
                // public static InputStream newInputStream(Path path,OpenOption... options):打开一个文件,返回一个输入流从文件中读取。
                InputStream in = Files.newInputStream(yuanPath);// 获取源文件输入流
                Files.copy(in, mudiPath);

                // public static long copy(Path source,OutputStream out):将所有字节从一个文件复制到一个输出流。
                // public static OutputStream newOutputStream(Path path,OpenOption... options):打开或创建一个文件,返回一个输出流,可用于将字节写入文件。
                OutputStream out = Files.newOutputStream(mudiPath);// 获取目标文件输出流
                Files.copy(yuanPath, out);
        }

        private static void getPaths(Path dir) throws IOException {
                // public static DirectoryStream<Path> newDirectoryStream(Path dir):返回一个目录流(该路径下的所有Path)。
                DirectoryStream<Path> stream = Files.newDirectoryStream(dir);
                // 遍历Path集合
                for (Path p : stream) {
                        System.out.println(p);
                }

                // public static DirectoryStream<Path> newDirectoryStream(Path dir,String glob):获取匹配的文件路径
                DirectoryStream<Path> stream1 = Files.newDirectoryStream(dir, "*.txt");
                // 遍历Path集合
                for (Path p : stream1) {
                        System.out.println(p);
                }

                // public static DirectoryStream<Path> newDirectoryStream(Path dir,DirectoryStream.Filter<? super Path> filter):用目录过滤器,获取匹配的文件路径集合。
                DirectoryStream<Path> stream2 = Files.newDirectoryStream(dir,        new DirectoryStream.Filter<Path>() {
                                        public boolean accept(Path file) throws IOException {
                                                return Files.isDirectory(file);
                                        }
                                });

                // 遍历Path集合
                for (Path p : stream2) {
                        System.out.println(p);
                }
        }
}

1 个回复

倒序浏览
谢谢。new IO 确实接触的少!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马