捕获异常:
一.异常捕获:
- 如果某个异常发生时没有在任何地方进行捕获,那程序就会终止执行,并在控制台上打印出异常信息,其中包括异常的类型和堆栈的内容。
- catch 可以出现1-N
- 所有的异常都是Exception的子类
- 捕获异常的时候,先子后父
- 想要捕获一个异常,必须设置try/catch语句块,如下方
public static void main(String[] args) {
try {
//对下面的代码进行监控,如果出问题了就进行处理(catch)
String s = null ;
s.equals("");// 当执行到有异常的代码,就会进行异常捕获处理,下面的代码就不会执行了。
System.out.println(5/0);
System.out.println(args[2]);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("--catch ArrayIndexOutOfBoundsException ");
e.printStackTrace();
} catch (NullPointerException e) {
System.out.println("--catch NullPointerException ");
e.printStackTrace();
} catch(ArithmeticException e){
System.out.println("--catch ArithmeticException ");
e.printStackTrace();
}catch(Exception e){
System.out.println("--catch Exception ");
e.printStackTrace();
}
}
}
自定义异常
//自定义异常 自定义年龄异常
public class AgeOutOfBoundException extends RuntimeException {
public AgeOutOfBoundException(){
super();
}
public AgeOutOfBoundException(String message){
super(message);
}
}
public static void main(String[] args) {
methodA();
}
public static void methodA(){
try {
methodB();
} catch (AgeIndexOutofRange e) {
System.out.println("-----抓到你了-----");
e.printStackTrace();
} catch (Weightout e) {
e.printStackTrace();
}
}
public static void methodB() throws AgeIndexOutofRange, Weightout{
int age=-20;
if(age>=0){
System.out.println("年龄合法");
}else{
// 通过throws把异常抛出去,自己不处理,让调用者来决定如何处理。
// 抛出多个异常,多个异常之间用,号隔开
throw new AgeIndexOutofRange("你确定年龄有负数?");
}
}
输出:
-----抓到你了-----
com.hp.kk02.AgeIndexOutofRange: 你确定年龄有负数?
at com.hp.kk02.Demo01.methodB(Demo01.java:27)
at com.hp.kk02.Demo01.methodA(Demo01.java:12)
at com.hp.kk02.Demo01.main(Demo01.java:7)
文件
public static void main(String[] args) throws IOException {
File f=new File("d:/aaa.txt");
File f1=new File("d:/xxx.txt");
//测试此抽象路径名表示的文件或目录是否存在。
//System.out.println(f.exists());
//是否可执行
// System.out.println(f.canExecute());
//是否可读
// System.out.println(f.canRead());
//是否可写
// System.out.println(f.canWrite());
//是否存在
// if(!f1.exists()){
// //不存在,创建新的
// f1.createNewFile();
// }
// //是否存在
// if(f1.exists()){
// //存在删除
// f1.delete();
// }
// 测试此抽象路径名与给定对象是否相等。
// System.out.println(f.equals(f1));
// 返回此抽象路径名的绝对路径名形式。
// File f2=new File("C://tomcat");
// File f3=new File("d://11.19/1/2/3/4/56/7");
// System.out.println(f2.getAbsolutePath());
//返回此抽象路径名指定的分区中未分配的字节数(返回当前盘符可用内存)
// System.out.println(f2.getFreeSpace()/1024/1024/1024.0);
//返回由此抽象路径名表示的文件或目录的名称。
// System.out.println(f2.getName());
//返回此抽象路径名父目录的路径名字符串
// System.out.println(f2.getParent());
// 返回此抽象路径名父目录的抽象路径名
// File a=f2.getParentFile();
// System.out.println(a);
// 测试此抽象路径名是否为绝对路径名。
// System.out.println(f2.isAbsolute());
// 测试此抽象路径名表示的文件是否是一个目录。
// System.out.println(f2.isDirectory());
//测试此抽象路径名表示的文件是否是一个标准文件。
// System.out.println(f2.isFile());
// 测试此抽象路径名指定的文件是否是一个隐藏文件。
// System.out.println(f2.isHidden());
//返回此抽象路径名表示的文件最后一次被修改的时间。
// System.out.println(f2.lastModified());
//返回由此抽象路径名表示的文件的长度。
// System.out.println(f2.length());
//返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录。
// String [] a=f2.list();
// for(String b:a){
// System.out.println(b);
// }
//返回一个抽象绝对路径名数组
// File [] a=f2.listFiles();
// for(File b:a){
// System.out.println(b);
// }
//在当前目录下创建新的目录
// f2.mkdir();
// f3.mkdirs();
// f3.setReadable(false);
// System.out.println(f3.canRead());
}
拓展
// * 显示某个目录下的所有的文件,包括文件跟子目录
public static void showFiles(File dirFile){
File [] fileList = dirFile.listFiles();
for (int i=0; fileList!=null && i<fileList.length;i++) {
if(fileList[i].isFile()){
String fileName = fileList[i].getName();
if(fileName.endsWith(".mp4") || fileName.endsWith(".flv") ||fileName.endsWith(".mkv")||fileName.endsWith(".avi")||fileName.endsWith(".mov")){
System.out.println(fileList[i].getAbsolutePath());
}
}else{
showFiles(fileList[i]);
}
}
}
IO流
流的定义:流是指一连串流动的字符,是以先进先出方式发送信息的通道。
按处理数据单元划分:
字节流:
字节输入流:InputStream基类
字节输出流:OutputStream基类
字符流:
字符输入流:Reader基类
字符输出流:Writer基类
(字节流是 8 位通用字节流,字符流是16位Unicode字符流)
字节流
public static void main(String[] args) {
//定义字节输出流,输入流
InputStream inputStream = null ;
OutputStream outputStream = null ;
try {
//输入的照片
File file = new File("d://a/1.jpg");
//输出的照片
File targetFile = new File("d://a/2.jpg");
//判断是否存在
if(!targetFile.exists()){
targetFile.createNewFile();
}
inputStream = new FileInputStream(file) ;
outputStream = new FileOutputStream(targetFile) ;
byte [] b = new byte [1024] ;
int i = 0 ;
//读入缓冲区的总字节数;如果因为已经到达流末尾而不再有数据可用,则返回 -1。
while((i = inputStream.read(b))!= -1){
for(int j=0;j<i;j++){
outputStream.write(b[j]);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(inputStream != null){
inputStream.close();
}
if(outputStream != null){
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
字符流
输入字符流 Reader
public static void main(String[] args) {
FileReader fr = null ;
BufferedReader br = null ;
try {
File file = new File("d://a/a.txt");
fr = new FileReader(file);
br = new BufferedReader(fr);
String s = null ;
do{
s = br.readLine();
if(s!=null){
System.out.println(s);
}
}while(s!=null);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(br != null){
br.close();
}
if(fr != null){
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
输入字符流 Reader
public static void main(String[] args) {
File file = new File("d://a/a.txt");
FileWriter fw = null;
BufferedWriter bw = null ;
try {
fw = new FileWriter(file,true);// boolean:表示是覆盖还是追加
bw = new BufferedWriter(fw);
bw.write("hello seven ...........");
bw.newLine();
bw.write("明天星期3....");
bw.newLine();
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(bw != null){
bw.close();
}
if(fw != null){
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}