栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

javaIO输入输出流

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

javaIO输入输出流

java IO流
  • 一、IO流概述
    • 1.IO流的概念及作用
    • 2.IO流的类结构图
    • 3.IO流分类
  • 二、代码示例
    • 1.File类
    • 2.IO流
      • (1).close(),关闭资源
      • (2).字符输出流
      • (2).字符输入流
      • (3).字节输出流
      • (4).字节输入流
      • (5).文件复制
      • (6).字符集编码转换
      • (7).main(),测试
      • (8).运行结果

一、IO流概述 1.IO流的概念及作用
  • IO(input输入,output输出),是指应用程序与外部设备之间的数据传递。
  • 流(Stream),是一种抽象概念,它代表了数据的无结构化传递,其本质就是数据传输。
2.IO流的类结构图

3.IO流分类
  • 按数据流向不同分为:输入流(读取)、输出流(写入)
  • 按处理数据的类型不同分为:字符流(以字符为单位)、字节流(以字节为单位)
  • 按数据的功能不同分为:节点流(程序直接操作数据源)、处理流(节点流外面再套一层,增强读写速度)

二、代码示例 1.File类
public class IOFile {
    public static void main(String[] args) throws IOException {
        //File file = new File("C:\Windows");// 绝对路径
        File file = new File("IO.txt");// 相对路径,IDEA中默认相对于工程根目录

        // 常用方法
        /
public static void close(Closeable... resources) {
    for (Closeable resource : resources) {
        if (resource != null) {
            try {
                resource.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
(2).字符输出流
public static void writerTest(String str, String pathName) {
    // 提升需要关闭的资源的作用域
    BufferedWriter bw = null;
    FileWriter fw = null;
    try {
        // 1.获取文件的输出流(节点流)
        fw = new FileWriter(new File(pathName));
        // 2.在文件输出流上套一层处理流(可选)
        bw = new BufferedWriter(fw);
        // 3.操作流资源
        bw.write(str);// 字符输出流可直接写入字符串
        System.out.println(pathName + "-->writer写入完成:"" + str + """);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        // 4.关闭资源
        close(bw, fw);
    }
}
(2).字符输入流
public static void readerTest(String pathName) {
    // 提升需要关闭的资源的作用域
    FileReader fr = null;
    BufferedReader br = null;
    try {
        // 1.获取文件的输入流(节点流)
        fr = new FileReader(pathName);
        // 2.在文件输入流上套一层处理流(可选)
        br = new BufferedReader(fr);
        // 3.操作流资源
        StringBuilder result = new StringBuilder();// 保存所有文本
        String line;// 保存每行的文本
        while ((line = br.readLine()) != null) {
            result.append(line + "n");// 每次读取一行文本,将其添加到结果中
        }
        result.deleteCharAt(result.length()-1);// 删除最后一个换行符
        System.out.println(pathName + "-->reader读取的内容:"" + result + """);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // 4.关闭资源
        close(br, fr);
    }
}
(3).字节输出流
public static void outputStreamTest(String str, String pathName) {
    // 提升需要关闭的资源的作用域
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    try {
        // 1.获取文件的输出流(节点流)
        fos = new FileOutputStream(pathName);
        // 2.在文件输出流上套一层处理流(可选)
        bos = new BufferedOutputStream(fos);
        // 3.操作流资源
        bos.write(str.getBytes());// 将字符串转为字节数组写入到文件中,文件不存在会先创建文件
        System.out.println(pathName + "-->outputStream写入完成:"" + str + """);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // 关闭资源
        close(bos, fos);
    }
}
(4).字节输入流
public static void inputStreamTest(String pathName) {
    // 提升需要关闭的资源的作用域
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    ByteArrayOutputStream baos = null;
    try {
        // 1.获取文件的输入流(节点流)
        fis = new FileInputStream(pathName);
        // 2.在文件输入流上套一层处理流(可选)
        bis = new BufferedInputStream(fis);
        // 3.操作流资源
        byte[] buffer = new byte[1024];// 每次读取的最大长度:1kb
        int len; // 实际每次读取的长度
        baos = new ByteArrayOutputStream();// 创建数组输出流保存读取的数据(防止中文乱码)
        while ((len = bis.read(buffer)) != -1) {
            baos.write(buffer, 0, len);// 将每次读取的数据写入到数组输出流中
        }
        System.out.println(pathName + "-->InputStream读取的内容:"" + baos.toString() + """);// 将数组流转换成字符串输出
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // 4.关闭资源
        close(baos, bis, fis);
    }
}
(5).文件复制
public static void copy(String fromPath,String toPath) {
    long start = System.currentTimeMillis();
    // 提升需要关闭的资源的作用域
    FileInputStream fromFis = null;
    FileOutputStream toFos = null;
    BufferedInputStream fromBis = null;
    BufferedOutputStream toBos = null;
    try {
        // 1.获取文件输入输出流
        fromFis = new FileInputStream(fromPath);
        toFos = new FileOutputStream(toPath);
        // 2.在文件输入输出流上套一层处理流(可选)
        fromBis = new BufferedInputStream(fromFis);
        toBos = new BufferedOutputStream(toFos);
        // 3.操作流资源
        byte[] buffer = new byte[1024];
        int len;
        while ((len = fromBis.read(buffer)) != -1) {
            toBos.write(buffer, 0, len);
        }
        long end = System.currentTimeMillis();
        System.out.println(fromPath + "-->" + toPath + "[复制完成](大小:" + new File(fromPath).length() + "Byte,总耗时:" + (end-start) + "毫秒)");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // 4.关闭资源
        close(toBos, fromBis, toFos, fromFis);
    }
}
(6).字符集编码转换
public static void charsetConversion(String fromPath, String fromCharsetName, String toCharsetName) {
    // 提升需要关闭的资源的作用域
    FileInputStream fis = null;
    FileOutputStream fos = null;
    InputStreamReader isr = null;
    OutputStreamWriter osw = null;
    BufferedReader br = null;
    BufferedWriter bw = null;
    try {
        // 1.获取输入流
        fis = new FileInputStream(fromPath);
        // 2.设置输入流的字符集编码
        isr = new InputStreamReader(fis, fromCharsetName);
        // 3.在输入流上套一层处理流(可选)
        br = new BufferedReader(isr);
        // 4.将输入流读取的数据写入到字符数组流中
        char[] buffer = new char[1024];
        int len;
        CharArrayWriter caw = new CharArrayWriter();
        while ((len = br.read(buffer)) != -1) {
            caw.write(buffer, 0, len);
        }
        // 5.获取输出流
        fos = new FileOutputStream(fromPath);
        // 6.设置输出流的字符集编码
        osw = new OutputStreamWriter(fos, toCharsetName);
        // 3.在输出流上套一层处理流(可选)
        bw = new BufferedWriter(osw);
        // 4.将字符数组流写入到文件中
        bw.write(caw.toCharArray());
        System.out.println(fromPath + "-->["+fromCharsetName + " >>> " + toCharsetName + "](转换完成)");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        // 5.关闭资源
        close(bw, osw, fos, br, isr, fis);
    }
}
(7).main(),测试
public static void main(String[] args) {
    writerTest("writer测试", "D:\IO.txt");
    readerTest("D:\IO.txt");
    outputStreamTest("outputStream测试", "D:\IO.txt");
    inputStreamTest("D:\IO.txt");
    copy("D:\IO.txt", "E:\IOCopy.txt");
    charsetConversion("D:\IO.txt", "GBK", "UTF-8");
    readerTest("D:\IO.txt");
    inputStreamTest("D:\IO.txt");
    charsetConversion("D:\IO.txt", "UTF-8", "GBK");
    readerTest("D:\IO.txt");
    inputStreamTest("D:\IO.txt");
}
(8).运行结果
D:IO.txt-->writer写入完成:"writer测试"
D:IO.txt-->reader读取的内容:"writer测试"
D:IO.txt-->outputStream写入完成:"outputStream测试"
D:IO.txt-->InputStream读取的内容:"outputStream测试"
D:IO.txt-->E:IOCopy.txt[复制完成](大小:18Byte,总耗时:0毫秒)
D:IO.txt-->[GBK >>> UTF-8](转换完成)
D:IO.txt-->reader读取的内容:"outputStream娴嬭瘯"
D:IO.txt-->InputStream读取的内容:"outputStream娴嬭瘯"
D:IO.txt-->[UTF-8 >>> GBK](转换完成)
D:IO.txt-->reader读取的内容:"outputStream测试"
D:IO.txt-->InputStream读取的内容:"outputStream测试"
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/328494.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号