1 package day8.lesson3;
2
3 import java.io.FileOutputStream;
4 import java.io.IOException;
5
6 /*
7 3.5 字节流写数据加异常处理
8
9 try-catch-finally:
10 try{
11 可能出现异常的代码;
12 }catch(异常类名 变量名){
13 异常的处理代码;
14 }finally{
15 执行所有清除操作;
16 }
17
18 finally特点:
19 被finally控制的语句一定会执行,除非JVM退出
20 */
21 public class FileOutputStreamDemo04 {
22 public static void main(String[] args) {
23 /*try {
24 FileOutputStream fos = new FileOutputStream("stage2\\src\\day8\\lesson3\\fos4.txt");
25 fos.write("hello".getBytes());
26 fos.close();
27 }catch (IOException e){
28 e.printStackTrace();
29 }*/
30
31 FileOutputStream fos = null;
32 try {
33 fos = new FileOutputStream("stage2\\src\\day8\\lesson3\\fos4.txt");
34 fos.write("hello".getBytes());
35 }catch (IOException e){
36 e.printStackTrace();
37 }finally {
38 if(fos != null){
39 try {
40 fos.close();
41 } catch (IOException e) {
42 e.printStackTrace();
43 }
44 }
45 }
46
47 }
48 }