zoukankan      html  css  js  c++  java
  • 字节流

    1、

    参考网址:“http://www.cnblogs.com/liuling/archive/2013/05/07/InputStreamAndOutputStream.html

    ①字节数组输入流:

     1 package com.iotest;
     2 
     3 import java.io.ByteArrayInputStream;
     4 import java.io.IOException;
     5 public class ByteArryInputStreamDemo {
     6     public static void main(String[] args) throws IOException {
     7         String str = "abcdefghijk";
     8         byte[] strBuf = str.getBytes();  //字符串转换成字节数组
     9         ByteArrayInputStream bais = new ByteArrayInputStream(strBuf);
    10         int data = bais.read();          //从字节数组输入流读取字节
    11         while(data!=-1){
    12             char upper = Character.toUpperCase((char)data);
    13             System.out.print(upper+" ");
    14             data = bais.read();
    15         }
    16         bais.close();
    17     }
    18 }

    程序运行结果:A B C D E F G H I J K

    ②字节数组输出流:

     1 package com.iotest;
     2 
     3 import java.io.ByteArrayOutputStream;
     4 import java.io.IOException;
     5 
     6 public class ByteArrayOutputStreamDemo {
     7     public static void main(String[] args) throws IOException {
     8         ByteArrayOutputStream baos = new ByteArrayOutputStream();
     9         String s = "welcome to use ByteArrayOutputStreamDemo";
    10         byte[] buf = s.getBytes();  
    11         baos.write(buf); //将指定的byte数组写到字节数组输出流中
    12         System.out.println(baos.toString());  //将字节数组输出流内容转换成字符串输出
    13         //将字节数组输出流中的内容复制到字节数组中
    14         byte[] b = baos.toByteArray();
    15         for (int i = 0; i < b.length; i++) {
    16             System.out.print((char)b[i]);
    17         }
    18         baos.close();
    19     }
    20 }

    程序运行结果:
    welcome to use ByteArrayOutputStreamDemo
    welcome to use ByteArrayOutputStreamDemo

    ④管道流的使用:
      一个PipedInputStream对象必须和一个PipedOutputStream对象进行连接从而产生一个通信管道。通常一个线程从管道输出流写入数据,另一个线程从管道输入流中读取数据。当线程A执行管道输入流的read()方法时,如果暂时没有数据,这个线程就会被阻塞,只有当线程B想管道输出流写了数据后,线程A才会恢复运行。

    ⑤缓冲流的使用:

    如果不用缓冲流的话,程序是读一个数据,写一个数据。这样在数据量大的程序中非常影响效率。 缓冲流作用是把数据先写入缓冲区,等缓冲区满了,再把数据写到文件里。这样效率就大大提高了。

    2、

    参考网址:“http://blog.csdn.net/worker90/article/details/6869524

    1)        字节输出流:OutputStream

    OutputStream是一个抽象类,要想使用它,必须通过子类来实例化它。

    OutputStream类的常用方法

    方法名称

    描述

    public void close() throws IOException

    关闭输出流

    public void flush() throws IOException

    刷新缓冲区

    public void write(byte[] b) throws IOException

    将一个byte数组写入数据流

    public void write(byte[] b,int off,int len) throws IOException

    将一个指定范围的byte数组写入数据流

    public abstract void write(int b) throws IOException

    将一个字节数据写入数据流

    2)        字节输入流:InputStream

    InputStream类的常用方法

    方法名称

    描述

    public void avaliable() throws IOException

    可以取得输入文件的大小

    public void close() throws IOException

    关闭输入流

    public abstract int read() throws IOException

    读取内容,以数字的方式读取

    public int read (byte b) throws IOException

    将内容读到byte数组,同时返回读入的个数

    与OutputStream类一样,InputStream本身也是一个抽象类,要想使用它,也必须依靠其子类。

     

    C

  • 相关阅读:
    string类的方法
    引用
    面向对象之多态性(基类引用可以指向子类)
    SQL Server、Oracle数据库排序空值null问题解决办法(转)
    远程计算机已结束连接解决方法
    GUID自增ID表关联比较
    第四种行转列
    SQL简体转繁体互换(常用汉字词组)
    CSS基本布局16例
    Dojo Javascript 编程规范
  • 原文地址:https://www.cnblogs.com/codeskilla/p/4964444.html
Copyright © 2011-2022 走看看