|
| 1 | +下文中所出现的代码可能不规范,譬如文件检查,读者自行体会用法就好。 |
| 2 | +### 0.什么是字节流 |
| 3 | +简单来说就是面向字节的IO流,也就是提供的read或者write方法,是对byte或者byte[] 操作的。在Java中,有这么几类。 |
| 4 | + |
| 5 | +**InputStream** |
| 6 | + |
| 7 | +| 类 | 功能 | 构造器的参数 | |
| 8 | +| ------------- |:-------------:| -----:| |
| 9 | +| ByteArrayInputStream | 将内存中的缓冲区当做InputStream使用 | byte[] | |
| 10 | +| StringBufferInputStream | 将String转化为InputStream | String | |
| 11 | +|FIleInputStream |从文件中读取信息 | File,FileDescriptor(文件描述符),String(文件)| |
| 12 | +|PipedInputStream |读取PipedOutputStream输入的数据,实现“管道化” | PipedOutputStream,需要和PipedOutputStream建立链接 | |
| 13 | +| SequenceInputStream| 将两个或多个流合并成一个(按顺序)|1.两个流 2.充满流的容器 | |
| 14 | +|FilterInputStream |包装其他流,提供更多有用的功能 | | |
| 15 | + |
| 16 | +**OutputStream** |
| 17 | +|类名 |功能 |参数 | |
| 18 | +|-|:-:|-:| |
| 19 | +|ByteOutputStream |将输入写入到byte[] |大小size | |
| 20 | +|FileOutputStream|输入到文件 | File,FileDescriptor(文件描述符),String(文件)| |
| 21 | +| PipedOutputStream| 输入数据,当做PipedInputStream的输入数据,实现“管道化” |PipedInputStream,需要和PipedInputStream建立链接关系 | |
| 22 | +|FilterOutputStream | 包装其他流,提供更多有用的功能| | |
| 23 | + |
| 24 | +### 1.通用 |
| 25 | +通过read和write的方法来进行数据的读写。 |
| 26 | +### 2.ByteArray相关 |
| 27 | +举个例子,现在将一个byte[]数组的内容读出来并输入到控制台。 |
| 28 | +``` |
| 29 | + String s = new String("这是测试数据"); |
| 30 | + byte[] in = new byte[0]; |
| 31 | + try { |
| 32 | + in = s.getBytes("utf-8"); |
| 33 | + } catch (UnsupportedEncodingException e) { |
| 34 | + e.printStackTrace(); |
| 35 | + } |
| 36 | + ByteArrayInputStream bais = new ByteArrayInputStream(in); |
| 37 | + byte[] bytes1 = new byte[1024]; |
| 38 | + try { |
| 39 | + StringBuffer stringBuffer = new StringBuffer(); |
| 40 | + while (bais.read(bytes1) != -1){ |
| 41 | + stringBuffer.append(new String(bytes1).trim()); |
| 42 | + System.err.println(stringBuffer); |
| 43 | + } |
| 44 | + bais.close(); |
| 45 | + } catch (IOException e) { |
| 46 | + e.printStackTrace(); |
| 47 | + } |
| 48 | +``` |
| 49 | +看下控制台的输出。 |
| 50 | + |
| 51 | + |
| 52 | +由于StringBufferInputStream和ByteArrayInputStream的用法一致,就不在多说。 |
| 53 | +### 2.File相关 |
| 54 | +举个例子,从一个文件中读取内容,写入到另一个文件中。代码如下。 |
| 55 | +``` |
| 56 | + File file = new File("./1.txt"); |
| 57 | + File file1 = new File("./2.txt"); |
| 58 | + if (!file1.exists()){ |
| 59 | + try { |
| 60 | + file1.createNewFile(); |
| 61 | + } catch (IOException e) { |
| 62 | + e.printStackTrace(); |
| 63 | + } |
| 64 | + } |
| 65 | + try { |
| 66 | + FileInputStream fis = new FileInputStream(file); |
| 67 | + StringBuffer sb = new StringBuffer(); |
| 68 | + FileOutputStream fos = new FileOutputStream(file1); |
| 69 | + byte[] b = new byte[1024]; |
| 70 | + while ( fis.read(b) != -1){ |
| 71 | + sb.append(new String(b).trim()); |
| 72 | + } |
| 73 | + fos.write(b); |
| 74 | + fos.flush(); |
| 75 | + fos.close(); |
| 76 | + System.err.println(sb); |
| 77 | + fis.close(); |
| 78 | + } catch (FileNotFoundException e) { |
| 79 | + e.printStackTrace(); |
| 80 | + } catch (IOException e) { |
| 81 | + e.printStackTrace(); |
| 82 | + } finally { |
| 83 | +
|
| 84 | + } |
| 85 | +``` |
| 86 | +### 3.Piped相关 |
| 87 | +在此之前,我们需要了解一下管道。[点我了解管道](http://blog.chinaunix.net/uid-27034868-id-3394243.html) |
| 88 | +接下来,我们以两个线程为例,来展示下他的用法。 |
| 89 | +``` |
| 90 | + static class ReadThread implements Runnable{ |
| 91 | +
|
| 92 | + private PipedInputStream pis ; |
| 93 | +
|
| 94 | + public ReadThread(PipedInputStream pis){ |
| 95 | + this.pis = pis ; |
| 96 | + } |
| 97 | +
|
| 98 | + @Override |
| 99 | + public void run() { |
| 100 | + byte[] b = new byte[1024]; |
| 101 | + try { |
| 102 | + if (null != pis){ |
| 103 | + System.err.println(getDate() + " 等待另一头输入数据"); |
| 104 | + while (pis.read(b) != -1){ |
| 105 | + System.err.println(getDate()+ " 读取成功,数据为:"+new String(b).trim()); |
| 106 | + } |
| 107 | + pis.close(); |
| 108 | + }else { |
| 109 | + System.err.println("pis is null"); |
| 110 | + } |
| 111 | + } catch (IOException e) { |
| 112 | + e.printStackTrace(); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +``` |
| 117 | +读线程一直等待输入线程像管道中写入数据。若没有数据,就一致处在阻塞状态。 |
| 118 | +接下来我们看写入线程。 |
| 119 | +``` |
| 120 | + static class WriteThread implements Runnable{ |
| 121 | +
|
| 122 | + private PipedOutputStream pos; |
| 123 | +
|
| 124 | + public WriteThread(PipedOutputStream pos){ |
| 125 | + this.pos = pos ; |
| 126 | + } |
| 127 | +
|
| 128 | + @Override |
| 129 | + public void run() { |
| 130 | + byte[] b = new byte[1024]; |
| 131 | + try { |
| 132 | + if (null != pos){ |
| 133 | + Thread.sleep(5000); |
| 134 | + pos.write("这是输入的数据".getBytes("utf-8")); |
| 135 | + System.err.println(getDate()+" 输入数据成功"); |
| 136 | + pos.close(); |
| 137 | + } |
| 138 | + }catch (IOException e){ |
| 139 | + e.printStackTrace(); |
| 140 | + } catch (InterruptedException e) { |
| 141 | + e.printStackTrace(); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | +``` |
| 146 | +接下来我们就看下控制台的输出。稍后我会将这个类push到https://github.com/Guolei1130/ATips/tree/master/Java 下面。 |
| 147 | + |
| 148 | + |
| 149 | +### 4.SequenceInputStream |
| 150 | +``` |
| 151 | +FileInputStream fis_1 = new FileInputStream(new File("./1.txt")); |
| 152 | + FileInputStream fis_2 = new FileInputStream(new File("./2.txt")); |
| 153 | +
|
| 154 | + FileOutputStream fos = new FileOutputStream(new File("./3.txt")); |
| 155 | +
|
| 156 | + //将两个或者多个合并. |
| 157 | + SequenceInputStream sis = new SequenceInputStream(fis_1,fis_2); |
| 158 | +
|
| 159 | + byte[] b = new byte[1024]; |
| 160 | + while (sis.read(b) != -1){ |
| 161 | + fos.write(b); |
| 162 | + } |
| 163 | + sis.close(); |
| 164 | + fos.close(); |
| 165 | + fis_1.close(); |
| 166 | + fis_2.close(); |
| 167 | +``` |
| 168 | +将两个流合并成一个之后,(这里是有顺序的),输出的流就会按照输入流的顺序输出数据。 |
| 169 | +### 5.Filter相关 |
| 170 | +这个很重要,因为我们在平常使用过程中往往需要结合这些来用,一般的流只提供了read,write方法,而装饰之后的流则有更多的方法,如DataInputStrem,提供了readInt等,比较方便。还有就是BufferedInputStream使用缓存区,来防止每次都进行实际操作。 |
| 171 | +**FilterInputStream** |
| 172 | +| 类|功能 | 构造器参数| 比普通流增加了那些功能| |
| 173 | +|--|:--:|:-:|--:| |
| 174 | +|DataInputStream|读取基本数据类型 |InputStream|增加基础数据类型的读写 | |
| 175 | +|BufferedInputStream|使用缓存区|- |增加缓存区| |
| 176 | +|LineNumberInputStream |跟踪输入流中的行号 |- | 增加行号| |
| 177 | +|PushedbackInputStream |能弹出字节的缓冲区 |- | | |
| 178 | + |
| 179 | +**FilterOutputStream** |
| 180 | +| 类|功能 | 构造器参数| 比普通流增加了那些功能| |
| 181 | +|--|:--:|:-:|--:| |
| 182 | +|DataOutputStream|写入基本数据类型 |OutputStream|增加基础数据类型的读写 | |
| 183 | +|PrintStream |格式化参数 | -| | |
| 184 | +|BufferedOutputStream|使用缓存区|- |增加缓存区| |
| 185 | + |
| 186 | +### 6.总结 |
| 187 | +这一块虽然简单点,但是还是很重要的。 |
| 188 | + |
0 commit comments