zoukankan      html  css  js  c++  java
  • IO流2 --- File类的常用方法1 --- 技术搬运工(尚硅谷)

    • File类的获取功能
    @Test
    public void test2(){
        File file1 = new File("hello.txt");
        File file2 = new File("C:\Users\Mi\Documents\project\idea\JavaBase\hello.txt");
    
        System.out.println(file1.getAbsolutePath());//获取绝对路径
        System.out.println(file1.getPath());//实例化时的路径参数
        System.out.println(file1.getName());//文件名
        System.out.println(file1.getParent());//实例化时的父路径,相对路径时为null
        System.out.println(file1.length());//文件长度
        System.out.println(file1.lastModified());//文件修改时间
    
        System.out.println();
    
        System.out.println(file2.getAbsolutePath());
        System.out.println(file2.getPath());
        System.out.println(file2.getName());
        System.out.println(file2.getParent());
        System.out.println(file2.length());
        System.out.println(file2.lastModified());
    }
    

      

    @Test
    public void test3(){
        File file = new File("C:\Program Files");
    
        //获取指定目录下的所有文件或者文件目录的名称数组
        String[] fileNameList = file.list();
        
        //获取指定目录下的所有文件或者文件目录的File数组
        File[] fileList = file.listFiles();
    }
    
    • File类的重命名功能
    @Test
    public void test4(){
        File file1 = new File("hello.txt");
        File file2 = new File("C:\Users\Mi\Documents\io\hi.txt");
    
        //把文件重命名为指定的文件路径。
        //相当于把hello.txt移动到“C:UsersMiDocumentsio”下且改名为“hi.txt”。要想保证成功,需要file1存在且file2不存在
        boolean b = file1.renameTo(file2);
        System.out.println(b);
    }
    

      

  • 相关阅读:
    Python程序员用文字加密的方式,给女程序员写情书,一周后牵手回家
    小学生在网吧用python抓取LOL英雄皮肤,步骤简单
    vuex中module的命名空间概念
    动态设置html的font-size值
    JavaScript判断各种数据类型
    vuex脑图
    作用域链和函数内部this指向问题以及bind、call、apply方法
    BOM
    jQuery_base
    js_base_note
  • 原文地址:https://www.cnblogs.com/noyouth/p/11699158.html
Copyright © 2011-2022 走看看