博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java.io几种读写文件的方式
阅读量:7033 次
发布时间:2019-06-28

本文共 3628 字,大约阅读时间需要 12 分钟。

一、把这些不同来源和目标的数据都统一抽象为数据流。

  Java语言的输入输出功能是十分强大而灵活的。

  在Java类库中,IO部分的内容是很庞大的,因为它涉及的领域很广泛:,文件的操作,网络上的数据流,字符串流,对象流,zip文件流。

  这里介绍几种读写文件的方式

二、InputStream、OutputStream(字节流)

     //读取文件(字节流)        InputStream in = new FileInputStream("d:\\1.txt");        //写入相应的文件        OutputStream out = new FileOutputStream("d:\\2.txt");        //读取数据        //一次性取多少字节        byte[] bytes = new byte[2048];        //接受读取的内容(n就代表的相关数据,只不过是数字的形式)        int n = -1;        //循环取出数据        while ((n = in.read(bytes,0,bytes.length)) != -1) {            //转换成字符串            String str = new String(bytes,0,n,"GBK"); #这里可以实现字节到字符串的转换,比较实用            System.out.println(str);            //写入相关文件            out.write(bytes, 0, n);        }        //关闭流        in.close();        out.close();

三、BufferedInputStream、BufferedOutputStream(缓存字节流)使用方式和字节流差不多,但是效率更高(推荐使用)

            //读取文件(缓存字节流)        BufferedInputStream in = new BufferedInputStream(new FileInputStream("d:\\1.txt"));        //写入相应的文件        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("d:\\2.txt"));        //读取数据        //一次性取多少字节        byte[] bytes = new byte[2048];        //接受读取的内容(n就代表的相关数据,只不过是数字的形式)        int n = -1;        //循环取出数据        while ((n = in.read(bytes,0,bytes.length)) != -1) {            //转换成字符串            String str = new String(bytes,0,n,"GBK");            System.out.println(str);            //写入相关文件            out.write(bytes, 0, n);        }        //清楚缓存        out.flush();        //关闭流        in.close();        out.close();

四、InputStreamReader、OutputStreamWriter(字节流,这种方式不建议使用,不能直接字节长度读写)。使用范围用做字符转换

     //读取文件(字节流)        InputStreamReader in = new InputStreamReader(new FileInputStream("d:\\1.txt"),"GBK");        //写入相应的文件        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("d:\\2.txt"));        //读取数据        //循环取出数据        byte[] bytes = new byte[1024];        int len = -1;        while ((len = in.read()) != -1) {            System.out.println(len);            //写入相关文件            out.write(len);        }        //清楚缓存        out.flush();        //关闭流        in.close();        out.close();

 

五、BufferedReader、BufferedWriter(缓存流,提供readLine方法读取一行文本)

     //读取文件(字符流)        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("d:\\1.txt"),"GBK"));#这里主要是涉及中文        //BufferedReader in = new BufferedReader(new FileReader("d:\\1.txt")));        //写入相应的文件        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d:\\2.txt"),"GBK"));        //BufferedWriter out = new BufferedWriter(new FileWriter("d:\\2.txt"));        //读取数据        //循环取出数据        String str = null;        while ((str = in.readLine()) != null) {            System.out.println(str);            //写入相关文件            out.write(str);            out.newLine();        }                //清楚缓存        out.flush();        //关闭流        in.close();        out.close();

六、Reader、PrintWriter(PrintWriter这个很好用,在写数据的同事可以格式化)

     //读取文件(字节流)        Reader in = new InputStreamReader(new FileInputStream("d:\\1.txt"),"GBK");        //写入相应的文件        PrintWriter out = new PrintWriter(new FileWriter("d:\\2.txt"));        //读取数据        //循环取出数据        byte[] bytes = new byte[1024];        int len = -1;        while ((len = in.read()) != -1) {            System.out.println(len);            //写入相关文件            out.write(len);        }        //清楚缓存        out.flush();        //关闭流        in.close();        out.close();

七、基本的几种用法就这么多,当然每一个读写的使用都是可以分开的。为了更好的来使用io。流里面的读写,建议使用BufferedInputStream、BufferedOutputStream

 

转载于:https://www.cnblogs.com/ll409546297/p/7197911.html

你可能感兴趣的文章
C#默认OrderBy()函数的排序问题
查看>>
17--在rails中使用scss
查看>>
python基本数据类型
查看>>
并行计算结课论文边写边总结2
查看>>
程序访问网络报java.net.ConnectException:/127.0.0.1:8080-Connection refused的解决方法
查看>>
Chrome 扩展插件开发
查看>>
【翻译】在Visual Studio中使用Asp.Net Core MVC创建第一个Web Api应用(二)
查看>>
如何为workflow单据类型定义一个form属性,在notify中通过这个属性打开表单
查看>>
使用jquery-tmpl使JavaScript与HTML分离
查看>>
Prototype 原型模式(转)
查看>>
一次性设置设置所有属性值,当然要保证属性的类型相同
查看>>
vue.js 第三课
查看>>
Grunt针对静态文件的压缩,版本控制打包方案
查看>>
pagination分页
查看>>
orm 扩展
查看>>
类所创建对象个数
查看>>
POJ-2828 Buy Tickets---线段树+逆序
查看>>
js颜色选择器
查看>>
[LeetCode] Count and Say
查看>>
黑马程序员---ADO.NET基础之数据库操作辅助类
查看>>