java.io的几种读写文件的方式

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

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

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

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

二、InputStream、OutputStream(字节流)

//读取文件(字节流)
FileInputStream in = new FileInputStream("d:\\1.txt");
//写入相应的文件
FileOutputStream 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,"UTF-8"); #这里可以实现字节到字符串的转换,比较实用
System.out.println(str);
//写入相关文件
out.write(bytes, 0, n);
//清除缓存向文件写入数据
out.flush();
}
//关闭流
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,"UTF-8");
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"),"UTF-8");
//写入相应的文件
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("d:\\2.txt"));
//读取数据
//循环取出数据
char[] chars = new char[2048];
int len = -1;
while ((len = in.read(chars,0,chars.length)) != -1) {
System.out.println(len);
//写入相关文件
out.write(chars,0,len);
//清除缓存
out.flush();
}
//关闭流
in.close();
out.close();

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

FileInputStream fileInputStream = new FileInputStream("d:\\1.txt");
FileOutputStream fileOutputStream = new FileOutputStream("d:\\2.txt", true);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"UTF-8");
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream,"UTF-8");
//读取文件(字符流)
BufferedReader in = new BufferedReader(inputStreamReader,"UTF-8"));#这里主要是涉及中文
//BufferedReader in = new BufferedReader(new FileReader("d:\\1.txt")));
//写入相应的文件
BufferedWriter out = new BufferedWriter(outputStreamWriter,"UTF-8"));
//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"),"UTF-8");
//写入相应的文件
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

java几种读写文件的方式的更多相关文章

  1. java.io几种读写文件的方式

    一.Java把这些不同来源和目标的数据都统一抽象为数据流. Java语言的输入输出功能是十分强大而灵活的. 在Java类库中,IO部分的内容是很庞大的,因为它涉及的领域很广泛:标准输入输出,文件的操作 ...

  2. Java IO如何读写文件

    Java把这些不同来源和目标的数据都统一抽象为数据流:Java语言的输入输出功能是十分强大而灵活的:在Java类库中,IO部分的内容是很庞大的,因为它涉及的领域很广泛:标准输入输出,文件的操作,网络上 ...

  3. python下几种打开文件的方式

    昨天看完了这本python进阶,感觉这本书对我启发很大,做了三张纸的笔记,方便我在遇到问题的时候翻阅,然后寻找可能的解决方案.作为一个使用Python一年的小白,虽然说不是小白,但是这一年来基本上是用 ...

  4. java 获取classpath下文件多种方式

    java 获取classpath下文件多种方式 一:properties下配置 在resources下定义server.properties register.jks.path=classpath\: ...

  5. Java使用RandomAccessFile读写文件

    目录 转载自:http://blog.csdn.net/akon_vm/article/details/7429245 Java RandomAccessFile RandomAccessFile是用 ...

  6. java使用IO读写文件总结

    每次用到IO的读写文件都老忘记写法,都要翻过往笔记,今天总结下,省的以后老忘.java读写文件的IO流分两大类,字节流和字符流,基类分别是字符:Reader和Writer:字节:InputStream ...

  7. java FileReader/FileWriter读写文件

    java FileReader/FileWriter读写字母和数字没问题,但读写汉字就乱码.记录下,后面找到解决方法再补上. public static void main(String[] args ...

  8. Java 字符流读写文件

    据说,java读写文件要写很多,贼麻烦,不像c艹,几行代码就搞定.只能抄抄模板拿来用了. 输入输出流分字节流和字符流.先看看字符流的操作,字节转化为字符也可读写. 一.写入文件 1.FileWrite ...

  9. java使用IO读写文件

    https://www.cnblogs.com/qiaoyeye/p/5383723.html java读写文件的IO流分两大类,字节流和字符流,基类分别是字符:Reader和Writer:字节:In ...

随机推荐

  1. static类型autowired 注入失败

    原代码:注入commonService对象失败 @Autowired private static CommonService commonService; public static List< ...

  2. etymology-R

    1)vor = to eat devour vt. 狼吞虎咽地吃光: 吞没,毁灭: 目不转睛地看[de-向下+vour-吃] voracity  n.贪食,贪婪.拉丁词根vor-,vorac-表示吞食 ...

  3. HDU 5167

    范围 内的斐波那契的数不多,求出范围内的数,再暴力枚举即可. #include <iostream> #include <cstdio> #include <algori ...

  4. Android 自己定义View学习(2)

    上一篇学习了基本使用方法,今天学一下略微复杂一点的.先看一下效果图 为了完毕上面的效果还是要用到上一期开头的四步 1,属性应该要有颜色,要有速度 <?xml version="1.0& ...

  5. 【云快讯】之四十八《IBM和Cisco最新收购,加强Openstack易用能力》

    2015-06-08 张晓东 东方云洞察 点击上面的链接文字,能够高速关注"东方云洞察"公众号 本周宣布的两起收购引人注意.思科购买Piston云计算公司.同期IBM的收购Blue ...

  6. POJ--1087--A Plug for UNIX【Dinic】网络最大流

    链接:http://poj.org/problem? id=1087 题意:提供n种插座.每种插座仅仅有一个,有m个设备须要使用插座,告诉你设备名称以及使用的插座类型,有k种转换器.能够把某种插座类型 ...

  7. uva是崩了 吗,还是我太年轻?

    刚刚提交了一道题,发现提交状态一直是in judge queue,去提交状态那里看了下,排在我20分钟前的也在in judge queue,不知道前面还有多少.顿时感到好无力......

  8. HDU 2054 A==B? 大数

    Problem Description Give you two numbers A and B, if A is equal to B, you should print "YES&quo ...

  9. cmd 进入mysql 小技巧

    1.開始中找出执行:输入cmd 2.查找appserv所在盘,我的在D盘.所以接着输入:d: 3.在d盘中查找mysql所在文件夹:cd appserv\mysql\bin 4.再输入主机名.数据库名 ...

  10. C# 用ManulResetEvent 控制Thread的 Suspend、Resume

    class Program { static void Main(string[] args) { Thread thread = new Thread(Work); thread.Start(); ...