对IO流的理解

来源:互联网 发布:手机淘宝店铺实名认证 编辑:程序博客网 时间:2024/06/05 01:17

(1)IO用于在设备间进行数据传输的操作
(2)分类:
A:流向
输入流 读取数据
输出流 写出数据
B:数据类型
字节流
字节输入流
字节输出流
字符流
字符输入流
字符输出流
注意:
a:如果我们没有明确说明按照什么分,默认按照数据类型分。
b:除非文件用windows自带的记事本打开我们能够读懂,才采用字符流,否则建议使用字节流。
(3)FileOutputStream写出数据
A:操作步骤
a:创建字节输出流对象
b:调用write()方法
c:释放资源

    B:代码体现:
        FileOutputStream fos = new FileOutputStream("fos.txt");            fos.write("hello".getBytes());            fos.close();
    C:要注意的问题?        a:创建字节输出流对象做了几件事情?        b:为什么要close()?        c:如何实现数据的换行?        d:如何实现数据的追加写入?(4)**FileInputStream读取数据**    A:操作步骤        a:创建字节输入流对象        b:调用read()方法        c:释放资源    B:代码体现:
            FileInputStream fis = new FileInputStream("fos.txt");            //方式1            int by = 0;            while((by=fis.read())!=-1) {                System.out.print((char)by);            }            //方式2            byte[] bys = new byte[1024];            int len = 0;            while((len=fis.read(bys))!=-1) {                System.out.print(new String(bys,0,len));            }            fis.close();
