java 基础之IO流 字节流与转换流 --07

来源:互联网 发布:小米手机无法数据上网 编辑:程序博客网 时间:2024/06/06 13:16


17.5 字节流

1.概述

  基本操作与字符流类相同
  它不仅可以操作字符,还可以操作其他媒体文件 如音乐 电影 图片 等等

2.常用类

  基类 : InputStream OutputStream
  常用子类:
FileInputStream ObjectInputStream BufferedInputStream, ByteArrayInputStream, DataInputStream
BufferedOutputStream, ByteArrayOutputStream, DataOutputStream ObjectOutputStream  FileOutputStream
3.示例  略


17.6 字节缓冲流

同字符流一样 同样是提高了字节流的读取速度
示例 :可以复制mp3 文本文件 等等 
package cn;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;/** * 使用带缓冲功能的字节流复制文件。 * @author 李昂志 */public class Test5 {public static void copy(String fileName,String toWhere) throws IOException{System.out.println("开始copy文件 文件路径:"+fileName +",复制路径:"+toWhere);BufferedInputStream br = null;BufferedOutputStream out = null;try {//BufferedReader 缓冲读取流br = new BufferedInputStream(new FileInputStream(fileName));//BufferedWriter 缓冲写入流out = new BufferedOutputStream(new FileOutputStream(new File(toWhere)));byte[] b = new byte[1024];int i = -1;while( ( i = br.read(b)) != -1){out.write(b, 0, i);}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally{//关闭流if(br!=null){br.close();}if(out!=null){out.close();}}System.out.println("copy完毕");}public static void main(String[] args) {try {Test5.copy("abc.txt", "CopyFile/abc.txt");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

自定义的字节缓冲流
package test2;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;/** * 自定义缓存字节流 * 难点 : 如果byte数据中有数据为-1 11111111 会上升成4个字节的int类型 换成二进制 会在前面补1 * 11111111 11111111 11111111 11111111  = -1 * 为避免这种情况 则应该在该数据前面补0 变成该int 类型的数据 * 00000000 00000000 00000000 11111111  * @author 李昂志 * */public class MyBufferInput extends InputStream {private InputStream in ;private int count=0,point=0;private byte[] buf ;public MyBufferInput(InputStream in ){this.in = in;buf = new byte[1024] ;}@Overridepublic int read() throws IOException {return in.read();}/** * 所返回的int数是 0 - 255 * 如果不是则返回-1 * @return * @throws IOException */public int myread() throws IOException{if(count == 0 ){count = in.read(buf); count --;return buf[point++]&0xff;  //前面补0}else if(count >0){count -- ;return buf[point++]&0xff;//前面补0}return -1;}public void close() throws IOException {if(in != null){in.close();}}


结论:
字节流的读一个字节的read方法为什么返回值类型不是byte,而是int。
因为有可能会读到连续8个二进制1的情况,8个二进制1对应的十进制是-1.
那么就会数据还没有读完,就结束的情况。因为我们判断读取结束是通过结尾标记-1来确定的。
所以,为了避免这种情况将读到的字节进行int类型的提升。
并在保留原字节数据的情况前面了补了24个0,变成了int类型的数值。


而在写入数据时,只写该int类型数据的最低8位。

17.7 转换流

1.概念

OutputStreamWriter:是Writer的子类,将输出的字符流变为字节流,即将一个字符流的输出对象变为字节流输出对象。

InputStreamReader:是Reader的子类,将输入的字节流变为字符流,即将一个字节流的输入对象变为字符流的输入对象。

如果以文件操作为例,则内存中的字符数据需要通过OutputStreamWriter变为字节流才能保存在文件中,
读取时需要将读入的字节流通过InputStreamReader变为字符流。过程如下:
写入数据-->内存中的字符数据-->字符流-->OutputStreamWriter-->字节流-->网络传输(或文件保存)
读取数据<--内存中的字符数据<--字符流<--InputStreamReader<--字节流<--网络传输(或文件保存)
可以清楚地发现,不管如何操作,最终全部是以字节的形式保存在文件中或者进行网络传输。

2.简单转换流示例

public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));String s ;while(true){s = br.readLine();if(s.equals("break")) break;System.out.println(s);}br.close();}


17.8 标准输入输出流

1.概念

System.in   输入流  是InputStream类 一般要通过转换流转换成字符流输入输出
System.out  输出流  是PrintStream类 打印流

2.常用方式

通过System类的setIn,setOut方法对默认设备进行改变。
System.setIn(new FileInputStream(“1.txt”));//将源改成文件1.txt。
System.setOut(new FileOutputStream(“2.txt”));//将目的改成文件2.txt

BfferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));

3.示例1 将输入的文字存入到一个文件中

可以用打印流 PrintWriter;
public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));//new FileWriter("abc.txt",true) 输入的东西添加到文件末尾,//new PrintWriter(new FileWriter("abc.txt",true),true) 自动刷新缓冲PrintWriter pw = new PrintWriter(new FileWriter("abc.txt",true),true);String s ;while(true){s = br.readLine();if(s.equals("break")) break;pw.println(s);//pw.flush();}br.close();pw.close();}


17.9 IO流小结

1.流是用来处理数据的。
2.处理数据时,一定要先明确数据源,与数据目的地(数据汇)。
3.数据源可以是文件,可以是键盘。
4.数据目的地可以是文件、显示器或者其他设备。
5.而流只是在帮助数据进行传输,并对传输的数据进行处理,比如过滤处理.转换处理等。


0 0
原创粉丝点击