Stream

package com.demo.io;

import java.io.*;

/**
 * 1.1 节点流 --> 直接作用于数据源的流
 * 1.2 处理流 --> 包裹节点流
 *   byte[]  <-->  String
 * 2.1 字节流
 * 2.2 字符流
 * 2.3 转换流
 */
public class StreamTest
{
    public static void main(String[] args)
    {
        //byteStream();
        //ReadByBytes();
        //writeByByte();
        //bufferedReadByFileReader();
        //bufferedReadByFileWrite();
        //translationStreamWriter();
        translationStreamReader();
    }

    /***
     * 字符输出流
     */
    private static void writeByByte()
    {
        String path = "F:\\java-demo\\java-demo\\src\\com\\demo\\io\\byteText.dat";
        FileOutputStream fileOutputStream = null;
        try
        {
            fileOutputStream = new FileOutputStream(path);
            StringBuffer sb = new StringBuffer();
            for (int i=0;i<1000000;i++)
            {
                sb.append("字节流输出\n");
            }
            // 将字符串转换成字节数组
            byte[] bytes =null;
            bytes = sb.toString().getBytes();
            System.out.println(bytes.length);
            fileOutputStream.write(bytes);
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (null != fileOutputStream)
            {
                try
                {
                    fileOutputStream.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }
    /**
     * 测试中文乱码
     *
     *  文件大小计算:位(8个二进制位,01代码)-->1个byte(字节)-->1/1024 KB
     *
     * 1.fileInputStream.read(bytes),将流中的数据读入指定的字节数组中,并返回读入的字节数。
     * 2.当流中没有数据时,返回的值为-1。
     * 3.对于一个中文字符,在utf-8的编码下占3个字节,在GBK的格式下占2个字节。
     * 4.假设设定的数据读入时使用的字节数组长度为10字节,而文件大于10字节,在utf-8的编码格式下,将出现
     *   中文乱码的情况。
     * 5.字节流在读取数据时,文档应是utf-8格式编码,否则将出现中文乱码。
     */
    private static void byteStream()
    {
        String path = "F:\\java-demo\\java-demo\\src\\com\\demo\\io\\byteText.dat";
        File file = new File(path);
        System.out.println("返回该文件的字节长度:"+file.length());
        // 如果文件不存在则创建
        if (!file.exists())
        {
            try
            {
                file.createNewFile();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        // 创建文件输入流对象  --> 1.文件路劲最为参数 2.文件作为参数
        //FileInputStream fileInputStream = new FileInputStream(path);
        FileInputStream fileInputStream = null;
        try
        {
            fileInputStream = new FileInputStream(file);
            byte[] bytes =new byte[10];
            // 文件的读取:1.字节读取 2.字节数组读取
            int resoult = fileInputStream.read(bytes);
            System.out.println("返回读取到的字节数:"+resoult);
            // byte[]  --> String : new String(bytes)
            // String --> byte[] : st.getBytes()
            System.out.println(new String(bytes));
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if(null != fileInputStream)
            {
                try
                {
                    fileInputStream.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }

    /***
     * 解决读取到的值有方块的情况
     * int result = fileInputStream.read(bytes);
     * result代表实际读取的字节数,如果实际的字节数小于bytes数组的长度,
     * 可以在已字节构建数组后,用result的值去对字符串进行截取,删除多余的空间。
     */
    private static void ReadByBytes()
    {
        String path = "F:\\java-demo\\java-demo\\src\\com\\demo\\io\\byteText.dat";
        FileInputStream fileInputStream = null;
        try
        {
            fileInputStream = new FileInputStream(path);
            byte[] bytes =new byte[1024]; // 每次以1024个字节的容量去读
            int result = fileInputStream.read(bytes);
            String st = new String(bytes);
            System.out.println(st);
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (null != fileInputStream)
            {
                try
                {
                    fileInputStream.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     *  用FileReader构造带缓冲的字符输入流
     */
    private static void bufferedReadByFileReader()
    {
        String path = "F:\\java-demo\\java-demo\\src\\com\\demo\\io\\byteText.dat";
        FileReader fileReader = null;
        BufferedReader br = null;
        try
        {
            File file = new File(path);
            fileReader = new FileReader(file);
            br = new BufferedReader(fileReader);
            StringBuffer sb = new StringBuffer();
            // 用临时变量line去接收按行读取的值
            //1.可以确保读值的指针不会发生变化
            //2.可以对此次读取的值进行所需操作
            String line = null;
            while(null != (line=br.readLine()))
            {
                sb.append(line);
            }
            System.out.println(sb);
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (null != br )
            {   // 先打开的流后关闭
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != fileReader )
            {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 用FileWriter构造带缓冲的字符输出流 jdk1.8
     * 1.带缓冲的字符输出流,必须使用.flush()方法刷新缓冲区
     * 2.带缓冲的字符流如果流关闭,缓冲区中的数据会强制刷新输出,即使没有刷新缓冲区。
     */
    private static void bufferedReadByFileWrite()
    {
        String path = "F:\\java-demo\\java-demo\\src\\com\\demo\\io\\byteText.dat";
        File file = new File(path);
        FileWriter fw = null;
        BufferedWriter bw = null;
        try
        {
            fw = new FileWriter(file);
            bw = new BufferedWriter(fw);
            bw.write("陕西理工大学_2012级新生");
            // 刷新缓冲区中的数据
            bw.flush();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (null != bw)
            {   // 先开启的流后关闭
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != fw)
            {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     *
     * 利用转换流构建带缓冲的字符输出流 jdk1.8
     * 1.带缓冲的字符输出流,必须使用.flush()方法刷新缓冲区
     * 2.带缓冲的字符流如果流关闭,缓冲区中的数据会强制刷新输出,即使没有刷新缓冲区。
     */
    private static void translationStreamWriter()
    {
        String path = "F:\\java-demo\\java-demo\\src\\com\\demo\\io\\byteText.dat";
        File file = new File(path);
        OutputStream ops = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try
        {
            ops = new FileOutputStream(file); // 字节流
            // 转换流,以utf-8的形式将字节转化成字符,是连接字节流和字符流的桥梁
            osw = new OutputStreamWriter(ops,"utf-8");
            bw = new BufferedWriter(osw);
            bw.write("陕西理工大学_2012级新生");
            bw.flush();
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if (null != bw)
            {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != osw)
            {
                try {
                    osw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(null != ops)
            {
                try {
                    ops.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 由转换流构建的字符输入流,如果编码格式不一致,可能会出现中文乱码情况
     */
    private static void translationStreamReader()
    {
        String path = "F:\\java-demo\\java-demo\\src\\com\\demo\\io\\byteText.dat";
        File file = new File(path);
        InputStream ips = null;
        InputStreamReader isr = null;
        BufferedReader br = null;

        try
        {
            ips = new FileInputStream(path);
            //以utf-8的编码格式将字节流转换成字符流
            // 如果转换流的编码和文件编码不一致,可能会出现乱码
            isr = new InputStreamReader(ips,"utf-8");
            br = new BufferedReader(isr);

            // 用临时变量line去接收按行读取的值
            //1.可以确保读值的指针不会发生变化
            //2.可以对此次读取的值进行所需操作
            String line = null;
            StringBuffer sb = new StringBuffer();
            while(null != (line=br.readLine()))
            {
                sb.append(line);
            }
            System.out.println(sb.toString());
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if (null != br)
            {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != isr)
            {
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != ips)
            {
                try {
                    ips.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

posted @ 2017-12-06 23:36  xinjia  阅读(294)  评论(0编辑  收藏  举报