Java 字节流

来源:互联网 发布:张大奕的淘宝店劣质 编辑:程序博客网 时间:2024/06/05 22:52
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();//关闭资源需要用}}

原创粉丝点击