zoukankan      html  css  js  c++  java
  • IO_处理流、转换流

    处理流

    处理流:用于增强功能,提高性能。一定要在节点流之上。(对其他的流的处理)

    一、缓冲流

    1、字节流缓冲流——针对字节流的处理流

    BufferedInputStream

    BufferedOutputStream

    2、字符流缓冲流——针对字符流的处理流

    BufferedReader        readLine( ):读取一行

    BufferedWriter        newLine( ):写入一个行分隔符

    /**
     * 字节流拷贝文件 加入缓冲流 提高性能
     * 缓冲流(节点流)——包裹
     * 以后使用过程中加入,提高性能
     * @author chenpeng
     * @date 2018/5/29 21:38
     */
    public class Buffered {
        /**
         *  文件的拷贝
         * @param src 源文件File对象
         * @param des 目标文件FIle对象
         */
        public static void copyFILE(File src, File des) throws IOException {
            if (!src.isFile()){//不是文件或者为null
                System.out.println("只能拷贝文件");
                throw new IOException("只能拷贝文件");
            }
            //如果des为已存在的文件夹,则不能建立与文件夹同名的文件
            if (des.isDirectory()){
                System.out.println("不能建立与文件夹同名的文件");
                throw new IOException("不能建立与文件夹同名的文件");
            }
    
            //2、选择流
            InputStream is =new  BufferedInputStream(new FileInputStream(src));
            OutputStream os =new BufferedOutputStream(new FileOutputStream(des));
            //3、文件拷贝——循环读取和写出文件
            byte[] flu = new byte[1024];//缓存数组
            int len = 0;//定义的接收长度
            //读取
            while (-1!=(len = (is.read(flu)))){
                //写出
                os.write(flu,0,len);
            }
            os.flush();//强制刷出
    
            //关闭流——规则:先打开的后关闭
            os.close();
            is.close();
        }
    }
    


    /**
     * 字符缓冲流 + 新增功能(使用新增方法不能发生多态)
     *
     * @author 
     * @date 2018/5/29 22:18
     */
    public class BufferedDemo {
        public static void main(String[] args) {
            //1、创建连接
            File src = new File("F:/test/1.txt");
            File des = new File("F:/test/p/100.txt");
    
            //选择流
            BufferedReader reader = null;
            BufferedWriter writer = null;
            try {
                reader = new BufferedReader(new FileReader(src));
                writer= new BufferedWriter(new FileWriter(des));
    
               /* char[] flu = new char[1024];
                int len = 0;
                //持续读取、写入
                while (-1!=(len = reader.read(flu))){
                    writer.write(flu,0,len);
                }
                */
                //改成一行一行的读取
                String line = null;
                while (null!=(line = (reader.readLine()))){
                    writer.write(line);
                    writer.newLine();//换行符
                }
    
                writer.flush();//强制刷出
    
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                //关闭
                try {
                    if(null!=writer){
                        writer.close();
                    }
                    if (null!=reader){
                        reader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    转换流

    转换流:字节流转字符流  处理乱码(编码集、解码集)

    1、编码与解码

    编码:字符   ——编码字符集——>   二进制

    解码:二进制    ——解码字符集——>   字符

    2、乱码:

    1)、编码与解码的字符集不统一

    2)、字节缺少,长度丢失

    public class Convert {
        public static void main(String[] args) throws UnsupportedEncodingException {
            String str = "中国";
            byte[] data = str.getBytes();
            //字节数不完整乱码
            System.out.println(new String(data,0,3));
            
        }
    
        /**
         * 编码与解码字符集必须统一,否则乱码
         * @throws UnsupportedEncodingException
         */
        public void test1() throws UnsupportedEncodingException {
            //解码 byte——>char
            String str = "中国";//utf-8
            //编码 char——>byte
            byte[] data = str.getBytes();
            //编码与解码字符集统一
            System.out.println(new String(data));
            //设定编码字符集
            data = str.getBytes("gbk");
            //字符集不统一乱码
            System.out.println(new String(data));
        }
    }
    




  • 相关阅读:
    ASP.NET中如何防范SQL注入式攻击?(转)
    打开D盘时速度奇慢?
    Visual Studio 2008 下载地址
    如何利用XML文件,做为配置参数?
    如何将一个表中的数据INSERT INTO 到另一个表中?
    拖延交货或惹万人诉讼 消费者称戴尔态度恶劣
    NHibernate Linq中Null值排序的解决方法
    NHibernate3剖析:Query篇之NHibernate.Linq标准查询
    Nhibernate出现No row with the given identifier exists问题的产生原因及解决方法
    Nhibernate使用动态Expression的问题解决
  • 原文地址:https://www.cnblogs.com/huangzhe1515023110/p/9276040.html
Copyright © 2011-2022 走看看