java几种读写文件的方式
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几种读写文件的方式的更多相关文章
- java.io几种读写文件的方式
一.Java把这些不同来源和目标的数据都统一抽象为数据流. Java语言的输入输出功能是十分强大而灵活的. 在Java类库中,IO部分的内容是很庞大的,因为它涉及的领域很广泛:标准输入输出,文件的操作 ...
- Java IO如何读写文件
Java把这些不同来源和目标的数据都统一抽象为数据流:Java语言的输入输出功能是十分强大而灵活的:在Java类库中,IO部分的内容是很庞大的,因为它涉及的领域很广泛:标准输入输出,文件的操作,网络上 ...
- python下几种打开文件的方式
昨天看完了这本python进阶,感觉这本书对我启发很大,做了三张纸的笔记,方便我在遇到问题的时候翻阅,然后寻找可能的解决方案.作为一个使用Python一年的小白,虽然说不是小白,但是这一年来基本上是用 ...
- java 获取classpath下文件多种方式
java 获取classpath下文件多种方式 一:properties下配置 在resources下定义server.properties register.jks.path=classpath\: ...
- Java使用RandomAccessFile读写文件
目录 转载自:http://blog.csdn.net/akon_vm/article/details/7429245 Java RandomAccessFile RandomAccessFile是用 ...
- java使用IO读写文件总结
每次用到IO的读写文件都老忘记写法,都要翻过往笔记,今天总结下,省的以后老忘.java读写文件的IO流分两大类,字节流和字符流,基类分别是字符:Reader和Writer:字节:InputStream ...
- java FileReader/FileWriter读写文件
java FileReader/FileWriter读写字母和数字没问题,但读写汉字就乱码.记录下,后面找到解决方法再补上. public static void main(String[] args ...
- Java 字符流读写文件
据说,java读写文件要写很多,贼麻烦,不像c艹,几行代码就搞定.只能抄抄模板拿来用了. 输入输出流分字节流和字符流.先看看字符流的操作,字节转化为字符也可读写. 一.写入文件 1.FileWrite ...
- java使用IO读写文件
https://www.cnblogs.com/qiaoyeye/p/5383723.html java读写文件的IO流分两大类,字节流和字符流,基类分别是字符:Reader和Writer:字节:In ...
随机推荐
- python第二周:数据类型、列表、字典
1.模块初识: sys模块: import sys print(sys.path) #path打印环境变量--> ['F:\\python学习\\projects\\s14\\day2', 'F ...
- C#使用 ComboBox 控件
Combox控件是一个下拉选择的控件,再做上位机的时候会经常用到,这里记录一下我是怎么用. 1.拉出一个combox控件 2.控件属性选为不可编辑,可编辑的话,你选择下拉框的内容后可以改下拉框里的内容 ...
- github发布博客
创建github项目: 名字为:{{你的帐号}}.github.io clone项目,创建并提交推送一个index页面 如: <!DOCTYPE html> <html> & ...
- js 只能输入英文和数字,且首位必须是字母,字母总数不能超过3个,总长度不能超过20!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- 关于springboot整合的详细过程
Spring-boot http://tengj.top/2017/04/24/springboot0/
- Windows-hosts文件地址
C:\Windows\System32\drivers\etc # Copyright (c) - Microsoft Corp. # # This is a sample HOSTS file us ...
- [Beginning SharePoint Designer 2010]列表和库&内部内容类型
本章概要: 1.SPS如何组织管理数据 2.如何创建列表和文档库 3.如何使用视图来过滤分类,分组列表和库 4.如何创建内容类型来应用一个定义好的结构到数据和文档中
- 【leetcode 字符串处理】Compare Version Numbers
[leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...
- Project Euler:Problem 37 Truncatable primes
The number 3797 has an interesting property. Being prime itself, it is possible to continuously remo ...
- Docker私服仓库Harbor安装
Harbor安装那里还是很简单,就是在Docker Login那里掉坑里去了,搞半天,写博客的时候,又重新安装了一遍 1.准备两台服务器 centos7 harbor 10.19.46.15 clie ...