Java的字节流

输入流输出流
字节流字节输入流
InputStream
字节输出流
OutputStream
字符流字符输入流
Reader
字符输出流
Writer

1. 字节输出流OutputStream 

hrows Exception {
        //会自动帮忙创建文件,但是文件夹不能建
        OutputStream os = new FileOutputStream("E:/AAA/b/f.txt");
        String str = "abcd";
        //把字符串转换为字节数组
        byte[] bytes = str.getBytes();
        os.write(bytes);
        os.close();//自带flush
    }

数据追加续写

public class Demo04OutputStream {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("c.txt", true);
        /*追加写10个"你好",并进行换行处理*/
        for (int i = 0; i < 10; i++) {
            fos.write("\n".getBytes());
            fos.write("你好".getBytes());
        }
    }
}

2.字节输入流InputStream

  //字节输入流   (一次一次的读取)
    @Test
    public void inputstream() throws Exception {
        InputStream is = new FileInputStream("E:/AAA/b/f.txt");
        byte[] bytes = new byte[3];
        int c = is.read(bytes);
        c = is.read(bytes);
        System.out.println(c);
    }
//一次性读取大文件
    @Test
    public void demoinp() throws Exception{
        InputStream is = new FileInputStream("E:/AAA/b/f.txt");
        byte[] bytes = new byte[3];
        int c= 0;
        while ((c=is.read(bytes))!=-1){
            String str = new String(bytes);
            System.out.println(str);
        }
        is.close();
    }

close方法,当完成流的操作时,必须调用此方法,释放系统资源。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值