(5)案例:2种实现    A:复制文本文件    B:复制图片    C:复制视频(6)字节缓冲区流    A:BufferedOutputStream    B:BufferedInputStream(7)案例:4种实现    A:复制文本文件    B:复制图片    C:复制视频
package cn.itcast_06;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;/* * 需求:把e:\\哥有老婆.mp4复制到当前项目目录下的copy.mp4中 *  * 字节流四种方式复制文件: * 基本字节流一次读写一个字节:   共耗时:117235毫秒 * 基本字节流一次读写一个字节数组: 共耗时:156毫秒 * 高效字节流一次读写一个字节: 共耗时:1141毫秒 * 高效字节流一次读写一个字节数组: 共耗时:47毫秒 */public class CopyMp4Demo {    public static void main(String[] args) throws IOException {        long start = System.currentTimeMillis();        // method1("e:\\哥有老婆.mp4", "copy1.mp4");        // method2("e:\\哥有老婆.mp4", "copy2.mp4");        // method3("e:\\哥有老婆.mp4", "copy3.mp4");        method4("e:\\哥有老婆.mp4", "copy4.mp4");        long end = System.currentTimeMillis();        System.out.println("共耗时:" + (end - start) + "毫秒");    }    // 高效字节流一次读写一个字节数组:    public static void method4(String srcString, String destString)            throws IOException {        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(                srcString));        BufferedOutputStream bos = new BufferedOutputStream(                new FileOutputStream(destString));        byte[] bys = new byte[1024];        int len = 0;        while ((len = bis.read(bys)) != -1) {            bos.write(bys, 0, len);        }        bos.close();        bis.close();    }    // 高效字节流一次读写一个字节:    public static void method3(String srcString, String destString)            throws IOException {        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(                srcString));        BufferedOutputStream bos = new BufferedOutputStream(                new FileOutputStream(destString));        int by = 0;        while ((by = bis.read()) != -1) {            bos.write(by);        }        bos.close();        bis.close();    }    // 基本字节流一次读写一个字节数组    public static void method2(String srcString, String destString)            throws IOException {        FileInputStream fis = new FileInputStream(srcString);        FileOutputStream fos = new FileOutputStream(destString);        byte[] bys = new byte[1024];        int len = 0;        while ((len = fis.read(bys)) != -1) {            fos.write(bys, 0, len);        }        fos.close();        fis.close();    }    // 基本字节流一次读写一个字节    public static void method1(String srcString, String destString)            throws IOException {        FileInputStream fis = new FileInputStream(srcString);        FileOutputStream fos = new FileOutputStream(destString);        int by = 0;        while ((by = fis.read()) != -1) {            fos.write(by);        }        fos.close();        fis.close();    }}

字符流
(1)字节流操作中文数据不是特别的方便,所以就出现了转换流。
转换流的作用就是把字节流转换字符流来使用。
(2)转换流其实是一个字符流
字符流 = 字节流 + 编码表
(3)编码表
A:就是由字符和对应的数值组成的一张表
B:常见的编码表
ASCII
ISO-8859-1
GB2312
GBK
GB18030
UTF-8
C:字符串中的编码问题
编码
String – byte[]
解码
byte[] – String
(4)IO流中的编码问题
A:OutputStreamWriter
OutputStreamWriter(OutputStream os):默认编码,GBK
OutputStreamWriter(OutputStream os,String charsetName):指定编码。
B:InputStreamReader
InputStreamReader(InputStream is):默认编码,GBK
InputStreamReader(InputStream is,String charsetName):指定编码
C:编码问题其实很简单
编码只要一致即可
(5)字符流
Reader
|–InputStreamReader
|–FileReader
|–BufferedReader
Writer
|–OutputStreamWriter
|–FileWriter
|–BufferedWriter
(6)复制文本文件(5种方式)

2:IO流小结(掌握)
IO流
|–字节流
|–字节输入流
InputStream
int read():一次读取一个字节
int read(byte[] bys):一次读取一个字节数组

                |--FileInputStream                |--BufferedInputStream        |--字节输出流            OutputStream                void write(int by):一次写一个字节                void write(byte[] bys,int index,int len):一次写一个字节数组的一部分                |--FileOutputStream                |--BufferedOutputStream    |--字符流        |--字符输入流            Reader                int read():一次读取一个字符                int read(char[] chs):一次读取一个字符数组                |--InputStreamReader                    |--FileReader                |--BufferedReader                    String readLine():一次读取一个字符串        |--字符输出流            Writer                void write(int ch):一次写一个字符                void write(char[] chs,int index,int len):一次写一个字符数组的一部分                |--OutputStreamWriter                    |--FileWriter                |--BufferedWriter                    void newLine():写一个换行符                    void write(String line):一次写一个字符串
package cn.itcast_01;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;/* * 复制文本文件 *  * 分析: * 复制数据,如果我们知道用记事本打开并能够读懂,就用字符流,否则用字节流。 * 通过该原理,我们知道我们应该采用字符流更方便一些。 * 而字符流有5种方式,所以做这个题目我们有5种方式。推荐掌握第5种。 * 数据源: * c:\\a.txt -- FileReader -- BufferdReader * 目的地: * d:\\b.txt -- FileWriter -- BufferedWriter */public class CopyFileDemo {public static void main(String[] args) throws IOException {String srcString = "c:\\a.txt";String destString = "d:\\b.txt";// method1(srcString, destString);// method2(srcString, destString);// method3(srcString, destString);// method4(srcString, destString);method5(srcString, destString);}// 字符缓冲流一次读写一个字符串private static void method5(String srcString, String destString)throws IOException {BufferedReader br = new BufferedReader(new FileReader(srcString));BufferedWriter bw = new BufferedWriter(new FileWriter(destString));String line = null;while ((line = br.readLine()) != null) {bw.write(line);bw.newLine();bw.flush();}bw.close();br.close();}// 字符缓冲流一次读写一个字符数组private static void method4(String srcString, String destString)throws IOException {BufferedReader br = new BufferedReader(new FileReader(srcString));BufferedWriter bw = new BufferedWriter(new FileWriter(destString));char[] chs = new char[1024];int len = 0;while ((len = br.read(chs)) != -1) {bw.write(chs, 0, len);}bw.close();br.close();}// 字符缓冲流一次读写一个字符private static void method3(String srcString, String destString)throws IOException {BufferedReader br = new BufferedReader(new FileReader(srcString));BufferedWriter bw = new BufferedWriter(new FileWriter(destString));int ch = 0;while ((ch = br.read()) != -1) {bw.write(ch);}bw.close();br.close();}// 基本字符流一次读写一个字符数组private static void method2(String srcString, String destString)throws IOException {FileReader fr = new FileReader(srcString);FileWriter fw = new FileWriter(destString);char[] chs = new char[1024];int len = 0;while ((len = fr.read(chs)) != -1) {fw.write(chs, 0, len);}fw.close();fr.close();}// 基本字符流一次读写一个字符private static void method1(String srcString, String destString)throws IOException {FileReader fr = new FileReader(srcString);FileWriter fw = new FileWriter(destString);int ch = 0;while ((ch = fr.read()) != -1) {fw.write(ch);}fw.close();fr.close();}}
原创粉丝点击