• Java IO


    IO(输入输出)是所有程序员必需的——输入,程序读取外部数据;输出,将程序数据输出到磁盘、光盘等。

    Java的IO包下主要包括输入、输出两种IO流,每种输入、输出流又可以分为字节流和字符流。字节流是以字节为单位处理输入、输出操作;字符流是以字符为单位处理输入、输出操作。

    Java的IO流使用了一种“装饰器设计模式”,它将IO流分成底层节点流和上层处理流,其中节点流用于和底层的物理存储节点直接关联——不同的物理节点获取的节点流的方式可能存在一定的差异,但程序可以把不同的物理节点流包装成统一的处理流,从而允许程序使用统一的输入、输出代码来读取不同的物理存储节点的资源。

    Java 7 在java.nio及其子包下提供了一系列全新的API,这些API是对原有新IO的升级,因此被称为NIO2,通过这些NIO2, 程序可以更高效的进行输入、输出操作

    public  class MyFile {
        
        @Test
        public  void fileNameFilter(){
             // 表示当前路径 
            File file = new File("." );
            System.out.println(file.getAbsolutePath());
            String [] nameList = file.list((dir,name)->name.endsWith(".java") || new File(name).isDirectory());
             for (String name:nameList){
                System.out.println(name);
            }
        }
        
        @Test
        public  void operateDir(){
            String filePath = "D:/rding/testfile3/" ;
            File file = new File(filePath);
             boolean canMkdir = file.mkdir();
            System.out.println(canMkdir);
            File dirs = new File("d:/rding/testfile3/test31/test32" );
            dirs.mkdirs();
            File roots[] = File.listRoots();
             for ( int i = 0; i < roots.length;i++ ){
                System.out.println(roots[i]);
            }
            
        }
    
        @Test
        public  void operateFile(){
            String filepath = "D:/rding/testfile2/file1.txt" ;
            File file = new File(filepath);
             boolean createNewFile;
             try {
                createNewFile = file.createNewFile();
                System.out.println(createNewFile);
                // 生成一个文件,文件名是前缀+随机数+后缀名 
                File tempFile = File.createTempFile("Hello", ".java", new File("D:/rding/testfile2/" ));
                File delteFile = new File("D:/rding/testfile2/file2.txt" );
                 boolean deleteFile = delteFile.delete();
                System.out.println(delteFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        @Test
        public  void fileContent(){
            String filePath = "D:/rding/testfile2/file.txt" ;
            File file = new File(filePath);
             long length = file.length();
            System.out.println(length);
            Date lastModified = new Date(file.lastModified());
            System.out.println(lastModified);
        }
        
        @Test
        public  void fileName() {
            String filePath = "D:/rng/testfile1" ;
            File file = new File(filePath);
            String fileName = file.getName();
            System.out.println( "fileName : " + fileName);
            String path = file.getPath();
            System.out.println( "path : " + path);
            String absolutePath = file.getAbsolutePath();
            System.out.println( "absolutePath : " + absolutePath);
            File absoluteFile = file.getAbsoluteFile();
            System.out.println( "absoluteFile : " + absoluteFile);
            File parentFile = file.getParentFile();
            System.out.println( "parentFile : " + parentFile);
            String parentPath = file.getParent();
            System.out.println( "parentPath : " + parentPath);
             boolean canRenameToFile = file.renameTo( new File("D:/rding/testfile2" ));
            System.out.println( "canRename : " + canRenameToFile);
        }
    
        @Test
        public  void checkFile() {
            String filePath = "D:/rding/testfile" ;
            File file = new File(filePath);
             boolean exists = file.exists();
            System.out.println( "exists : " + exists);
             boolean canWrite = file.canWrite();
            System.out.println( "canWrite : " + canWrite);
             boolean canRead = file.canRead();
            System.out.println( "canRead : " + canRead);
             boolean isFile = file.isFile();
            System.out.println( "isFile : " + isFile);
             boolean isDirectory = file.isDirectory();
            System.out.println( "isDirectory : " + isDirectory);
             boolean isAbsolute = file.isAbsolute();
            System.out.println( "isAbsolute : " + isAbsolute);
        }
    }
  • 相关阅读:
    设计一个移动应用的本地缓存机制
    LeetCode 15
    POJ 2411
    Mahout构建图书推荐系统【一起学Mahout】
    firefox篇
    zTree实现删除树节点
    Tomcat与Servlet工作流程
    cocos2d 重写顶点着色语言
    将HTML格式的String转化为HTMLElement
    JQuery总结
  • 原文地址:https://www.cnblogs.com/lfdingye/p/7597236.html
Copyright © 2020-2023  润新知