异常以及IO流的应用(2019年10月24日)

在做含小数精确运算的时候,如果用基本数据类型那么会出现不准确的情况

这个时候我们就需要使用BigDecimal这个类

 1 BigDecimal b1 = new BigDecimal(6.34) ; 2 BigDecimal b2 = new BigDecimal("3.14") ;// 推荐用这一种,因为上面的6.75的默认类型就是double在这个时候就已经有精度的缺失了 

1 System.out.println(b1.add(b2)); //加法
2 System.out.println(b1.subtract(b2));//减法
3 System.out.println(b1.multiply(b2)); //乘法
4 System.out.println(b1.divide(b2,4,BigDecimal.ROUND_HALF_UP)); //除法,第二个参数表示的是保留小数点后的位数,第三个参数表示的是舍入模式(BigDecimal.ROUND_HALF_UP 是我们经常使用的舍入模式)

异常:  Error (错误)  Exception(异常)

编译时异常:编译期间就需要捕获异常。也就是说必须有try catch捕获

运行时异常:NullPointerException   。可以根据需要有或者没有。

throws IOException:把异常继续往上面抛出。谁调用我谁来处理

 try {代码块1} :对代码块1进行代码监控
 catch(){代码块2}:当代码1出现了对应捕获的异常则执行此对应的代码块内容。
 finally{代码块3} :表示无论如何,是否出现异常,都会执行里面的代码内容。

有异常:代码块1 ---> 代码块2 ---->代码块3 (出现异常以后,异常后面的代码就不会执行了,代码流程改变了。)
没异常:代码块1 ---> 代码块3

 我们可以分别捕获多种异常,一旦发生那种异常就进入对应catch里面的代码块。
 捕获的异常的顺序:先子类到父类 先细后粗
可不可以没有finally块?可以
    可以不可以没有catch块?没有finally就一定要有catch.如果有finally语法上可以没有catch,我们还是要加上catch

  try中的代码块会被我们监控,如果有异常,就会进入代码块二中

IO流: input ouput 流 相对于java(内存) 包含字节流(input 和output) 和字符流(reader 和writer)

下面是几种常见的使用(file为一个File对象)

扫描二维码关注公众号,回复: 7583340 查看本文章
1          System.out.println("exists-->"+file.exists()); // 判断此文件是否存在
2          System.out.println("canRead-->"+file.canRead());// 判断文件是否可读
3          System.out.println("canWrite-->"+file.canWrite());// 判断文件是否可写
4          System.out.println("isHidden-->"+file.isHidden());// 判断文件是否隐藏
5          System.out.println("isDirectory-->"+file.isDirectory());// 判断文件是否是目录
6          System.out.println("getName-->"+file.getName());// 获取文件的名字
7          System.out.println("getName-->"+file.getPath());// 获取文件的路径
8          System.out.println("lastModified-->"+ new Date(file.lastModified()) );// 获取文件的最后修改时间
9          System.out.println("isFile-->"+file.isFile() );// 是否是文件

下面代码显示用递归算法查看文件:

 1     public static void showAllFiles(File file) {
 2         if (file == null) {
 3             return;
 4         }
 5 
 6         File[] files = file.listFiles();
 7         
 8         if(files == null){
 9             return ;
10         }
11         
12         for (File f : files) {
13             if (f.isDirectory()) {
14 //                    子目录
15                 showAllFiles(f);
16             } else {
17 //                   子文件
18                 if(f.getName().endsWith(".exe") || f.getName().endsWith(".tmp")){
19                     System.out.println(f.getPath() + f.getName());
20                 }
21                 
22             }
23         }
24 
25     }

下面代码演示用字节流将一个文件复制到另一个地方:

 1 import java.io.FileInputStream;
 2 import java.io.FileNotFoundException;
 3 import java.io.FileOutputStream;
 4 import java.io.IOException;
 5 
 6 /**
 7  * 复制一个文件,字节输入流和字节输出流
 8  * 
 9  * @author Administrator
10  *
11  */
12 public class FileTest02 {
13 
14     public static void main(String[] args) {
15         // 新建一个字节输出流和一个字节输入流
16         FileInputStream fis = null;
17         FileOutputStream fos = null;
18         try {
19             fis = new FileInputStream("D:\\a\\b\\集合.png");// 将这连个流给到路径("D:\\a\\b\\集合.png"为需要复制的文件路径)
20             fos = new FileOutputStream("D:\\a\\集合.png"); // 将这连个流给到路径("D:\\a\\集合.png"为复制文件之后存放的位置)
21             int a = 0;
22             byte[] b = new byte[1024]; // 给一个传递内容的容器
23             while ((a = fis.read(b)) > 0) { // a=fis.read(b),a每次传递中容器中的字节数,最后一次容器会是0,那么就会返回一个-1,那么就会停止循环
24                 fos.write(b); // 把容器b中的内容放到fos内,就是将复制的东西粘贴到那里面
25             }
26         } catch (FileNotFoundException e) {
27             e.printStackTrace();
28         } catch (IOException e) {
29             // TODO Auto-generated catch block
30             e.printStackTrace();
31         } finally {
32             // 把输入和输出流关闭,得先判断这个流是否为空,如果是空的话就会报空指针异常
33             try {
34                 if (fis != null) {
35                     fis.close();
36                 }
37                 if (fos != null) {
38                     fos.close();
39                 }
40             } catch (IOException e) {
41                 // TODO Auto-generated catch block
42                 e.printStackTrace();
43             }
44         }
45     }
46 }

