目录
ArrayList和LinkedList各自的工作原理分析原理分析
HashSet和LinkedHashSet各自的工作原理分析
第四部分:异常
异常的概念
异常的定义: 异常指不期而至的各种状况,如:文件找不到、网络连接失败、除0操作、非法参数等。 异常是一个事件,它发生在程序运行期间,干扰了正常的指令流程。
常见的异常
算术异常类:ArithmeticExecption
空指针异常类:NullPointerException
类型强制转换异常:ClassCastException
数组负下标异常:NegativeArrayException
数组下标越界异常:ArrayIndexOutOfBoundsException
字符串转换为数字异常:NumberFormatException
操作数据库异常:SQLException
异常的处理方式
1、try....catch方法处理异常,如下所示:
public class Demo {
public static void main(String[] args) {
try{
int a = 5/0;
}catch (ArithmeticException e){
//ArithmeticException 算术异常
System.out.println("除数不能为0");
}
}
}
//输出结果:除数不能为0
2、throws扔出异常,将异常抛出去留给JVM处理异常
public class Demo {
public static void main(String[] args) throws ArithmeticException{
int a = 5/0;
}
}
3、try...catch....finally finally中的代码块不管是否捕获异常都会执行,常用来放一些关闭数据库资源的方法。
自定义异常
public class MyException extends Exception{
//自定义异常
public MyException(){
super();
}
public MyException(String message){
super(message);
}
}
在我们需要调用该异常时需要用到throw和throws关键字,如下所示:
public class Demo {
public static void main(String[] args) throws MyException{//抛出自定义异常
int number = 0;
if (number==0){
throw new MyException("这是一个自定义异常");
}
}
}
异常的分类
异常的理解
常见异常介绍
运行时异常
编译时异常
运行和编译异常区别
异常的处理方式之捕获异常
异常的处理方式之抛出异常
异常的处理方式之断点调试
断点调试之引导运行步骤
自定义异常
异常关键字处理
关键字执行流程
异常总结
第五部分:集合
集合的概念和作用
集合和数组的区别
集合框架体系介绍
集合框架之Collection接口
ArrayList和LinkedList的方法使用
ArrayList和LinkedList各自的工作原理分析原理分析
使用多种方式遍历集合
HashSet和LinkedHashSet各自的工作原理分析
集合框架之Map接口
泛型的使用
Collections工具类的使用
第六部分:IO流
File类的作用
-
一个File对象而可以代表一个文件或目录
-
File可以实现获取文件和目录属性等功能
-
可以实现对文件和目录的创建,删除等功能
File类操作目录与文件的常用方法有哪些
文件方法:
-
对象名.createNewFile( ) //创建新文件。
-
对象名.delete( ) //直接从磁盘上删除
-
对象名.exists( ) //查询磁盘中的文件是否存在
-
对象名.getAbsolutePath( ) //获取绝对路径
-
对象名.getPath( ) //获取相对路径
-
对象名.getName( ) //获取文件名 相当于调用了一个toString方法。
-
对象名.isFile( ) //判断是否是文件
-
对象名.length( ) //查看文件中的字节数
目录方法:
-
对象名.exists( ) //查询目录是否存在
-
对象名.isDirectory( ) //查询文件文件夹是否存在
-
对象名.isFile( ) //查询文件是否存在
-
对象名.mkdir( ) //创建单层目录
-
对象名.mkdirs( ) //创建多层目录
-
对象名.delete( ) //删除目录
-
对象名.getParentFile( ) //获取cc目录的父级目录。
-
对象名.list( ) //返回一个字符串数组,命名由此抽象路径名表示的目录中的文件和目录。
-
对象名.listFiles //返回一个字符串。命名由此抽象路径名表示的目录中的文件和目录。可以获取相对路径和绝对路径,可以对文件进行操作。
File类中常用方法的使用介绍
使用File类操作文件和文件夹
相对路径和绝对路径的介绍
IO流的概念和工作原理
IO流的分类
简介:
IO就是Input和Output(输入和输出)。
流指的就是流向,比如数据写入内存就是流入,数据写出内存就是流出。
从流向上区分,分为输入流和输出流。
从IO流处理的数据类型上分为:字节流,字符流,缓冲流。
分类:
1、字节流:
字节输入流InputStream
常用方法:
int read();
int read(byte[] b);
void close();
常用子类:FileInputStream
构造方法:
public FileInputStream(String pathname);
public FileInputStream(File pathname);
字节输出流OutputStream
常用方法:
void write(int i);
void write(byte[] b);
void write(byte[] b,int offset,int len);
void flush();
void close();
常用子类:FileOutputStream
构造方法:
public FileOutputStream(String pathname);
public FileOutputStream(File pathname);
public FileOutputStream(String pathname,booleanappend);
public FileOutputStream(File pathname,boolean append);
2、字符流:
字符输入流Reader
常用方法:
int read();
int read(char[] ch);
void close();
常用子类:FileReader
构造方法:
public FileReader(String pahtname);
public FileReader(File pahtname);
字符输出流Writer
成员方法:
void write(int b);
void write(char[] ch);
void write(char[] ch,int offset,int len);
void write(String str);
void write(String str,int offset,int len);
void flush();
void close();
常用子类:FileWriter
构造方法:
public FileWriter(String pathname);
public FileWriter(File pathname);
public FileWriter(String pathname,booleanappend);
public FileWriter(File pathname,boolean append);
缓冲流:
字节输入流(InputStream):
常用子类:
FileInputStream
BufferedInputStream:
构造方法:
public BufferedInputStream(InputStream is);
public BufferedInputStream(InputStream is,int size);
字节输出流(OutputStream):
常用子类:
FileOutputStream
BufferedOutputStream:
构造方法:
public BufferedOutputStream(OutputStream os);
public BufferedOutputStream(OutputStream os,int size);
字符输入流(Reader):
常用子类:
FileReader
BufferedReader:
构造方法:
public BufferedReader(Reader r);
public BufferedReader(Reader r,int size);
字符输出流(Writer):
常用子类:
FileWriter
BufferedWriter
构造方法:
public BufferedWriter(Writer w);
public BufferedWriter(Writer w,int size);
注意事项:
1.底层数据的传输来说只有字节流,没有字符流。
2.字符输入流和缓冲字符输入流读取数据,读取的次数是一样的,高效体现在字节到字符的转换。
3.字符缓冲输出流的高效体现在,字符到字节的转换上,写出次数这两种(FileWriter、BufferedWriter)是一样的