java学习二十四天——IO流、异常机制、File类

java学习二十四天——IO流、异常机制、File类

1_IO流(IO流概述及其前奏)(理解)

	A:IO流概述
		IO流用来处理设备之间的数据传输
		上传文件和下载文件
	B:IO流前奏
		讲解IO流之前为什么先讲解异常和File类呢?
		因为File表示的是IO流将来要操作的文件,所以我们需要学习File类。
		而常见操作文件无非就是上传文件和下载文件,在这个操作的过程中可能出现问题,
		出现问题后,我们需要对对应的代码进行处理。所以我们需要学习异常。
		

2_IO流(异常的概述和分类)(理解)

A:异常的概述:	异常就是Java程序在运行过程中出现的错误。
B:异常的分类:	举例:张三骑自行车旅游
C:异常的继承体系
	异常的基类:	Throwable
		严重问题:	Error	不予处理,因为这种问题一般是很严重的问题,比如: 内存溢出
		非严重问题:	Exception
						编译时异常:	非RuntimeException
						运行时异常:	RuntimeException

3_IO流(JVM默认是如何处理异常的)(理解)

A:JVM默认是如何处理异常的
main函数收到这个问题时,有两种处理方式:
a:自己将该问题处理,然后继续运行
b:自己没有针对的处理方式,只有交给调用main的jvm来处理
jvm有一个默认的异常处理机制,就将该异常进行处理.
并将该异常的名称,异常的信息.异常出现的位置打印在了控制台上,同时将程序停止运行
B:案例演示: 1 / 0 ; JVM默认如何处理异常

public static void main(String[] args) {
    //运行期异常,发生在运行期间RuntimeException,及其子类属于运行期异常,对于运行期异常,可以选择解决,也可以不解决。
    int a=10;
    int b=0;
    //ArithmeticException 运行期异常,算术异常
    System.out.println(a/b);
    //运行期异常,你可以选择处理,也可以选择不处理,那上面这段代码,你并没有对这个异常进行处理。
    //你没有处理,那么Java使用的就是默认处理方式
    //默认处理方式,是怎么处理的?
    //当遇到异常时Java虚拟机会打印异常的堆栈信息,然后会退出java虚拟机
    System.out.println("后面代码");
    System.out.println("后面代码");
    System.out.println("后面代码");
    System.out.println("后面代码");
    System.out.println("后面代码");
    System.out.println("后面代码");
    //也就是说,Java虚拟机,默认处理运行期异常的方式,不够友好
    //如果你觉得这种默认的处理方式,不够友好的话,你可以自己去处理运行期异常。
}
运行结果
Exception in thread "main" java.lang.ArithmeticException: / by zero
	at org.westos.demo.MyTest2.main(MyTest2.java:14)

Process finished with exit code 1

4_IO流(try…catch的方式处理异常1)(掌握)

A:异常处理的两种方式

​ a:try…catch…finally
​ b:throws

B:try…catch处理异常的基本格式
try	{
	可能出现问题的代码 ;
}catch(异常名 变量名){
	针对问题的处理 ;
}finally{
	释放资源;
}
变形格式:

​ try {
​ 可能出现问题的代码 ;
​ }catch(异常名 变量名){
​ 针对问题的处理 ;
​ }

注意事项:

​ a: try中的代码越少越好
​ b: catch中要做处理,哪怕是一条输出语句也可以.(不能将异常信息隐藏)

C:案例演示: try…catch…finally的方式处理1个异常
public static void main(String[] args) {
    int a=10;
    int b=0;
    try{
        //ctrl+shift+上下方向键,可以上下挪动一样代码
        System.out.println(a / 0);
    }catch (ArithmeticException e){
        System.out.println("除数为0了");
    }finally {
        //finally最终的: 不管try里面的代码有没有遇到异常,那么finally里面的代码都会执行
        //一般我们会在finally善后收尾的的代码写finally里面 比如释放资源
        System.out.println("不管try里面的代码有没有遇到异常,那么finally里面的代码都会执行");
    }
    System.out.println("下面的代码");
    System.out.println("下面的代码");
    System.out.println("下面的代码");
    System.out.println("下面的代码");
    System.out.println("下面的代码");
}

