字节流的简单读写 FileOutputStream,FileInputStream

来源:互联网 发布:php的集成开发环境 编辑:程序博客网 时间:2024/06/04 18:38

字节流:

读写:

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

字节流缓冲区有刷新方法。



0 0
原创粉丝点击