19.01_IO流(IO流概述及其前奏)
A:IO流概述
IO流用来处理设备之间的数据传输
上传文件和下载文件,复制文件
B:IO流前奏
讲解IO流之前先讲解异常和File类
因为File表示的是IO流将来要操作的文件,所以我们需要学习File类。
而常见操作文件无非就是上传文件和下载文件,在这个操作的过程中可能出
现问题,出现问题后,我们需要对对应的代码进行处理。所以我们需要学习
异常。
19.02_异常的概述和分类
A:异常的概述: 异常就是Java程序在运行过程中出现的错误。
B:异常的分类: 举例:张三骑自行车旅游
C:异常的继承体系
异常的基类: Throwable
严重问题: Error 不予处理,因为这种问题一般是很严重的
问题。比如:内存溢出
非严重问题: Exception
编译时异常:非RuntimeException
运行时异常:RuntimeException
public static void main ( String[ ] args) {
int a = 1 ;
int b = 0 ;
System. out. println ( a/ b) ;
System. out. println ( "下面写了很多代码" ) ;
System. out. println ( "下面写了很多代码" ) ;
System. out. println ( "下面写了很多代码" ) ;
}
19.03_JVM默认是如何处理异常的
A:JVM默认是如何处理异常的
main函数收到这个问题时,有两种处理方式:
a:自己将该问题处理,然后继续运行
b:自己没有针对的处理方式,只有交给调用main的jvm来处理
JVM有一个默认的异常处理机制,就将该异常进行处理
并将该异常的名称,异常的星系,异常出现的位置打印在了控制台上,同时
将程序停止运行
19.04_try…catch的方式处理异常
A:异常处理的两种方式
a:try...catch...finally
b:throws
B:try...catch处理异常的基本格式
try{
可能出现问题的代码;
}catch(异常名 变量名){
针对问题的处理;
}finally{
释放资源;
}
变形格式:
try{
可能出现问题的代码;
}catch(异常名 变量名){
针对问题的处理;
}
注意事项:
a:try中的代码越少越好
b:catch中要做处理,哪怕是一条输出语句也可以.(不能将异常信息隐藏)
C:try...catch处理异常的基本格式2
try{
可能出现问题的代码;
}catch(异常名1 变量名1){
对异常的处理方式;
}catch(异常名2 变量名2){
对异常的处理方式;
}....
public static void main ( String[ ] args) {
int a = 1 ;
int b = 0 ;
try {
System. out. println ( a/ 1 ) ;
} catch ( ArithmeticException e) {
System. out. println ( "初始为0了" ) ;
}
System. out. println ( "下面写了很多代码" ) ;
System. out. println ( "下面写了很多代码" ) ;
System. out. println ( "下面写了很多代码" ) ;
}
public static void main ( String[ ] args) {
int [ ] arr = { 1 , 2 , 3 } ;
arr = null;
try {
System. out. println ( arr. length) ;
arr[ 10 ] = 30 ;
int a = 1 / 0 ;
} catch ( ArithmeticException e) {
System. out. println ( "初始为0了" ) ;
} catch ( ArrayIndexOutOfBoundsException e) {
System. out. println ( "角标越界了" ) ;
} catch ( NullPointerException e) {
System. out. println ( "空指针异常" ) ;
} catch ( Exception e) {
System. out. println ( "其他异常" ) ;
}
System. out. println ( "下面的代码" ) ;
System. out. println ( "下面的代码" ) ;
System. out. println ( "下面的代码" ) ;
}
public static void main ( String[ ] args) {
int [ ] arr = { 1 , 2 , 3 } ;
arr = null;
try {
System. out. println ( arr. length) ;
arr[ 10 ] = 30 ;
int a = 1 / 0 ;
} catch ( ArithmeticException e) {
System. out. println ( "除数为0了" ) ;
} catch ( ArrayIndexOutOfBoundsException e) {
System. out. println ( "角标越界了" ) ;
} catch ( NullPointerException e) {
System. out. println ( "空指针异常" ) ;
} catch ( Exception e) {
System. out. println ( "其他异常" ) ;
}
System. out. println ( "下面的代码" ) ;
System. out. println ( "下面的代码" ) ;
System. out. println ( "下面的代码" ) ;
}
public static void main ( String[ ] args) {
int [ ] arr = { 1 , 2 , 3 } ;
arr = null;
try {
System. out. println ( arr. length) ;
arr[ 10 ] = 30 ;
int a = 1 / 0 ;
} catch ( ArithmeticException | NullPointerException | ArrayIndexOutOfBoundsException e) {
if ( e instanceof ArithmeticException ) {
System. out. println ( "除数为0了" ) ;
} else if ( e instanceof NullPointerException ) {
System. out. println ( "空指针异常了" ) ;
} else if ( e instanceof ArrayIndexOutOfBoundsException ) {
System. out. println ( "空指针异常了" ) ;
} else if ( e instanceof ArrayIndexOutOfBoundsException ) {
System. out. println ( "角标越界异常了" ) ;
}
}
}
19.05_编译期异常和运行期异常的区别
A:编译期异常和运行期异常的区别
Java中的异常被分为两大类:编译时异常和运行时异常
所有的RuntimeException类及其子类的实例被称为运行时异常,其他的异
常就是编译时异常
编译时异常: Java程序必须显示处理,否则程序就会发生错误,无法通过
编译
运行时异常: 无需显示处理,也可以和编译时异常一样处理
19.06_Throwable的几个常见方法
A:Throwable的几个常见方法
a:getMessage(): 获取异常信息,返回字符串
b:toString(): 获取异常类名和异常信息,返回字符串
c:printStackTrace(): 获取异常类名和异常信息,以及异常出现在程
序中的位置。返回值void
public static void main ( String[ ] args) {
int a = 10 ;
int b = 5 ;
try {
System. out. println ( a / 0 ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
System. out. println ( "aaaaa" ) ;
}
public static void main ( String[ ] args) {
while ( true ) {
Scanner scanner = new Scanner ( System. in) ;
System. out. println ( "请输入一个整数" ) ;
try {
int i = scanner. nextInt ( ) ;
System. out. println ( i) ;
break ;
} catch ( InputMismatchException e) {
System. out. println ( "你输入的类型不正确,请重新输入" ) ;
}
}
}
public static void main ( String[ ] args) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
try {
Date date = simpleDateFormat. parse ( "2020-10=10" ) ;
} catch ( ParseException e) {
e. printStackTrace ( ) ;
}
System. out. println ( "下面的代码" ) ;
System. out. println ( "下面的代码" ) ;
System. out. println ( "下面的代码" ) ;
System. out. println ( "下面的代码" ) ;
System. out. println ( "下面的代码" ) ;
}
19.07_throws的方式处理异常
A:throws的方式处理异常
定义功能方法时,需要把出现的问题暴露出来让调用者去处理
那么就通过throws在方法上标识
19.08_throw概述及和throws的区别
A:throw的概述: 在功能方法内部出现某种情况,程序不能继续运行,需要进
行跳转时,就用throw把异常对象抛出
B:throws和throw的区别
a:throws
用在方法声明后面,跟的是异常类名,可以跟多个异常类名,
用逗号隔开;表示抛出异常,由该方法的调用者来处理;
throws表示出现异常的一种可能性,并不一定会发生这些异常
b:throw
用在方法体内,跟的是异常对象名;只能抛出一个异常对象名;
这个异常对象可以是编译期异常对象,可以是运行期异常对象;
表示抛出异常,由方法体内的语句处理;
throw是抛出了异常,执行throw一定抛出了某种异常
public static void main ( String[ ] args) {
try {
show ( ) ;
} catch ( ParseException e) {
e. printStackTrace ( ) ;
}
System. out. println ( "下面代码" ) ;
System. out. println ( "下面代码" ) ;
System. out. println ( "下面代码" ) ;
System. out. println ( "下面代码" ) ;
System. out. println ( "下面代码" ) ;
System. out. println ( "下面代码" ) ;
System. out. println ( "下面代码" ) ;
}
private static void show ( ) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
Date date = simpleDateFormat. parse ( "2020-10=10" ) ;
}
public static void main ( String[ ] args) {
double r = division ( 5 , 0 ) ;
System. out. println ( "r = " + r) ;
System. out. println ( "下面的代码r=" + r) ;
System. out. println ( "r = " + r) ;
}
private static double division ( int a, int b) {
double r = 0 ;
if ( b == 0 ) {
throw new ArithmeticException ( "除数为0了" ) ;
} else {
r = a/ b;
}
return r;
}
19.09_finally关键字的特点及作用
A:finally的特点
被finally控制的语句体一定会执行(前提:JVM没有停止)
特殊情况:在执行到finally之前JVM退出了(比如System.exit(0))
B:finally的作用:用于释放资源,在IO流操作和数据库操作中会见到
C:面试题1: final,finally和finalize的区别
* final:是一个状态修饰符,可以用来修饰类、变量、成员方法。
被修饰的类不能被子类继承,修饰的变量其实是一个常量
不能再次被赋值;修饰的方法不能被子类重写
* finally:用在try...catch...语句中,作用:释放资源,
特点:始终被执行(JVM不能退出)
* finalize:Object类中的一个方法,用来回收垃圾
面试题2: 如果catch里面有return语句,请问finally的代码还会执行吗?
如果会,请问在return前还是return后
答:会执行,在return前
public static void main ( String[ ] args) throws ParseException {
try {
System. out. println ( 1 / 1 ) ;
} catch ( ArithmeticException e) {
System. out. println ( "catch里面的代码执行了" ) ;
} finally {
System. out. println ( "不管你 try里面有没有遇到异常finally里面的代码都会执行" ) ;
}
}
public static void main ( String[ ] args) throws ParseException {
Scanner scanner = null;
try {
System. out. println ( 1 / 0 ) ;
scanner = new Scanner ( System. in) ;
System. out. println ( "请输入一个整数" ) ;
int i = scanner. nextInt ( ) ;
System. out. println ( i) ;
} catch ( InputMismatchException e) {
e. printStackTrace ( ) ;
} finally {
System. out. println ( "finally里面的代码执行了" ) ;
if ( scanner != null) {
scanner. close ( ) ;
}
}
}
public static void main ( String[ ] args) {
ArrayList< Object> list = new ArrayList < > ( ) ;
list. add ( 20 ) ;
Iterator< Object> iterator = list. iterator ( ) ;
}
public static void main ( String[ ] args) {
System. gc ( ) ;
try {
System. out. println ( 23 / 0 ) ;
} catch ( Exception e) {
System. out. println ( "哦,catch了..............." ) ;
return ;
} finally {
System. out. println ( "哦,被执行了.............." ) ;
}
}
19.10_自定义异常概述和基本使用
A:为什么需要自定义异常
因为在以后的开发过程中,我们可能会遇到各种问题,而JDK不可能针对每
一种问题都给出具体的异常类与之对应。为了满足要求,我们就需要自定义
异常
B:自定义异常概述
需要将我们自定义的异常类纳入到我们的异常体系中
继承自Exception
继承自RuntimeException
public class MyTest {
static int money = 100 ;
public static void main ( String[ ] args) {
withdrawal ( ) ;
System. out. println ( "aaaaaa" ) ;
}
private static void withdrawal ( ) {
Scanner scanner = new Scanner ( System. in) ;
System. out. println ( "请输入你的取款金额" ) ;
int num = scanner. nextInt ( ) ;
if ( num <= money) {
money -= num;
System. out. println ( "取款成功" ) ;
} else {
throw new NoMonenyException ( "余额不足异常" ) ;
}
}
}
public class NoMonenyException extends RuntimeException {
public NoMonenyException ( ) {
super ( ) ;
}
public NoMonenyException ( String message) {
super ( message) ;
}
}
public static void main ( String[ ] args) {
Scanner scanner = new Scanner ( System. in) ;
System. out. println ( "请输入你的考试成绩 0-100" ) ;
int score = scanner. nextInt ( ) ;
showScore ( score) ;
}
private static void showScore ( int score) {
if ( score > 100 || score < 0 ) {
throw new ScoreException ( "成绩不在有效的范围内(0~100)...." ) ;
} else {
System. out. println ( "成绩合法....................." ) ;
}
}
public class ScoreException extends RuntimeException {
public ScoreException ( ) {
}
public ScoreException ( String message) {
super ( message) ;
}
}
19.11_异常的注意事项及如何使用异常处理
A:异常注意事项(针对编译期异常)
a:子类重写父类方法时,子类的方法必须抛出相同的异常或父类异常的子
类,或者子类不抛出异常也是可以的。(父亲坏了,儿子不能比父亲更坏)
b:如果父类抛出多个异常,子类重写父类时,只能抛出相同的异常或者是
他的子集,子类不能抛出父类没有的异常,或者子类不抛出异常也是可以的
c:如果被重写的方法没有异常抛出,那么子类的方法绝对不可以抛出异常,
如果子类方法内有异常发生,那么子类只能try,不能throws
B:如何使用异常处理
原则:如果该功能内部可以将问题处理,用try,如果处理不了,交由
调用者处理,这是用throws
区别:
后续程序需要继续运行就try
后续程序不需要继续运行就throws
public class MyTest {
public static void main ( String[ ] args) {
}
}
abstract class Fu {
public abstract void show ( ) throws ParseException, ArithmeticException, ActivationException;
}
class Zi extends Fu {
@Override
public void show ( ) throws ParseException, ActivationException {
new SimpleDateFormat ( "yyyy-MM-dd" ) . parse ( "2020-10-10" ) ;
}
}
public static void main ( String[ ] args) {
int a = 10 ;
int b = 20 ;
try {
System. out. println ( a / b) ;
} catch ( ArithmeticException e) {
e. printStackTrace ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
System. out. println ( "下面的代码" ) ;
try {
new SimpleDateFormat ( "yyyy-MM-dd" ) . parse ( " " ) ;
} catch ( ParseException e) {
e. printStackTrace ( ) ;
}
System. out. println ( "下面的代码" ) ;
System. out. println ( "下面的代码" ) ;
System. out. println ( "下面的代码" ) ;
System. out. println ( "下面的代码" ) ;
System. out. println ( "下面的代码" ) ;
}
19.12_File类的概述和构造方法
A:File类的概述
查看API
文件和目录路径的抽象表示形式
这个File类可以用来表示文件,也可以用来表示目录
B:构造方法
File(String pathname):根据一个路径得到File对象
File(String parent,String child):根据一个目录和一个子文件/
目录得到File对象
File(File parent,String child):根据一个父File对象和一个子
文件/目录得到File对象
public static void main ( String[ ] args) {
File file = new File ( "C:\\Users\\57642\\Desktop\\a.txt" ) ;
File file1 = new File ( "b.txt" ) ;
}
public static void main ( String[ ] args) {
File file = new File ( "C:\\Users\\57642\\Desktop\\a.txt" ) ;
File file1 = new File ( "C:\\Users\\57642\\Desktop" , "a.txt" ) ;
}
public static void main ( String[ ] args) {
File file = new File ( "C:\\Users\\57642\\Desktop\\a.txt" ) ;
File file1 = new File ( "C:\\Users\\57642\\Desktop" , "a.txt" ) ;
File parentFile = new File ( "C:\\Users\\57642\\Desktop" ) ;
File file2 = new File ( parentFile, "a.txt" ) ;
File file3 = new File ( "C:\\Users\\57642\\Desktop\\a.txt" ) ;
File file4 = new File ( "C:/Users/57642/Desktop/a.txt" ) ;
}
19.13_File类的创建、删除和重命名功能
A:创建功能
public boolean createNewFile():创建一个新的文件 如果存在这样的
文件,就不创建了
public boolean mkdir():创建文件夹 如果存在这样的文件夹,就不创建
了 注意这个方法只能创建单层目录 如果创建
多层目录得一层一层创建
public boolean mkdirs():创建文件夹,如果父文件夹不存在,会帮你
创建出来 可以创建多层目录 当然也可以创
建单层目录
B:删除功能
public boolean delete():删除文件或者文件夹
注意:删除文件夹时,这个文件夹是空文件夹 如果这个文件夹里面有文件,
则不能删除
C:重命名功能
public boolean rename(File deat):把文件重命名为指定的文件路径
public static void main ( String[ ] args) throws IOException {
File file = new File ( "a.txt" ) ;
File file2 = new File ( "E:\\IdeaProjects\\20210103-File-类-下午\\b.txt" ) ;
boolean b = file. createNewFile ( ) ;
System. out. println ( b) ;
boolean b2 = file2. createNewFile ( ) ;
System. out. println ( b2) ;
}
public static void main ( String[ ] args) throws IOException {
File file = new File ( "C:\\Users\\57642\\Desktop\\abc" ) ;
boolean b = file. mkdir ( ) ;
System. out. println ( b) ;
File file1 = new File ( "C:\\Users\\57642\\Desktop" , "abc\\aa\\bb" ) ;
boolean b1 = file1. mkdirs ( ) ;
System. out. println ( b1) ;
}
public static void main ( String[ ] args) throws IOException {
File file = new File ( "b.txt" ) ;
boolean b = file. createNewFile ( ) ;
System. out. println ( b) ;
boolean b1 = file. delete ( ) ;
System. out. println ( b1) ;
File file1 = new File ( "C:\\Users\\57642\\Desktop\\abc\\aa\\bb" ) ;
boolean b2 = file1. delete ( ) ;
System. out. println ( b2) ;
}
public static void main ( String[ ] args) throws IOException {
File file = new File ( "c.txt" ) ;
File file1 = new File ( "C:\\Users\\57642\\Desktop\\bb.txt" ) ;
boolean b = file. renameTo ( file1) ;
System. out. println ( b) ;
}
public static void main ( String[ ] args) throws IOException {
File file = new File ( "new.txt" ) ;
file. createNewFile ( ) ;
File file2 = new File ( "./new2.txt" ) ;
file2. createNewFile ( ) ;
File file3 = new File ( "../new.txt" ) ;
file3. createNewFile ( ) ;
File file4 = new File ( "../../new.txt" ) ;
file4. createNewFile ( ) ;
}
19.14_File类的判断和获取功能
A:判断功能
public boolean isDirectory():判断是否是目录
public boolean isFile():判断是否是文件
public boolean exists():判断是否存在
public boolean canRead():判断是否可读
public boolean canWrite():判断是否可写
public boolean isHidden():判断是否隐藏
public boolean isAbsolute():判断是否使用的是绝对路径
B:获取功能
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数组
public static void main ( String[ ] args) throws IOException {
File file = new File ( "new.txt" ) ;
file. createNewFile ( ) ;
if ( file. isFile ( ) ) {
System. out. println ( "是文件" ) ;
}
if ( file. isDirectory ( ) ) {
System. out. println ( "是目录" ) ;
} else {
System. out. println ( "不是目录" ) ;
}
if ( file. exists ( ) ) {
System. out. println ( "文件存在" ) ;
}
}
public static void main ( String[ ] args) throws IOException {
File file = new File ( "C:\\Users\\57642\\Desktop\\a.txt" ) ;
if ( file. isAbsolute ( ) ) {
System. out. println ( "是绝对路径" ) ;
} else {
System. out. println ( "不是绝对路径" ) ;
}
if ( file. canRead ( ) ) {
System. out. println ( "可读" ) ;
}
if ( file. canWrite ( ) ) {
System. out. println ( "可写" ) ;
} else {
System. out. println ( "不可写" ) ;
}
if ( file. isHidden ( ) ) {
System. out. println ( "隐藏文件" ) ;
} else {
System. out. println ( "不是隐藏文件" ) ;
}
}
public static void main ( String[ ] args) throws IOException {
File file = new File ( "C:\\Users\\57642\\Desktop\\a.txt" ) ;
long size = file. length ( ) ;
System. out. println ( size) ;
String name = file. getName ( ) ;
System. out. println ( name) ;
String parent = file. getParent ( ) ;
System. out. println ( parent) ;
File parentFile = file. getParentFile ( ) ;
System. out. println ( parentFile. toString ( ) ) ;
}
public static void main ( String[ ] args) throws IOException {
File file = new File ( "C:\\Users\\57642\\Desktop\\a.txt" ) ;
String absolutePath = file. getAbsolutePath ( ) ;
System. out. println ( absolutePath) ;
File absoluteFile = file. getAbsoluteFile ( ) ;
System. out. println ( absoluteFile) ;
File file1 = new File ( "new.txt" ) ;
String path = file1. getPath ( ) ;
System. out. println ( path) ;
}
public static void main ( String[ ] args) throws IOException {
File file = new File ( "F:\\" ) ;
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) throws IOException {
File file = new File ( "C:\\Users\\57642\\Desktop\\a.txt" ) ;
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) ;
System. out. println ( format) ;
}
19.15_文件名称过滤器的概述及使用
A:文件名称过滤器的概述
public String[] list(FilenameFilter filter)
public File[] listFiles(FilenameFilter filter)
public static void main ( String[ ] args) throws IOException {
File file = new File ( "C:\\Users\\57642\\Desktop\\abc" ) ;
String[ ] list = file. list ( ) ;
for ( String s : list) {
System. out. println ( s) ;
}
}
public static void main ( String[ ] args) throws IOException {
File file = new File ( "C:\\Users\\57642\\Desktop\\abc" ) ;
File[ ] files = file. listFiles ( ) ;
for ( File file1 : files) {
System. out. println ( file1. getName ( ) + "=====" + file1. length ( ) ) ;
if ( file1. getName ( ) . endsWith ( ".png" ) ) {
file1. delete ( ) ;
}
}
}
public static void main ( String[ ] args) throws IOException {
File file = new File ( "C:\\Users\\57642\\Desktop\\abc" ) ;
File[ ] files = file. listFiles ( ) ;
for ( File file1 : files) {
if ( file1. getName ( ) . endsWith ( ".jpg" ) && file1. isFile ( ) ) {
System. out. println ( file1. getName ( ) ) ;
}
}
}
public static void main ( String[ ] args) throws IOException {
File file = new File ( "C:\\Users\\57642\\Desktop\\abc" ) ;
File[ ] files = file. listFiles ( new FilenameFilter ( ) {
@Override
public boolean accept ( File dir, String name) {
File file1 = new File ( dir, name) ;
if ( file1. isFile ( ) && name. endsWith ( ".jpg" ) ) {
return true ;
}
return false ;
}
} ) ;
System. out. println ( Arrays. toString ( files) ) ;
}
public static void main ( String[ ] args) throws IOException {
File file = new File ( "C:\\Users\\57642\\Desktop\\abc" ) ;
File[ ] files = file. listFiles ( new FileFilter ( ) {
@Override
public boolean accept ( File f) {
return f. isFile ( ) && f. getName ( ) . endsWith ( ".jpg" ) ;
}
} ) ;
for ( File file1 : files) {
System. out. println ( file1) ;
}
}