强哥说Java--IO流(二)

简介: 强哥说Java--IO流(二)

三、节点流(或文件流)



读取文件


@Test
    public void test() {
        FileInputStream fis = null;
        try {
            //1.指明要操作的文件
            File file = new File("hello.txt");
            //2.提供具体的流
            fis = new FileInputStream(file);
            //3.读数据
            byte[] buffer = new byte[5];
            int len;//记录每次读取的字节的个数
            while ((len = fis.read(buffer)) != -1) {
                String str = new String(buffer, 0, len);
                System.out.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                //4.关资源
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


写入文件


@Test
public void testFileWriter() {
    FileWriter fw = null;
    try {
        //1.提供File类的对象,指明写出到的文件
        File file = new File("hello1.txt");
        //2.提供FileWriter的对象,用于数据的写出
        fw = new FileWriter(file, false);
        //3.写出的操作
        fw.write("I hava a dream\n");
        fw.write("aoligei!");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        //4.流资源的关闭
        try {
            if (fw != null) {
                fw.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


节点流(或文件流):注意点


image.png


四、缓冲流



什么是缓冲流?

为了提高数据读写的速度,JavaAPI提供了带缓冲功能的流类,在使用这些流类时,会创建一个内部缓冲区数组,缺省使用8192个字节(8Kb)的缓冲区。


image.png


缓冲流要“套接”在相应的节点流之上,根据数据操作单位可以把缓冲流分为:


BufferedInputStream 和 BufferedOutputStream


BufferedReader 和 BufferedWriter


怎么工作的?

当读取数据时,数据按块读入缓冲区,其后的读操作则直接访问缓冲区


当使用BufferedInputStream读取字节文件时,BufferedInputStream会一 次性从文件中读取8192个(8Kb), 存在缓冲区中,直到缓冲区装满了,才重新从文件中读取下一个8192个字节数组


向流中写入字节时,不会直接写到文件,先写到缓冲区中直到缓冲区写满,BufferedOutputStream才会把缓冲区中的数据一 次性写到文件里。使用方法flush()可以强制将缓冲区的内容全部写入输出流


关闭流的顺序和打开流的顺序相反。只要关闭最外层流即可,关闭最外层流也会相应关闭内层节点流


flush()方法的使用:手动将buffer中内容写入文件


如果是带缓冲区的流对象的close()方法,不但会关闭流,还会在关闭流之前刷新缓冲区,关闭后不能再写出


图解缓冲流


image.png


实例


@Test
    public void BufferdStreamTest() {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            //1.造文件
            File srcFile = new File("cxk.jpg");
            File destFile = new File("cxk3.jpg");
            //2.造流
            //2.1节点流
            FileInputStream fis = new FileInputStream(srcFile);
            FileOutputStream fos = new FileOutputStream(destFile);
            //2.2缓冲流
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);
            //3.复制的细节:读取写入
            byte[] buffer = new byte[10];
            int len;
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.资源关闭
            //要求:先关闭外层的流,在关闭内层的流
            //说明:关闭外层流的同时,内层流会自动进行关闭,关闭内层流的操作可以省略
//        fos.close();
//        fis.close();
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


缓冲流练习


package com.caq.exer;
import org.junit.Test;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
 * 获取文本上字符出现的次数,把数据写入文件
 *
 * 思路:
 * 1.遍历文本每一个字符
 * 2.字符出现的次数存在Map中
 *
 * Map<Character,Integer> map = new HashMap<Character,Integer>();
 * map.put('a',18);
 * map.put('你',2);
 *
 * 3.把map中的数据写入文件
 *
 */
public class WordCount {
    /*
    说明:如果使用单元测试,文件相对路径为当前module
          如果使用main()测试,文件相对路径为当前工程
     */
    @Test
    public void testWordCount() {
        FileReader fr = null;
        BufferedWriter bw = null;
        try {
            //1.创建Map集合
            Map<Character, Integer> map = new HashMap<Character, Integer>();
            //2.遍历每一个字符,每一个字符出现的次数放到map中
            fr = new FileReader("dbcp.txt");
            int c = 0;
            while ((c = fr.read()) != -1) {
                //int 还原 char
                char ch = (char) c;
                //判断char是否在map中第一次出现
                if (map.get(ch) == null) {
                    map.put(ch, 1);
                } else {
                    map.put(ch, map.get(ch) + 1);
                }
            }
            //3.把map中数据存在文件count.txt
            //3.1 创建Writer
            bw = new BufferedWriter(new FileWriter("wordcount.txt"));
            //3.2 遍历map,再写入数据
            Set<Map.Entry<Character, Integer>> entrySet = map.entrySet();
            for (Map.Entry<Character, Integer> entry : entrySet) {
                switch (entry.getKey()) {
                    case ' ':
                        bw.write("空格=" + entry.getValue());
                        break;
                    case '\t'://\t表示tab 键字符
                        bw.write("tab键=" + entry.getValue());
                        break;
                    case '\r'://
                        bw.write("回车=" + entry.getValue());
                        break;
                    case '\n'://
                        bw.write("换行=" + entry.getValue());
                        break;
                    default:
                        bw.write(entry.getKey() + "=" + entry.getValue());
                        break;
                }
                bw.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关流
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
相关文章
|
10月前
|
Java
Java IO流各场景详解及使用
Java IO流各场景详解及使用
83 0
|
2月前
|
Java
Java的IO流
Java的IO流
17 0
|
2月前
|
Java 数据库连接 Windows
java中的IO流
java中的IO流
48 0
|
2月前
|
Java Linux
|
10月前
|
Java Linux Android开发
java中iO流
java中iO流
54 0
|
存储 设计模式 Java
Java IO流详解
Java IO流详解
143 0
|
存储 Java 索引
Java之IO流使用
1.2 文件流 (1)文件在程序中是以流的形式来操作的 (2)流:数据在数据源(文件)和程序(内存)之间经历的路径 ①输入流:数据从数据源(文件)到程序(内存)的路径 ②输出流:数据从程序(内存)到数据源(文件)的路径
80 0
Java之IO流使用
|
存储 算法 Java
Java IO流(上)
File类概述:File类的对象代表操作系统的文件(文件、文件夹),File类在java.io.File包下。 File类提供了诸如:创建文件对象代表文件,获取文件信息(大小、修改时间)、删除文件、创建文件(文件夹)等功能 。 IO流: 用来读写数据的。
107 0
Java IO流(上)
|
存储 缓存 Java
Java IO流(下)
字符流、缓冲流 转换流 序列化 IO框架等 打印流
160 0
Java IO流(下)
|
存储 Java 数据处理
Java -IO流
I/O是Input/Output的缩写, I/O技术是非常实用的技术,用于处理设备之间的数据传输。如读/写文件,网络通讯等。
70 0