字节流:
读写:
package com.bytedemo.test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class ByteStreamDemo {
public static void main(String[] args) throws IOException {
demo_write();
demo_read();
}
//读取
public static void demo_read() throws IOException {
FileInputStream fis = new FileInputStream("output.txt");
// 第三种方式available方法得到文件的大小,文件较小时适用 较大时一般不用
byte[] by2 = new byte[fis.available()];
fis.read(by2);
System.out.println(new String(by2));
/*
* 第二种方式 byte[] by = new byte[1024]; int len = 0; while((len =
* fis.read(by))!=-1){ System.out.println(new String(by,0,len)); }
*/
/*
* 第一种方式 int len = 0; while((len = fis.read())!=-1){
* System.out.println((char)len); }
*/
fis.close();
}
public static void demo_write() throws IOException {
FileOutputStream fos = new FileOutputStream("output.txt");
//write方法写入字节数组
fos.write("abcdefg".getBytes());
// 不需要刷新,直接写入。close里面没有刷新方法,关闭之间不会刷新
fos.close();
}
}
BufferInputStream, BufferOutputStream
字节流缓冲区有刷新方法。