Java学习日记 2022.7.8


IO流

FileInputStream字节流只能读AscII码文件,文件中有中文都不能用他读

序列化和反序列化

NotSerializableException 不允许序列化异常
对象从内存中写入磁盘的过程称为对象的持久化,也称为序列化
从磁盘(网络)中读取文件到内存还原为对象的过程称为反序列化

IO流有几种(流怎么分类)

1.流按照方向分
输入流
数据从磁盘(网络)中读取到内存
输出流
数据从内存中写入磁盘
2.流按照传输单位分
字节流
传输单位是Byte
字符流
传输单位是Char

Java提供了4个基类

InPutStream
OutPutStream
Reader
Writer

字节流中常用的

文件 FileOutputStream FileInputStream
对象 ObjectOutputStream ObjectInputStream
二进制 DataOutputStream DataInputStrea

字符流中常用的

文件 FileReader FileWriter
一次性读一行的缓冲流 BufferedReader BufferedWriter

具体代码演示

FileInputStream

    public static void main(String[] args) {
        try {
            //文件名需要写绝对路径,路径的分隔符用正斜杠和反斜杠都可以
            //正斜杠写一个即可,反斜杠需要写两个
            //因为一个反斜杠在字符串中是“转义符”
            //String name = "C:/Users/35965/Desktop/java全栈/java/java.txt";
            String name = "D:\\35965\\test\\Hello.txt";
            final FileInputStream is = new FileInputStream(name);
//            int d;
//            while((d = is.read()) != -1){
//                System.out.println((char) d);
//            }
            byte[] data = new byte[5];
            int n;
            String str = "";
            while ((n = is.read(data)) != -1){
//                for (int i = 0; i < n; i++) {
//                    System.out.println((char) data[i]);
//                }
                str += new String(data);
            }
            System.out.println(str);
            System.out.println(str.length());
            is.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

FileReader

public static void main(String[] args) {
        String name = "D:\\35965\\test\\Hello.txt";
        Reader reader = null;
        try {
            reader = new FileReader(name);
            int d;
            while ((d = reader.read()) != -1){
                System.out.print((char) d);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

FileWriter

 static String[] arr = {"面向对象", "封装", "继承", "多态"
            , "数组", "父类", "抽象类", "接口", "异常", "运行时异常"
            , "编译时异常", "抽象方法", "成员方法", "构造方法","静态方法"
            ,"成员变量", "局部变量", "静态变量", "常量", "重写", "重载"
            , "向上转型", "向下转型"};
    static Random random = new Random();
    public static void main(String[] args) {
        //创建一个词频测试文件
        try {
            String name = "D:\\35965\\test\\words.txt";
            Writer writer = new FileWriter(name);
            for (int i = 1; i <= 10000; i++) {
                int index = random.nextInt(arr.length); //每循环一次僧成一个随机整数
                if (i % 10 == 0){
                    writer.write(arr[index] + "\n");
                } else {
                    writer.write(arr[index] + " ");
                }
            }
            writer.flush(); //冲刷流中数据到磁盘
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

FileReader\BufferedReader

public static void main(String[] args) {
        //用于存储词频统计结果的HashMap
        Map<String,Integer> map = new HashMap<>();
        //读词频文件
        String name = "D:\\35965\\test\\words.txt";
        try {
            Reader reader = new FileReader(name);
            //用字节流对象reader作为缓冲流BufferedReader的参数
            //bufferedReader是包装流,包装了Reader,即底层是Reader
            BufferedReader buffReader = new BufferedReader(reader);
            String line = null;
            while ((line = buffReader.readLine()) != null){
                //拆分字符串,String类提供了一个成员方法split("分隔符"),返回拆分后的字串数组
                String[] arr = line.split(" ");
                for (String word : arr) {   //遍历字串数组arr。取出每一段
                    if (map.get(word) == null){ //用word作为key去HashMap的key
                        map.put(word, 1);
                    } else {
                        map.put(word, map.get(word)+1);
                    }
                }
            }
            //在箭头函数内部不能访问外部的变量,除非是常量或者是线程安全的类
            //final int count = 0;
            //1.不使用forEach,改用迭代器
//            map.forEach((word, count) -> {
//                System.out.println(word + ":" + count);
//            });
            int count = 0;
            Set<Map.Entry<String, Integer>> entries = map.entrySet();
            for (Map.Entry<String, Integer> entry:entries){
                String key = entry.getKey();
                Integer value = entry.getValue();
                count += value;
                System.out.println(key + ":" + value);
            }
            System.out.println(count);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

FileInputStream\ ObjectInputStream

public static void main(String[] args) {
        //对象输入流,从磁盘读取文件反序列化还远为对象
        String name = "D:\\35965\\test\\car";
        try {
            FileInputStream is = new FileInputStream(name);
            ObjectInputStream ois = new ObjectInputStream(is);
            // readObject()读取磁盘文件内容并反序列化为对象,返回Object类型
            //需要手动向下转型,才能得到car类型
            Car car = (Car) ois.readObject();
            System.out.println(car.getBrand());
            System.out.println(car.getModel());
            System.out.println(car.getColor());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

实现统计的排序

    static String[] arr = {"面向对象", "封装", "继承", "多态"
            , "数组", "父类", "抽象类", "接口", "异常", "运行时异常"
            , "编译时异常", "抽象方法", "成员方法", "构造方法","静态方法"
            ,"成员变量", "局部变量", "静态变量", "常量", "重写", "重载"
            , "向上转型", "向下转型"};
    static Random random = new Random();
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        Map<Integer, String> treemap = new TreeMap<>();

        String name = "D:\\35965\\test\\words.txt";
        Writer writer = null;
        try {
            writer = new FileWriter(name);
            for (int i = 1; i < 10000; i++) {
                int index = random.nextInt(arr.length);
                if (i % 10 == 0){
                    writer.write(arr[index] + "\n");
                } else {
                    writer.write(arr[index] + " ");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        Reader reader = null;
        BufferedReader buffRead = null;
        try {
            reader = new FileReader(name);
            buffRead = new BufferedReader(reader);
            String line = null;
            while((line = buffRead.readLine()) != null){
                String[] array = line.split(" ");
                for (String word : array) {
                    if ((map.get(word)) == null){
                        map.put(word,1);
                    }else {map.put(word,map.get(word) + 1);
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (buffRead != null) {
                try {
                    buffRead.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("***********排序前:*************");
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        for (Map.Entry<String, Integer> entry : entries){
            String key = entry.getKey();
            Integer value = entry.getValue();
            treemap.put(value,key);
            System.out.println(key + ":" + value);
        }
        System.out.println("***********排序后:**************");
        treemap.forEach((key,value) ->{
            System.out.println(value + ":" + key);
        });
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值