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. 代码示例:
1 package com.himi.fileoutputstream; 2 3 import java.io.FileNotFoundException; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 7 /** 8 * 9 *字节输出流操作步骤: 10 * 第一步:创建字节输出流对象 11 * 第二步:写数据 12 * 第三步:关闭字节输出流对象 13 * 14 * void write(byte[] buffer) 15 * Writes the entire contents of the byte array buffer to this stream. 16 * 17 * void write(byte[] buffer, int offset, int count) 18 * Writes count bytes from the byte array buffer starting at offset to this stream. 19 * 20 * void write(int oneByte) 21 * Writes the specified byte oneByte to this stream. 22 * 23 * 24 */ 25 26 27 public class FileOutputStreamDemo2 { 28 29 public static void main(String[] args) throws IOException{ 30 //创建字节输出流对象 31 FileOutputStream fos = new FileOutputStream("fos2.txt"); 32 33 //写数据 34 //write(int oneByte) 97---底层二进制数据--通过记事本打开--找到97对应的字符值--a 35 36 for (int i = 97; i < 110; i++) { 37 fos.write(i); 38 } 39 40 41 //写数据 42 //write(byte[] buffer) 写一个字节数组 ,对应也是字符 43 byte[] bytes = {111,112,113,114,115}; 44 fos.write(bytes); 45 46 //写数据 47 //write(byte[] buffer, int offset, int count) 写一个字节数组的一部分 48 fos.write(bytes, 1, 3); 49 50 51 52 } 53 54 }