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();
}
}
相关文章
- 黑马程序员--Java基础学习之IO流之字节流、字符流、读取写入文件、Copy文件、键盘输入输出、流操作的基本规律
- Java字符流和字节流对文件操作
- Java基础 FileReader-FileWriter / 缓冲字符输入输出流 / 缓冲字节输入输出流 三种方式 进行文本文件的复制
- Java基础教程之字符流文件读写
- Java 字节流实现文件读写操作(InputStream-OutputStream)
- java 对象输入输出流读写文件的操作实例
- 浅谈java字节流和字符流对文件的操作
- 黑马程序员_Java基础_IO流_字符流,带缓冲区的字符流,文本文件读写
- 黑马程序员_Java基础_IO流_字符流,带缓冲区的字符流,文本文件读写
- JAVA 字符流字节流区别以及文件操作代码