java中IO的原理是什么

发布时间:2021-07-05 18:36:22 作者:Leah
来源:亿速云 阅读:142

本篇文章为大家展示了java中IO的原理是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。


一、IO概念

1.什么是输入

程序从内存中读取数据叫输入Input。

2.什么输出(Output)

程序把数据写入到内存中叫输出Output。

二、流的分类

IO流体系

java中IO的原理是什么

1、InputStream(字节流)

示例:

 public static void main(String[] args) {
       iprt();
    }
    public static void ipst(){
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream("C:\\1.txt");
            int i;
            while ( (i = inputStream.read()) != -1){
                System.out.print((char) i);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null){
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

说明:使用InputStream向内存中读如文件数据。

2、OutputStream(字节流)

示例:

public class ImageCopy {
    public static void main(String[] args) {
        try(
                InputStream inputStream = new FileInputStream("D:\\KDA.jpg");
                OutputStream outputStream = new FileOutputStream("E:\\aaa\\KDA.jpg")
         ){
            byte[] bytes = new byte[1024];
            int i;
            while ((i = inputStream.read(bytes)) != -1){
                outputStream.write(bytes,0,i);
            }
        }  catch (IOException e) {
            e.printStackTrace();
        }
    }
}

说明:使用输入流与输出流结合实现图片复制的功能。

3、Reader(字符流)

示例:

public static  void iprt(){
        Reader reader = null;
        try {
            reader = new FileReader("C:\\1.txt");
            int i ;
            while ((i =  reader.read()) != -1){
                System.out.print((char) i);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
                try {
                    if (reader != null) {
                        reader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }

说明:使用Reader(字符流)从文件中读入数据。

4、Writer(字符流)

public static  void iprt(){
        Reader reader = null;
        Writer writer = null;
        try {
            reader = new FileReader("C:\\Users\\52425\\Desktop\\1.txt");
            writer = new FileWriter("C:\\2.txt");
            int i ;
            while ((i =  reader.read()) != -1){
                writer.write(i);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
                try {
                        writer.close();
                        reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }

说明:使用字符流实现文件复制功能。

三、总结(1+2)

1. File类及方法的使用

File是操作文件/目录的类,可以对文件/目录进行创建,重命名, 删除等操作。

2.IO流的分类

3.IO流的四个基本类

上述内容就是java中IO的原理是什么,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. java中的io流是什么
  2. java中nio和io的区别是什么

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:MyBatis-Plus中ActiveRecord(AR)如何使用

下一篇:java泛型方法指什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》