Java学习笔记

《Java编程要点》、《Java程序员修炼之道》
Java I/O
    1、顶层抽象类
        InputStream、OutputStream(字节); Reader、Writer(字符)
    2、实现类
        FileInputStream、FileOutputStream: read(byte[]), write(byte[])
        FileReader、FileWriter:read(char[]), write(char[])
    3、包装类
        InputStreamReader、OutputStreamWriter:将字节流转换成字符流,还可以设置编码规则
        BufferedReader、BufferedWriter:带缓冲区的字符流
        BufferedInputStream、BufferedOutputStream:带缓冲区的字节流
    4、封装好的工具类
        Scanner:文本扫描仪,可以转换String和基本数据类型。默认以空白字符(空格制表回车)作为分割符,也可以自定义,见源码。
        Console:调用系统的Console交互,暂时没用到。
        DataStream:二进制数据流,可读写取基本数据类型。
            抽象类:DataInput、DataOutput
            实现类:DataInputStream、DataOutputStream
        ObjectStream:对象流,Serializable有关, 参看序列化过程部分。
            ObjectInputStream <- ObjectInput <- DataInput
            ObjectOutputStream <- ObjectOutput <- DataOutput

Java 文件系统
    1、Path、Paths
        Path:可以用来表示路径, 试图避免不同系统上路径表示方式的差异,例如Linux的"/home/shabi/"和Windows的"D:\users\傻逼"。想想Python的os.path()。
            getFileName()、getNameCount()、getParent()、getRoot()、resolve()
        Paths:Path的工具类,用来创建Paths。内部调用了系统方法, 例如windwos系统调用WindowsPath()方法。
               Path path = Paths.get("/home/shabi");
    2、Files:文件工具类,用来操作文件(不是文件的文本信息哦)和目录
        返回值    方法                                                     作用
        Path            copy(Path source, Path target, CopyOption… options)                复制文件
        Path            delete(Path path)                                                删除文件
        Path            move(Path source, Path target, CopyOption… options)                移动文件
        DirectoryStream    newDirectoryStream(Path dir )                                    操作目录
        Path            walkFileTree(Path start, FileVisitor <? super Path> visitor)    遍历
        Path            walkFileTree(Path start, Set\<FileVisitOption> options, int maxDepth, FileVisitor<? super Path> visitor)    遍历
    3、递归遍历目录
        a、接口:FileVisitor
        b、简单实现类:SimpleFileVisitor
        c、例子
            //主程序
            public class Test {
                public static void main(String[] args){
                    Path path= Paths.get("D:\\Desktop\\test");
                    String ext="*.{txt,bmp}";
                    try{
                        EnumSet<FileVisitOption> opts =EnumSet.of(FileVisitOption.FOLLOW_LINKS);
                        Files.walkFileTree(path, opts,Integer.MAX_VALUE,new FindTxt(ext));
                    }catch (IOException ex){
                        ex.printStackTrace();
                    }
                }
            }
            /**
             *     ----    继承    ----
             * 1. 自写类继承SimpleFileVisitor<T>,选择重写文件访问方法
             * 2. 自写类添加PathMatch属性,以使用glob表达式
             * 3. 若仅仅重写访问方法,不添加其他属性方法,则函数调用可简化成
             *     Files.walkFileTree(path,new FindTxt());
             * 
             *     ----    实现    ----
             * 1. 自写类实现FileVisitor<T>的四个接口
             * 2. 函数调用的参数发生变化
             *  EnumSet<FileVisitOption> opts =EnumSet.of(FileVisitOption.FOLLOW_LINKS);
             *  Files.walkFileTree(path, opts,Integer.MAX_VALUE,new FindTxt(ext));
             */
            public  class FindTxt extends SimpleFileVisitor<Path> {
                private PathMatcher pathMatcher;
                public FindTxt(String ext){
                    this.pathMatcher = FileSystems.getDefault().getPathMatcher("glob:"+ext);
                }
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs){
                    if(null!=file.getFileName()&&pathMatcher.matches(file.getFileName())){
                        System.out.println(file.getFileName());
                    }
                    return FileVisitResult.CONTINUE;
                }
            }
    4、文件文本操作
        a、简化操作
            BufferedReader br = Files.newBufferedReader(Path.get("..."))
        b、一次读取文件全部行或全部字节
            try{
                List<String> lines = Files.readAllLines(path,StandardCharsets.UTF_8);
                byte[] bytes=Files.readAllBytes(path);
            }catch (IOException ex){
                ex.printStackTrace();
            }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值