5_IO流(try…catch的方式处理异

try {
可能出现问题的代码 ;
}catch(异常名1 变量名1){
对异常的处理方式 ;
}catch (异常名2 变量名2){
对异常的处理方式 ;
}…

A:案例演示: try…catch的方式处理多个异常
注意事项:

​ 1:能明确的尽量明确,不要用大的来处理。
​ 2:平级关系的异常谁前谁后无所谓,如果出现了子父关系,父必须在后面。

public static void main(String[] args) {
    int[] arr = {20, 30};
    int a = 10;
    int b = 0;
    //ArrayIndexOutOfBoundsException
    //ArithmeticException
    try {
        arr = null;
        System.out.println(arr[0]);
        System.out.println(a / b);
        System.out.println(arr[3]);
    }catch (ArrayIndexOutOfBoundsException e){
        System.out.println("索引越界异常");
    }catch (ArithmeticException e){
        System.out.println("除数为0了");
    }catch (NullPointerException e){
        System.out.println("空指针异常");
    } catch (Exception e) {
        System.out.println("其他异常");
    }
    //注意:捕获多种异常,语法如上。需要注意的是,能明确的异常,尽量明确,不要拿一个最大的Exception一捕了之
    //捕获的多个异常中有父子继承关系,父类异常放到最后,并列关系的异常谁前谁后没关系
    //try里面放的是有可能出现问题的代码,对于没有可能出现问题的代码,就不要往里面放了

    System.out.println("后面的代码");
    System.out.println("后面的代码");
    System.out.println("后面的代码");
    System.out.println("后面的代码");
    System.out.println("后面的代码");
    System.out.println("后面的代码");
}

6_IO流(JDK7针对多个异常的处理方案)(了解)

JDK1.7中对多个catch的变形格式

try {

可能出现问题的代码 ;

}catch(异常名1 | 异常名2 | … 变量名){

对异常的处理方案 ;

}

好处: 就是简化了代码

弊端: 对多个异常的处理方式是一致的

注意事项:

多个异常之间只能是平级的关系,不能出现子父类的继承关系

A:案例演示: JDK7以后处理多个异常的方式及注意事项
注意事项:
	1.处理方式是一致的。(实际开发中,好多时候可能就是针对同类型的问题,给出同一个处理)
	2.多个异常间必须是平级关系。
public static void main(String[] args) {
    //JDK1.7之后提供了一种捕获多种异常的语法
    int[] arr = {20, 30};
    int a = 10;
    int b = 0;
    //ArrayIndexOutOfBoundsException
    //ArithmeticException
    try {
        //arr = null;
        System.out.println(a / b);
        System.out.println(arr[0]);

        System.out.println(arr[3]);
        //JDK1.7之后提供了一种捕获多种异常的语法 不推荐
    } catch (ArrayIndexOutOfBoundsException | NullPointerException | ArithmeticException e) {
        if (e instanceof ArrayIndexOutOfBoundsException) {
            System.out.println("索引越界异常");
        } else if (e instanceof NullPointerException) {
            System.out.println("空指针异常");
        } else if (e instanceof ArithmeticException) {
            System.out.println("数学异常");
        }

    }
    System.out.println("后面的代码");
    System.out.println("后面的代码");
    System.out.println("后面的代码");
    System.out.println("后面的代码");
    System.out.println("后面的代码");
    System.out.println("后面的代码");
}

7_IO流(编译期异常和运行期异常的区别)(理解)

A:编译期异常和运行期异常的区别

​ Java中的异常被分为两大类:编译时异常和运行时异常。
​ 所有的RuntimeException类及其子类的实例被称为运行时异常,其他的异常就是编译时异常

​ 编译时异常: Java程序必须显示处理,否则程序就会发生错误,无法通过编译
​ 运行时异常: 无需显示处理,也可以和编译时异常一样处理

B:案例演示: 编译期异常和运行期异常的区别
public static void main(String[] args) throws ParseException {
    //编译期异常:发生在编译期间,非RuntimeException及其子类。编译期异常必须手动处理,不处理程序无法运行。
    String dateStr="2020-=02-23";
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    //调用parse()方法时,会遇到一个编译期异常,编译期异常必须处理,你不处理程序运行。
    Date date= simpleDateFormat.parse(dateStr);
    System.out.println(date);
    //处理编译期异常:有两种方式
    //方式1:使用throws向上抛出,抛给调用者去处理,谁调用谁处理,俗称甩锅

    System.out.println("下面 代码");
}
public static void main(String[] args) {
    //运行期异常,发生在运行期间RuntimeException,及其子类属于运行期异常,对于运行期异常,可以选择解决,也可以不解决。
    int a = 10;
    int b = 0;
    //ArithmeticException 运行期异常,算术异常
    //自己处理这个运行期异常:遇到异常了,我们可以自己提示异常信息,但是不退出虚拟机,让代码继续往下执行
    //那怎么来处理呢?使用关键字try catch
    //try{}大括号里面放的是,有可能出现问题的代码
    //catch(捕获何种类型的异常类名){放的是一旦出现这种异常,你的处理逻辑是啥}
    //如果没有出现你要捕获的这种异常类型,那么catch里面的代码不会执行
    try{
        System.out.println(a / 0);
    }catch (ArithmeticException e){
        System.out.println("除数为0了");
    }

    System.out.println("后面代码");
    System.out.println("后面代码");
    System.out.println("后面代码");
    System.out.println("后面代码");
    System.out.println("后面代码");
    System.out.println("后面代码");
}

8_IO流(Throwable的几个常见方法)(理解)

A:Throwable的几个常见方法

​ a:getMessage(): 获取异常信息,返回字符串。
​ b:toString(): 获取异常类名和异常信息,返回字符串。
​ c:printStackTrace(): 获取异常类名和异常信息,以及异常出现在程序中的位置。返回值void。

B:案例演示: Throwable的几个常见方法的基本使用
public static void main(String[] args) {
    int a=10;
    int b=0;
    try {
        System.out.println(a / b);
    }catch (ArithmeticException e){
        //对于异常的处理,不要做空处理.哪怕只是一句输出语句的提示,都行
        //  System.out.println("除数为0了");
        e.printStackTrace();//打印异常的堆栈信息
        // System.out.println(e.getMessage());
       // System.out.println(e.toString());
    }

    System.out.println("下面的代码");
}

9_IO流(throws的方式处理异常)(理解)

A:throws的方式处理异常

​ 定义功能方法时,需要把出现的问题暴露出来让调用者去处理。
​ 那么就通过throws在方法上标识。

B:案例演示

​ 举例分别演示编译时异常和运行时异常的抛出

public static void main(String[] args) throws ParseException{
    String dateStr = "2020-02-23";
   Date date= parseDateStr(dateStr);
}

private static Date parseDateStr(String dateStr) throws ParseException {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    //编译期异常。抛出抛给调用者,谁调用谁处理
    Date date = simpleDateFormat.parse(dateStr);
    return date;
}

10_IO流(throw的概述以及和throws的区别)(掌握)

A:throw的概述:

在功能方法内部出现某种情况,程序不能继续运行,需要进行跳转时,就用throw把异常对象抛出。

B:案例演示:

分别演示编译时异常对象和运行时异常对象的抛出

C:throws和throw的区别
a:throws

​ 用在方法声明后面,跟的是异常类名
​ 可以跟多个异常类名,用逗号隔开
​ 表示抛出异常,由该方法的调用者来处理
​ throws表示出现异常的一种可能性,并不一定会发生这些异常

b:throw

​ 用在方法体内,跟的是异常对象名
​ 只能抛出一个异常对象名
​ 这个异常对象可以是编译期异常对象,可以是运行期异常对象
​ 表示抛出异常,由方法体内的语句处理
​ throw则是抛出了异常,执行throw则一定抛出了某种异常

public static void main(String[] args) throws NullPointerException,ArithmeticException,IndexOutOfBoundsException{
    //throws 和 throw的区别

    //相同点是都可以进行异常的抛出
    //throws用在方法声明上
    //throw使用用在方法内部抛出异常

   /* C:
    throws和throw的区别
    a:throws
    用在方法声明后面,跟的是异常类名
    可以跟多个异常类名,用逗号隔开
    表示抛出异常,由该方法的调用者来处理
    throws表示出现异常的一种可能性,并不一定会发生这些异常
    b:
    throw
            用在方法体内,跟的是异常对象名
            只能抛出一个异常对象名
    这个异常对象可以是编译期异常对象, 可以是运行期异常对象
    表示抛出异常,由方法体内的语句处理
    throw则是抛出了异常对象,
    执行throw则一定抛出了某种异常*/
    Scanner sc = new Scanner(System.in);
    System.out.println("请输入第一个整数");
    int one = sc.nextInt();
    System.out.println("请输入第一个整数");
    int two = sc.nextInt();

    double r = test(one, two);
    System.out.println(r);

}

private static double test(int one, int two){
    double r=0;
    if(two==0){
        //如果除数为0 直接抛出异常
        throw new ArithmeticException("除数为0了");
    }else{
       r= one / two;
    }
    return r;
}

11_IO流(finally关键字的特点及作用)(掌握)

A:finally的特点

​ 被finally控制的语句体一定会执行(前提 jvm没有停止)
​ 特殊情况:在执行到finally之前jvm退出了(比如System.exit(0))

B:finally的作用:

用于释放资源,在IO流操作和数据库操作中会见到

C:案例演示:

finally关键字的特点及作用

public static void main(String[] args) {
    int a=10;
    int b=0;
    try{
        //ctrl+shift+上下方向键,可以上下挪动一样代码
        System.out.println(a / 0);
    }catch (ArithmeticException e){
        System.out.println("除数为0了");
    }finally {
        //finally最终的: 不管try里面的代码有没有遇到异常,那么finally里面的代码都会执行
        //一般我们会在finally善后收尾的的代码写finally里面 比如释放资源
        System.out.println("不管try里面的代码有没有遇到异常,那么finally里面的代码都会执行");
    }
    System.out.println("下面的代码");
    System.out.println("下面的代码");
    System.out.println("下面的代码");
    System.out.println("下面的代码");
    System.out.println("下面的代码");
}

12_IO流(finally关键字的面试题)(掌握)

A:面试题1:	final,finally和finalize的区别
             
	 * 	final: 是一个状态修饰符, 可以用来修饰类 , 变量 , 成员方法. 
			被修饰的类不能被子类继承, 修饰的变量其实是一个常量不能被再次赋值
	 * 		修饰的方法不能被子类重写
	 * 	finally:用在try...catch...语句中 , 作用: 释放资源 . 特点: 始终被执行(JVM不能退出)
	 * 	finalize: Obejct类中的一个方法,用来回收垃圾
B:面试题2:	如果catch里面有return语句,请问finally的代码还会执行吗?如果会,请问是在return前还是return后。
		答:会执行, 在return前
		例子如下:
		try {
		System.out.println(23 / 0);
	} catch (Exception e) {
		System.out.println("哦,catch了...............");
		return ;
	}finally {
		System.out.println("哦,被执行了..............");
	}

13_IO流(自定义异常概述和基本使用)(掌握)

A:为什么需要自定义异常:
 因为在以后的开发过程中,我们可能会遇到各种问题,
而Jdk不可能针对每一种问题都给出具体的异常类与之对应.
    为了满足需求,我们就需要自定义异常.
举例:考试成绩必须在0-100之间,不满足就产生异常。
B:自定义异常概述 需要将我们自定义的异常类纳入到我们的异常体系中
	继承自Exception
	继承自RuntimeException
C:案例演示: 自定义异常的基本使用
       // 判断成绩范围
	if(score > 100 || score < 0){
		
		// 抛出一个异常对象
		//注意 自定义异常类 提供有参数构造 
		throw new MyException("成绩不在有效的范围内(0~100)....") ;
		
	}else {
		System.out.println("成绩合法.....................");
	}

14_IO流(自定义异常练习)(掌握)

A:案例演示: 需求:从银行取钱,发现钱不够,给出提示。
static double money = 100; //余额
public static void main(String[] args) {
    //自定义异常:在我们开发项目中,会遇到各种各样的异常,那么Java给我们提供的这些异常类,并不能完全描述我们所遇到的各种异常。
    //余额不足异常。  面部识别异常   指纹识别异常。
    //对于Java中没有提供的异常,但是我们业务需求,又要这种异常,我们可以自定义异常
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入你的取款金额"); //200
    double num = scanner.nextDouble();
    quKuan(num);

}

private static void quKuan(double num) {
    if(num<=money){
        money -= num;
        System.out.println("取款成功");
    }else{
       // System.out.println("余额不足");
        //余额不足,就要抛出异常,程序终止。
        throw new NoMoneyException("余额不足");
    }
}

15_IO流(异常的注意事项及如何使用异常处理)(了解)

A:异常注意事项(针对编译期异常)
	a:子类重写父类方法时,子类的方法必须抛出相同的异常或父类异常的子类,或者子类不抛出异常也是可以的。(父亲坏了,儿子不能比父亲更坏)
	b:如果父类抛出了多个异常,子类重写父类时,只能抛出相同的异常或者是他的子集,子类不能抛出父类没有的异常,或者子类不抛出异常也是可以的。
	c:如果被重写的方法没有异常抛出,那么子类的方法绝对不可以抛出异常,如果子类方法内有异常发生,那么子类只能try,不能throws
	
B:如何使用异常处理
		原则:如果该功能内部可以将问题处理,用try,如果处理不了,交由调用者处理,这是用throws
		区别:
			后续程序需要继续运行就try
			后续程序不需要继续运行就throws
			
如果JDK没有提供对应的异常,需要自定义异常。			

16_IO流(File类的概述和构造方法)(掌握)

A:File类的概述

​ 查看API
​ 文件和目录路径名的抽象表示形式
​ 这个File类可以用来表示文件,也可以用来表示目录

B:构造方法

​ File(String pathname):根据一个路径得到File对象
​ File(String parent, String child):根据一个目录和一个子文件/目录得到File对象
File(File parent, String child):根据一个父File对象和一个子文件/目录得到File对象

C:案例演示

​ File类的构造方法

  1. File file = new File(“E:\20151020\day02\day02总结.java”) ;

  2. File file2 = new File(“E:\20151020\day02” , “day02总结.java”) ;

  3. File file3 = new File(“E:\20151020\day02”) ;
    File file4 = new File(file3 , “day02总结.java”) ;

    public static void main(String[] args) {
        //IO流的作用,用来进行设备之间的一个数据传输
        //这些数据在我们电脑上是以文件的形式来体现的,比如文本文件,图片文件,视频文件,音频文件,等等。
        //Java为了描述文件或文件夹这个概念,给我们提供了一个类 File来描述文件或文件夹。
        //File 文件和目录路径名的抽象表示形式。
        //构造方法
     /*   File(File parent, String child)
        根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。
        File(String pathname)
        通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。
        File(String parent, String child)
        根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。*/
    
       /* File(String pathname)
        通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。*/
    
       //通过一个路径字符串,可以封装一个文件或文件夹
        File file = new File("C:\\Users\\ShenMouMou\\Desktop\\aaa.txt");
        System.out.println(file);
        //注意路径分隔符的写法
        File file2 = new File("C:/Users/ShenMouMou/Desktop/aaa.txt");
        System.out.println(file2);
    
        String pathSeparator = File.pathSeparator;
        System.out.println(pathSeparator);
    
    /*    static String pathSeparator
        与系统有关的路径分隔符,为了方便,它被表示为一个字符串。
        static char pathSeparatorChar
        与系统有关的路径分隔符。
        static String separator
        与系统有关的默认名称分隔符,为了方便,它被表示为一个字符串。
        static char separatorChar
        与系统有关的默认名称分隔符。*/
        System.out.println(File.pathSeparatorChar);
    
        System.out.println(File.separator);
        System.out.println(File.separatorChar);
    
        //封装一个目录
        File file1 = new File("C:\\Users\\ShenMouMou\\Desktop");
    
    }
    

17_IO流(File类的创建功能)(掌握)

A:创建功能

​ public boolean createNewFile():创建一个新的文件 如果存在这样的文件,就不创建了
​ public boolean mkdir():创建文件夹 如果存在这样的文件夹,就不创建了 注意这个方法只能创建单层目录 如果创建多层目录得一层一层创建
​ public boolean mkdirs():创建文件夹,如果父文件夹不存在,会帮你创建出来 可以创建多层目录 当然也可以创建单层目录

B:案例演示

​ File类的创建功能

​ 注意事项:如果你创建文件或者文件夹忘了写盘符路径,那么,默认在项目路径下。

​ 相对路径:没有带盘符的路径
​ 绝对路径:带有盘符的路径

public static void main(String[] args) {
   /* File(File parent, String child)
    根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。*/
    File fuPath = new File("C:\\Users\\ShenMouMou\\Desktop");
    //参数1:父路径的File类型,参数2 子文件名
    File file = new File(fuPath, "aaa.txt");
    System.out.println(file);


  /*  File(String parent, String child)
    根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。*/
    //参数1:父路径的Stirng类型 参数2:子文件名
    File file2 = new File("C:\\Users\\ShenMouMou\\Desktop", "aaa.txt");
    System.out.println(file2);


    File file3 = new File("C:\\Users\\ShenMouMou\\Desktop\\aaa.txt");

    //提供者三种构造方法。是为了我们编码中,能够灵活的去选用哪种构造
}
public static void main(String[] args) {
    //绝对路径和相对路径
    //绝对路径:带有盘符号的一个详细路径 例如:C:\Users\ShenMouMou\Desktop\aaa.txt
     // 绝对路径:D:\HBuilderX .1 .3 .2 .20181214.full\HBuilderX\iconengines

    //相对路径:不带有盘符号的路径

    //Java程序当中这个相对路径指的是哪段路径,你项目的根路径下

    //使用的是绝对路径来封装文件
    File file1 = new File("E:\\IdeaProjects\\20200523-File-类-下午\\a.txt");
    System.out.println(file1);

    File file2 = new File("E:\\IdeaProjects\\20200523-File-类-下午\\b.txt");
    System.out.println(file2);

    //如果这个文件在我们项目的文件夹的根目录下,可以使用相对路径来封装
    File file3 = new File("a.txt");
    System.out.println(file3);

    File file4 = new File("b.txt");
    System.out.println(file4);
    //./ 当前目录 项目的文件夹的根目录下
    File file = new File("./");
    System.out.println(file.getAbsolutePath());
}
public static void main(String[] args) throws IOException {
    //用的相对路径
    File file = new File("c.txt");
    //创建一个文件。返回值指的是文件是否创建成功 返回true代表创建成功
    //文件已经存在,你重复创建就返回false
    boolean b = file.createNewFile();
    System.out.println(b);
    System.out.println("========================");
    //用绝对路径
    File file2 = new File("E:\\IdeaProjects\\20200523-File-类-下午\\d.txt");
    boolean newFile = file2.createNewFile();
    System.out.println(newFile);

    //用的相对路径 ./ 当前路径 指的是项目文件夹的根目录下
    File file3 = new File("./e.txt");
    file3.createNewFile();

    //用的相对路径 ../ 表示上一级路径
    File file4 = new File("../f.txt"); //当前目录的上一级目录
    boolean newFile1 = file4.createNewFile();
    System.out.println(newFile1);
    //../../ 当前目录的上两级目录
    File file5 = new File("../../f.txt");
    boolean newFile5 = file5.createNewFile();
    System.out.println(newFile5);

}
public static void main(String[] args) {
   //在当前项目根目录下创建一个demo文件夹
    File file = new File("demo");
    //创建文件夹,创建成功返回true ,如果文件夹已经存在,你重复创建,就返回false
    boolean b = file.mkdir();
    System.out.println(b);
    //使用绝对路径,在桌面上创建一个文件夹
    File file2 = new File("C:\\Users\\ShenMouMou\\Desktop","demo");
    boolean mkdir = file2.mkdir();
    System.out.println(mkdir);
    //mkdir() 这个方法的不好之处,就是只能创建单级文件夹

    //如果要创建多级文件夹,我们可以使用另外一个方法

    File file3 = new File("C:\\Users\\ShenMouMou\\Desktop", "aaa/bbb/ccc");
   // mkdirs(); 可以创建多级或单级文件夹,记这个方法就行了
   // boolean mkdir1 = file3.mkdir();
    boolean mkdirs = file3.mkdirs();
    System.out.println(mkdirs);

}

18_IO流(File类的删除功能)

A:删除功能

​ public boolean delete():删除文件或者文件夹
​ 注意:删除文件夹时 这个文件夹是空文件夹 如果这个文件夹里面有文件,则不能删除

B:案例演示

​ File类的删除功能

注意事项:Java中的删除不走回收站。要删除一个文件夹,请注意该文件夹内不能包含文件或者文件夹
public static void main(String[] args) {
    //删除文件或文件夹
    File file = new File("a.txt");
    //删除文件,删除成功返回true,重复删除返回false
    boolean b = file.delete();
    System.out.println(b);

    File file1 = new File("img");
    file1.delete();
}

19_IO流(File类的重命名功能)

A:重命名功能

​ public boolean renameTo(File dest):把文件重命名为指定的文件路径

B:案例演示

​ File类的重命名功能

注意事项:
	如果路径名相同,就是改名。
	如果路径名不同,就是改名并剪切。
public static void main(String[] args) {
    File file = new File("aaa.txt");
    File mbFile = new File("bbb.txt");
    //重名名:源文件和传入进来的这个文件对象,都在相同路径下他就是重命名
    boolean b = file.renameTo(mbFile);
    System.out.println(b);
    System.out.println("======================================");
    //renameTo(mbFile)方法 :源文件和传入进来的这个文件,不在同一路径下,就是剪贴并重命名
    File file1 = new File("eee.txt");
    File file2 = new File("C:\\Users\\ShenMouMou\\Desktop\\fff.txt");
    boolean b1 = file1.renameTo(file2);
    System.out.println(b1);

}

20_IO流(File类的判断功能)

A:判断功能

​ public boolean isDirectory(): 判断是否是目录
​ public boolean isFile(): 判断是否是文件
​ public boolean exists(): 判断是否存在
​ public boolean canRead(): 判断是否可读
​ public boolean canWrite(): 判断是否可写
​ public boolean isHidden(): 判断是否隐藏
​ public boolean isAbsolute(); 判断是否使用的是绝对路径

B:案例演示

​ File类的判断功能

public static void main(String[] args) {
    //判断是不是个文件
    File file = new File("bbb.txt");
    boolean file1 = file.isFile();
    System.out.println(file1);
    //判断是不是目录
    File file2 = new File("out");
    boolean directory = file2.isDirectory();
    System.out.println(directory);

    System.out.println("================================");
    File file3 = new File("bbb.txt");
    //判断文件或文件夹是否存在
    boolean exists = file3.exists();
    System.out.println(exists);
}
public static void main(String[] args) {
    //判断文件是否可读
    File file = new File("C:\\Users\\ShenMouMou\\Desktop\\test.txt");
    boolean b = file.canRead();
    System.out.println(b);

    //判断文件是否可写
    boolean b1 = file.canWrite();
    System.out.println(b1);

    //判断文件是否隐藏
    boolean hidden = file.isHidden();
    System.out.println(hidden);
    //判断文件是否使用的是绝对路径
    boolean absolute = file.isAbsolute();
    System.out.println(absolute);

    File file1 = new File("bbb.txt");
    boolean absolute1 = file1.isAbsolute();
    System.out.println(absolute1);
}

21_IO流(File类的获取功能)

A:获取功能

​ public String getAbsolutePath(): 获取绝对路径
​ public String getPath(): 获取相对路径
​ public String getParent() 返回此抽象路径名父目录的路径名字符串;如果此路径名没有指定父目录,则返回 null。
​ public File getParentFile() 返回此抽象路径名父目录的抽象路径名;如果此路径名没有指定父目录,则返回 null。
​ public long getTotalSpace() 返回此抽象路径名指定的分区大小。 返回总容量 单位字节
​ public long getFreeSpace() 返回此抽象路径名指定的分区中未分配的字节数。返回剩余容量 单位字节
​ public String getName(): 获取名称
​ public long length(): 获取长度。字节数
​ public long lastModified(): 获取最后一次的修改时间,毫秒值
​ public String[] list(): 获取指定目录下的所有文件或者文件夹的名称数组
​ public File[] listFiles(): 获取指定目录下的所有文件或者文件夹的File数组

B:案例演示

​ File类的获取功能

public static void main(String[] args) {
    File file = new File("a.txt");
    //E:\IdeaProjects\20200524 - File - 类 - 上午\a.txt
    //获取文件的绝对路径,返回的是字符串类型
    String absolutePath = file.getAbsolutePath();
    System.out.println(absolutePath);
    System.out.println("==============================");
    //获取文件的绝对路径,返回的是File 这个方法比较灵活
    File absoluteFile = file.getAbsoluteFile();
    System.out.println(absoluteFile.toString());
    absoluteFile.delete();
}
public static void main(String[] args) {
    File file = new File("a.txt");
    //获取相对路径
    String path = file.getPath();
    System.out.println(path);
}
public static void main(String[] args) {
    File file = new File("E:\\IdeaProjects\\20200524-File-类-上午", "a.txt");
    //获取文件的父路径,返回的是字符串类型
    String parent = file.getParent();
    System.out.println(parent);
    System.out.println("==========================");
    ///获取文件的父路径,返回的是File类型 这个方法灵活
    File parentFile = file.getParentFile();
    System.out.println(parentFile.toString());
   // File file1 = new File(parentFile, "b.txt");

    System.out.println("=========================================");
    File file3 = new File("E:\\IdeaProjects\\20200524-File-类-上午\\a.txt");
    //获取文件的父路径
    String parent1 = file3.getParent();
    System.out.println(parent1);
    File parentFile1 = file3.getParentFile();
    System.out.println(parentFile1);
    System.out.println("======================");
    //在封装文件时,没有指定父路径,返回就是null
    File file1 = new File("a.txt");
    String parent2 = file1.getParent();
    System.out.println(parent2);
}
public static void main(String[] args) {
        //获取磁盘的总容量,返回的是字节
        File file = new File("D://");
        long totalSpace = file.getTotalSpace();
        System.out.println((totalSpace/1024/1024/1024.0)+"GB");

        //获取磁盘的剩余容量
        long freeSpace = file.getFreeSpace();
        System.out.println((freeSpace / 1024 / 1024 / 1024.0) + "GB");

        //已用容量=总容量-剩余容量




    }
public static void main(String[] args) {
    File file = new File("a.txt");
    //获取文件名
    String name = file.getName();
    System.out.println(name);
    //获取文件大小
    long length = file.length();
    System.out.println(length);
}
public static void main(String[] args) {
   // C:\Users\ShenMouMou\Desktop
    File file = new File("C:\\Users\\ShenMouMou\\Desktop\\day20.md");
    //获取文件的最后一次修改时间,返回的是毫秒值
    long time = file.lastModified();
    //System.out.println(time);
    //把毫秒值,转换成字符串,以年月日 时分秒来展示
    Date date = new Date(time);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String format = simpleDateFormat.format(date);
    //2019 - 10 - 30 13:48:40
    System.out.println(format);
}

22_IO流(输出指定目录下指定后缀的文件名)

A:案例演示:	需求:判断E盘目录下是否有后缀名为.jpg的文件,如果有,就输出该文件名称
        分析:
        a: 把E:\\demo这个路径封装成一个File对象
		b: 获取该路径下所有的文件或者文件夹对应的File数组
 		c: 遍历这个数组,进行判断

23_IO流(文件名称过滤器的概述及使用)

A:文件名称过滤器的概述
	public String[] list(FilenameFilter filter)
	public File[] listFiles(FilenameFilter filter)
B:文件名称过滤器的使用:	需求:判断E盘目录下是否有后缀名为.jpg的文件,如果有,就输出该文件名称
- 分析:
- a: 把E:\\demo这个路径封装成一个File对象
- b: 获取所有的以.jpg结尾的文件数组		(相当于过滤)
- c: 遍历数组
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值