浅谈java IO流——四大抽象类

yizhihongxing

介绍Java IO流前,先来明确一下IO流的概念。IO(Input/Output)即输入/输出操作,是计算机应用程序与外部世界(用户、文件)进行交互的重要手段。Java IO流是Java程序中用于读写数据的一种机制,Java为此提供了一系列的API以便于开发者使用。

Java IO流分为两种:字节流和字符流。字节流操作所有类型的文件(如音频、视频、图片等),而字符流主要用于操作文本文件(如txt文件)。Java IO流又分为四大抽象类:InputStream、OutputStream、Reader、Writer,其中InputStream和OutputStream是字节流的抽象基类,而Reader和Writer是字符流的抽象基类。

以下是这四个抽象类的具体操作和示例:

InputStream:

InputStream是字节输入流的抽象类,它提供了读取数据的方法。对于InputStream的所有实现类,读取数据的方法都具有类似的名称,如read()、available()等。

示例1:从文件读取数据

    FileInputStream fis = null;
    try {
        fis = new FileInputStream("file.txt");
        int data;
        while ((data = fis.read()) != -1) {
            System.out.println((char)data);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

示例2:从网络读取数据

    URL url = null;
    InputStream is = null;
    try {
        url = new URL("http://www.baidu.com");
        is = url.openStream();
        int data;
        while ((data = is.read()) != -1) {
            System.out.println((char)data);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

OutputStream:

OutputStream是字节输出流的抽象类,它提供了写入数据的方法。同样,对于OutputStream的所有实现类,写入数据的方法都具有类似的名称,如write()、flush()等。

示例1:将数据写入文件

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream("file.txt");
        String data = "Hello, World!";
        fos.write(data.getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

示例2:将数据通过网络写出

    URL url = null;
    OutputStream os = null;
    try {
        url = new URL("http://www.baidu.com");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        os = connection.getOutputStream();
        String data = "name=value";
        os.write(data.getBytes());
        os.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Reader:

Reader是字符输入流的抽象类,它提供了读取数据的方法。同样,对于Reader的所有实现类,读取数据的方法都具有类似的名称,如read()、mark()等。

示例1:从文件中读取数据

    FileReader fr = null;
    try {
        fr = new FileReader("file.txt");
        int data;
        while ((data = fr.read()) != -1) {
            System.out.println((char)data);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            fr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

示例2:从网络中读取数据

    URL url = null;
    Reader reader = null;
    try {
        url = new URL("http://www.baidu.com");
        reader = new InputStreamReader(url.openStream());
        int data;
        while ((data = reader.read()) != -1) {
            System.out.println((char)data);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Writer:

Writer是字符输出流的抽象类,它提供了写入数据的方法。同样,对于Writer的所有实现类,写入数据的方法都具有类似的名称,如write()、flush()等。

示例1:将数据写入文件

    FileWriter fw = null;
    try {
        fw = new FileWriter("file.txt");
        String data = "Hello, World!";
        fw.write(data);
        fw.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

示例2:将数据通过网络写出

    URL url = null;
    Writer writer = null;
    try {
        url = new URL("http://www.baidu.com");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        writer = new OutputStreamWriter(connection.getOutputStream());
        String data = "name=value";
        writer.write(data);
        writer.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

以上就是浅谈Java IO流——四大抽象类的完整攻略,希望对读者有所帮助。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:浅谈java IO流——四大抽象类 - Python技术站

(0)
上一篇 2023年6月27日
下一篇 2023年6月27日

相关文章

  • keil5最新破解教程

    为了保护正版软件的版权,我们不应该支持或鼓励破解软件。要获得 Keil 5 的最新版本,建议购买合法的许可证或加入 Keil 官方计划。如果您有任何合法的使用需求,请考虑咨询 Keil 官方支持团队。 此外,我们也不会提供任何非法破解软件或教程。我们应该尊重知识产权和法律法规。 无论如何,如果您遇到任何 Keil 相关的问题,我们乐意提供帮助和解答。请提出具…

    其他 2023年4月16日
    00
  • PHP命名空间与自动加载机制的基础介绍

    PHP命名空间与自动加载机制的基础介绍 1. 什么是命名空间? 命名空间(Namespace)是 PHP5 中引入的一种组织代码的方式,通过在代码中使用命名空间,可以避免命名冲突,并提高代码的可读性和可维护性。 使用命名空间可以将相关的类、接口、函数等放在同一个命名空间下,使其成为一个逻辑上的独立单元。不同命名空间下的相同名称的类、接口、函数不会发生冲突。 …

    other 2023年6月28日
    00
  • MySQL常见建表选项及约束

    下面是关于MySQL常见建表选项及约束的完整攻略,包括建表选项和约束的介绍、使用方法和两个示例说明。 建表选项 在MySQL中,建表时可以使用多种选项来设置表的属性,常见的建表选项包括: ENGINE:指定表的存储引擎,如InnoDB、MyISAM等; CHARSET:指定表的字符集,如utf8、gbk等; COLLATE:指定表的排序规则,如utf8_ge…

    other 2023年5月6日
    00
  • javascript实现图片预加载和懒加载

    下面是详细的“JavaScript实现图片预加载和懒加载”的攻略教程。 一、图片预加载 1.1 概述 图片预加载是指在页面加载时,提前将需要用到的图片资源加载到缓存中,等到需要显示时再从缓存中获取,以此提高页面的加载速度和用户体验。 1.2 实现方法 1.2.1 使用Image对象 使用Image对象的onload事件,可以在图片加载完成后执行相应的操作。 …

    other 2023年6月25日
    00
  • Vue实现无限级树形选择器

    我们来详细讲解“Vue实现无限级树形选择器”的完整攻略。 树形选择器的设计思路 首先,我们需要了解树形选择器的设计思路。它的基本思路是通过递归渲染节点,构建出一颗树形结构,然后通过点击事件来选中节点,最终构成所需要的选择结果。由于树形结构是具有层级的,因此在递归渲染的过程中需要考虑如何传递层级关系,以便于后续操作。 实现步骤 根据上述设计思路,我们可以得出实…

    other 2023年6月27日
    00
  • springboot启动时如何指定spring.profiles.active

    要指定Spring Boot启动时使用哪个application.properties文件中的配置,可以使用spring.profiles.active属性。这个属性的值可以是”dev”、”test”、”prod”中的任意一个,我们需要创建不同的配置文件来放置不同环境的属性。 下面是指定spring.profiles.active属性的完整攻略: 1.在ap…

    other 2023年6月27日
    00
  • SQL函数将某个字段合并在一起的操作

    对于SQL函数将某个字段合并在一起的操作,可以使用一些字符串函数将视图或者其他查询结果中的多个字段合并成一个字段。以下是常用的字符串函数: CONCAT()函数 该函数用于将多个字符串合并,与“+”运算符具有相同的功能。例如: SELECT CONCAT(‘Hello’, ‘, ‘, ‘World’); 执行结果为: Hello, World 可以将多个字段…

    other 2023年6月25日
    00
  • Java集合TreeSet用法详解

    Java集合TreeSet用法详解 1. 什么是TreeSet TreeSet是Java集合框架中的一种实现,它是一个有序的、支持基本操作(添加、删除、查找)的集合。使用TreeSet可以方便地实现对元素的排序,并且支持非重复元素的存储。 在TreeSet中,元素按照自然顺序或者指定的比较器顺序进行排序,其中自然顺序指元素实现Comparable接口,并且根…

    other 2023年6月26日
    00
合作推广
合作推广
分享本页
返回顶部