• 文件字符流


    文件字符流复制操作

    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    public class TestFileCopy2 {
        public static void main(String[] args) {
            // 写法和使用Stream基本一样。只不过,读取时是读取的字符。
            FileReader fr = null;
            FileWriter fw = null;
            int len = 0;
            try {
                fr = new FileReader("d:/a.txt");
                fw = new FileWriter("d:/d.txt");
                //为了提高效率,创建缓冲用的字符数组
                char[] buffer = new char[1024];
                //边读边写
                while ((len = fr.read(buffer)) != -1) {
                    fw.write(buffer, 0, len);
                }
     
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fw != null) {
                        fw.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (fr != null) {
                        fr.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    处理字节流数组时:

    1:源头换成字节流数组

    2:字节流数组不用关(关了也没事)

    3:任何东西都可以转成字节流数组

    4:字节流数组不要太大

    ByteArrayInputStream和ByteArrayOutputStream经常用在需要流和数组之间转化的情况

    FileInputStream是把文件当做数据源。ByteArrayInputStream则是把内存中的”某个字节数组对象”当做数据源。

    import java.io.ByteArrayInputStream;
    import java.io.IOException;
     
    public class TestByteArray {
        public static void main(String[] args) {
            //将字符串转变成字节数组
            byte[] b = "abcdefg".getBytes();
            test(b);
        }
        public static void test(byte[] b) {
            ByteArrayInputStream bais = null;
            StringBuilder sb = new StringBuilder();
            int temp = 0;
            //用于保存读取的字节数
            int num = 0; 
            try {
                //该构造方法的参数是一个字节数组,这个字节数组就是数据源
                bais = new ByteArrayInputStream(b);
                while ((temp = bais.read()) != -1) {
                    sb.append((char) temp);
                    num++;
                }
                System.out.println(sb);
                System.out.println("读取的字节数:" + num);
            } finally {
                try {
                    if (bais != null) {
                        bais.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
  • 相关阅读:
    多维数组,转化为一维数组多种解决方案
    word-wrap与word-break的区别,以及无效情况
    重温前端基础之-js排序算法
    重温前端基础之-css浮动之怪异现象
    重温前端基础之-css浮动与清除浮动
    重温前端基础之-css盒模型
    C# 应用
    C# 应用
    C# 应用
    C# 应用
  • 原文地址:https://www.cnblogs.com/cykfory/p/10297790.html
Copyright © 2020-2023  润新知