下面展示用字符输出流给文件加内容:

 1 import java.io.BufferedWriter;
 2 import java.io.File;
 3 import java.io.FileWriter;
 4 import java.io.IOException;
 5 
 6 /**
 7  * 用字符输入流 以及包装类实现向一个txt文件输入数据
 8  * @author Administrator
 9  *
10  */
11 public class FileTest03 {
12 
13     public static void main(String[] args) {
14             File file=new File("D:/a/ab.txt");//目标文件
15             FileWriter rw=null;
16             BufferedWriter bw=null;
17             try {
18                 rw=new FileWriter(file,true);//默认是对文件的内容覆盖添加,也就是说默认是false(true表示的是追加 append)
19                 bw=new BufferedWriter(rw);
20                 bw.write("1111111111111");
21                 bw.newLine();//换行
22                 bw.write("1111111111111");
23                 bw.write("1111111111111"); //这个是后还没有把内容放到目标文件中,只是在缓冲区中(BufferedWriter中),还得通过flush方法传到文件中
24                 
25                 bw.flush();//把缓冲区的东西的的确确的放到文件中
26             } catch (IOException e) {
27                 // TODO Auto-generated catch block
28                 e.printStackTrace();
29             }finally{                //记得要把这两个流关掉,关掉的时候要记得向内部再外部
30                         
31                 try {
32                     if(bw!=null){        //为了防止空指针异常
33                         bw.close();
34                     }
35                     if(rw!=null){
36                         rw.close();
37                     }
38                 } catch (IOException e) {
39                     // TODO Auto-generated catch block
40                     e.printStackTrace();
41             }
42         }
43     }
44 }

下面代码展示用字符输入流将文件的内容打印到控制台:

 1 import java.io.BufferedReader;
 2 import java.io.File;
 3 import java.io.FileNotFoundException;
 4 import java.io.FileReader;
 5 import java.io.IOException;
 6 
 7 /**
 8  * 用字符输入流读取一个txt文件中的内容,打印到控制台上面
 9  * 
10  * @author Administrator
11  *
12  */
13 public class Tset04 {
14 
15     public static void main(String[] args) {
16         File file = new File("D:/a/ab.txt");// 写需要打印的文件的path,不能是文件夹,不然就会报:java.io.FileNotFoundException:
17                                             // D:\a (拒绝访问。)
18         // 初始化,方便等会关闭这个流
19         FileReader fr = null;
20         BufferedReader br = null;
21         String a = null;
22 
23         try {
24 
25             fr = new FileReader(file);
26             br = new BufferedReader(fr);
27             do {
28                 //
29                 // a=br.readLine(); //读一行 这样写会多打印一个null
30                 // System.out.println(a);
31 
32                 a = br.readLine(); // 我先取到那一行的内容,如果没有(a为null),那么我就break;否者打印
33                 if (a == null) {
34                     break;
35                 } else {
36                     System.out.println(a);
37                 }
38 
39             } while (a != null);
40 
41             // while(a!=null){
42             // a=br.readLine();
43             // System.out.println(a);
44             //
45             // }
46 
47         } catch (FileNotFoundException e) {
48             // TODO Auto-generated catch block
49             e.printStackTrace();
50         } catch (IOException e) {
51             // TODO Auto-generated catch block
52             e.printStackTrace();
53         } finally {
54             try {
55                 if (fr != null) {
56                     fr.close();
57                 }
58                 if (br != null) {
59                     br.close();
60                 }
61 
62             } catch (IOException e) {
63                 // TODO Auto-generated catch block
64                 e.printStackTrace();
65             }
66         }
67 
68     }
69 
70 }

猜你喜欢

转载自www.cnblogs.com/Starlets/p/11734631.html