Java 字节流,
分享于 点击 27762 次 点评:133
Java 字节流,
package io.bytestream.demo;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class ByteStreamDemo {
public static void main(String[] args) throws IOException {
demo_read();
}
private static void demo_read() throws IOException {
//1 创建一个读取流对象 和指定的文件关联
FileInputStream fis=new FileInputStream("bytedemo.txt");
//System.out.println(fis.available());返回字节数
/* byte [] buf=new byte[fis.available()];不能读取太大的
fis.read(buf);
System.out.println(new String(buf));
*/
//建议使用这种读取数据的方式
/* byte[] buf=new byte[1024];
int len=0;
while((len=fis.read(buf))!=-1) {
System.out.println(new String(buf,0,len));
}*/
/* while((len=fis.read())!=-1) {
System.out.println((char)ch);
}*/
//一次读取一个字节
/* int ch=fis.read();
System.out.println(ch);
fis.close();*/
}
private static void demo_write() throws IOException {
//1.创建字节输出流对象,用于操作文件
FileOutputStream fos=new FileOutputStream("bytedemo.txt");
//2 写数据
fos.write("abcdefg".getBytes());
//fos.flush(); 不需要 都没定义 缓冲区对象才需要
fos.close();//关闭资源需要用
}
}
相关文章
- 暂无相关文章
用户点评