Java基础知识强化之IO流笔记19:FileOutputStream的三个write方法

时间:2023-03-08 17:42:02

1. FileOutputStream的三个write方法:

 void write(byte[] buffer)
          Writes the entire contents of the byte array buffer
to this stream.
 void write(byte[] buffer,
int offset, int count)

          Writes count bytes from
the byte array buffer starting at offset to this
stream.
 void write(int oneByte)

          Writes the specified byte oneByte to this stream.

2. 代码示例:

 package com.himi.fileoutputstream;

 import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; /**
*
*字节输出流操作步骤:
* 第一步:创建字节输出流对象
* 第二步:写数据
* 第三步:关闭字节输出流对象
*
* void write(byte[] buffer)
* Writes the entire contents of the byte array buffer to this stream.
*
* void write(byte[] buffer, int offset, int count)
* Writes count bytes from the byte array buffer starting at offset to this stream.
*
* void write(int oneByte)
* Writes the specified byte oneByte to this stream.
*
*
*/ public class FileOutputStreamDemo2 { public static void main(String[] args) throws IOException{
//创建字节输出流对象
FileOutputStream fos = new FileOutputStream("fos2.txt"); //写数据
//write(int oneByte) 97---底层二进制数据--通过记事本打开--找到97对应的字符值--a for (int i = 97; i < 110; i++) {
fos.write(i);
} //写数据
//write(byte[] buffer) 写一个字节数组 ,对应也是字符
byte[] bytes = {111,112,113,114,115};
fos.write(bytes); //写数据
//write(byte[] buffer, int offset, int count) 写一个字节数组的一部分
fos.write(bytes, 1, 3); }

}