首页 > 其他分享 >IO流-字节流2

IO流-字节流2

时间:2023-03-11 10:11:05浏览次数:36  
标签:字节 fos finally IOException FileOutputStream IO

IO流-字节流2

package IO.ByteStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class ByteStreamException {
    public static void main(String[] args) {
        FileOutputStream fos = null;    //把变量初始化,否则空指针异常
        try {
            fos = new FileOutputStream("fos.txt");
            fos.write("hello,world".getBytes());

        }catch (IOException i ){
            i.printStackTrace();
        }finally{     if (fos!=null) {   //加入finally来释放资源, if语句增强健壮性,包括整个finally语句块
            try {           //Alt+Enter自动处理异常
                fos.close();    //释放资源的方法
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
            //Cannot resolve symbol 'fos' : 无法解析的符号, 就是没有这个变量
            //Variable 'fos' might not have been initialized : 变量可能没被初始化
        }
    }
}

标签:字节,fos,finally,IOException,FileOutputStream,IO
From: https://www.cnblogs.com/lg369/p/17205234.html

相关文章