字节流写数据(OutputStream)

利用构造方法也可以写入文件

FileOutputStream(String name)

底层:

public FileOutputStream(String name) throws FileNotFoundException {
this(name != null ? new File(name) : null, false);
}

相当于new file(name)

 

OutputStream的基本方法

//向输出流中写入一个字节数据该字节数据为参数b的低8位

void write(int b) throws IOException

 public byte[] getBytes​()

使用平台的默认字符集将该String编码为一系列字节,将结果存储到新的字节数组中。

当该字符串不能在默认字符集中编码时,此方法的行为是未指定的。 当需要对编码过程进行更多控制时,应使用CharsetEncoder类。

//将一个字节类型的数组中的数据写入输出流

void write (byte 【】 b) throws IOException   例如 file.write(hello.getBytes());

//将一个字节类型的数组中的数据从指定位置(off)开始的len个字节写入到输出流

void write(byte【】 b,int off,int len) throws IOException

//关闭流释放内存资源

void close() throws IOException

将输出流中缓冲的数据全部写到目的地

void flush() throws IOException

注意:先写flush再写close

 ---------------------------------------------------------------------------------------------------------------------------------------------------|------|--------|----------------------------------------------

 

 字节流写数据加异常处理

 

posted @ 2019-08-17 18:15  水果、、  阅读(1676)  评论(0编辑  收藏  举报