java 字符流实现文件读写操作(FileReader-FileWriter)
2016-07-01 10:59
405 查看
java 字符流实现文件读写操作(FileReader-FileWriter)
备注:字符流效率高,但是没有字节流底层
字节流地址:http://pengyan5945.iteye.com/blog/1092120
Java代码 收藏代码
package com.frank.io;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
/**
* author:pengyan
* date:Jun 15, 2011
* file:WriterReaderTest.java
*/
public class WriterReaderTest {
File f=new File("E:\\abc.txt");
public static void main(String[] args) throws IOException{
WriterReaderTest test=new WriterReaderTest();
test.writeFile("Java字符流读写文件测试!");
test.readFile();
}
private void readFile() throws IOException{
//reate BufferedReader with file
Reader r=new BufferedReader(new FileReader(f));
//in order to receive the value of this stream read every time
int temp=0;
//the all content of this stream read
String str="";
while ((temp=r.read())!=-1) {
//if not end,the total content add the value of the stream read this time
str+=(char)temp;
}
//show the content of the file
System.out.println("文件内容:"+str);
}
private void writeFile(String content) throws IOException {
if (f.exists()==false) {
f.createNewFile();//create file if not exist
}
//create FileWriter with file
Writer w=new FileWriter(f);
//write file
w.write(content);
//flush this stream
w.flush();
//close this stream
w.close();
}
}
备注:字符流效率高,但是没有字节流底层
字节流地址:http://pengyan5945.iteye.com/blog/1092120
Java代码 收藏代码
package com.frank.io;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
/**
* author:pengyan
* date:Jun 15, 2011
* file:WriterReaderTest.java
*/
public class WriterReaderTest {
File f=new File("E:\\abc.txt");
public static void main(String[] args) throws IOException{
WriterReaderTest test=new WriterReaderTest();
test.writeFile("Java字符流读写文件测试!");
test.readFile();
}
private void readFile() throws IOException{
//reate BufferedReader with file
Reader r=new BufferedReader(new FileReader(f));
//in order to receive the value of this stream read every time
int temp=0;
//the all content of this stream read
String str="";
while ((temp=r.read())!=-1) {
//if not end,the total content add the value of the stream read this time
str+=(char)temp;
}
//show the content of the file
System.out.println("文件内容:"+str);
}
private void writeFile(String content) throws IOException {
if (f.exists()==false) {
f.createNewFile();//create file if not exist
}
//create FileWriter with file
Writer w=new FileWriter(f);
//write file
w.write(content);
//flush this stream
w.flush();
//close this stream
w.close();
}
}
相关文章推荐
- JAVA JDK CGLIB 动态代理 比较
- Eclipse配置相关
- Java基本数据类型
- selenium定位方法(java实例)(二)
- Ajax SpringMvc 实现登录请求
- 介绍 Spring IoC 容器和 bean
- springMVC学习--RESTful支持
- Java基础加密之BASE64加解密
- 复习spring碰到的一些小知识
- spring boot 数据库连接池配置
- java多线程--“升级版”生产者消费者
- java调用WebService(客户端)
- springmvc学习笔记——ModelAttribute
- Spring MVC POM
- 深入浅出RxJava(一:基础篇)
- Spring的实例工厂方法和静态工厂方法
- eclipse jee中检出项目并转换为Maven project
- Java SE-运算符
- 位运算的常用操作总结
- java 反射方法收集