I/O流


Java I/O流
================================================
流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。
即数据在两设备间的传输称为流,流的本质是数据传输,
根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。

一、IO流的分类:
a.根据处理数据类型的不同分为:字符流和字节流
b.根据数据流向不同分为:输入流和输出流

【以内存为主】

1.字符流和字节流

字符流的由来:因为数据编码的不同,而有了对字符进行高效操作的流对象。
本质其实就是基于字节流读取时,去查了指定的码表。
字节流和字符流的区别:
(1)读写单位不同:字节流以字节(8bit)为单位,字符流以字符为单位,根据码表映射字符,一次可能读多个字节。
(2)处理对象不同:字节流能处理所有类型的数据(如图片、avi等),而字符流只能处理字符类型的数据。
(3)字节流在操作的时候本身是不会用到缓冲区的,是文件本身的直接操作的;
     而字符流在操作的时候是会用到缓冲区的,是通过缓冲区来操作文件,我们将在下面验证这一点。

结论:优先选用字节流。首先因为硬盘上的所有文件都是以字节的形式进行传输或者保存的,包括图片等内容,
      字符只是在内存中才会形成的,所以在开发中,字节流使用广泛。

2.输入流和输出流
对输入流只能进行读操作,对输出流只能进行写操作,程序中需要根据待传输数据的不同特性而使用不同的流。

二、Java流操作有关的类或接口:
Java流类图结构:

===========================================================
1.输入字节流InputStream
InputStream 是所有的输入字节流的父类,它是一个抽象类。
ByteArrayInputStream、StringBufferInputStream、FileInputStream 是三种基本的介质流,
它们分别从Byte数组、StringBuffer、和本地文件中读取数据。
ObjectInputStream 和所有FilterInputStream的子类都是装饰流(装饰器模式的主角)。
意思是FileInputStream类可以通过一个String路径名创建一个对象,FileInputStream(String name)。
而DataInputStream必须装饰一个类才能返回一个对象
【案例】字节流读取文件
/**
 * 字节流
 *读文件
 * */
import java.io.*;
public class FileInputStreamDemo{
   public static void main(String[] args) throws IOException {
       String fileName = "D:"+File.separator+"hello.txt";
       File f = new File(fileName);
       InputStream in = new FileInputStream(f);
       byte[] b = new byte[(int)f.length()];
       int count = 0;
       int temp = 0;
       while((temp=in.read()) != (-1)){
           b[count++] = (byte)temp;
       }
       in.close();
       System.out.println(new String(b, "GBK"));//UTF-8
    }
}
注意:当读到文件末尾的时候会返回-1.正常情况下是不会返回-1的。

【案例】DataInputStream类
public class DataOutputStreamDemo{
   public static void main(String[] args) throws IOException{
       File file = new File("d:" + File.separator +"hello.txt");
       DataInputStream input = new DataInputStream(new FileInputStream(file));
       char[] ch = new char[(int)f.length()];
       int count = 0;
       char temp;
       while((temp = input.readChar()) != 'C'){
           ch[count++] = temp;
       }
       System.out.println(ch);
    }
}
2. 输出字节流OutputStream
OutputStream 是所有的输出字节流的父类,它是一个抽象类。
ByteArrayOutputStream、FileOutputStream是两种基本的介质流,
它们分别向Byte数组、和本地文件中写入数据。
【案例】向文件中写入字符串
/**
 * 字节流
 * 向文件中写入字符串
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName="D:"+File.separator+"hello.txt";
       File f=new File(fileName);
       OutputStream out =new FileOutputStream(f);
       //OutputStream out =new FileOutputStream(f,true);//true表示追加模式,否则为覆盖
       String str="Hello World";
       //String str="\r\nRollen"; 可以换行
       byte[] b=str.getBytes();
       out.write(b);
       out.close();
    }
}

3.字符输入流Reader/输出流Writer
定义和说明:
Reader是所有的输入字符流的父类,它是一个抽象类。
CharReader、StringReader是两种基本的介质流,它们分别将Char数组、String中读取数据。PipedReader是从与其它线程共用的管道中读取数据。
BufferedReader很明显就是一个装饰器,它和其子类负责装饰其它Reader 对象。
FilterReader是所有自定义具体装饰流的父类,其子类PushbackReader 对Reader 对象进行装饰,会增加一个行号。
InputStreamReader是一个连接字节流和字符流的桥梁,它将字节流转变为字符流。
FileReader可以说是一个达到此功能、常用的工具类,在其源代码中明显使用了将FileInputStream 转变为Reader的方法。
我们可以从这个类中得到一定的技巧。Reader 中各个类的用途和使用方法基本和InputStream中的类使用一致。后面会有Reader 与InputStream 的对应关系。
/**
 * 字符流
 * 从文件中读出内容
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName = "D:"+File.separator+"hello.txt";
       File f = new File(fileName);
       char[] ch=new char[100];
       Reader read=new FileReader(f);
       int temp = 0;
       int count = 0;
       while((temp = read.read())!=(-1)){
           ch[count++]=(char)temp;
       }
       read.close();
       System.out.println("内容为"+new String(ch,0,count));//有中文会乱码
    }
}
【案例】向文件中写入数据
/**
 * 字符流
 * 写入数据
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName="D:"+File.separator+"hello.txt";
       File f = new File(fileName);
       Writer out = new FileWriter(f);
       String str = "hello";
       out.write(str);
       out.close();
    }
}
字符流与字节流转换
InputStreamReader:字节到字符的桥梁
OutputStreamWriter:字符到字节的桥梁
这两个流对象是字符体系中的成员,它们有转换作用,本身又是字符流,所以在构造的时候需要传入字节流对象进来。
/**
* 读取文件
* 字符流与字节流转换
* 装饰器模式
*/
public static void test() throws IOException{
        String fileName = "F:" + File.separator + "hello.txt";
        File file = new File(fileName);
        Reader read = new InputStreamReader(new FileInputStream(file), "GBK");//UTF-8
        BufferedReader reader = new BufferedReader(read);
        String str = "";
        String tempString = null;
        while((tempString = reader.readLine()) != null){
            str += tempString;
        }
        reader.close();
    System.out.println(str);

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值