Java 字符流实现文件读写操作(FileReader-FileWriter)
Java 字符流实现文件读写操作(FileReader-FileWriter)
备注:字符流效率高,但是没有字节流底层
字节流地址:http://pengyan5945.iteye.com/blog/1092120
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 字符流实现文件读写操作(FileReader-FileWriter)的更多相关文章
- java 21 - 4 字符流的文件复制操作以及简化
既然字节流可以复制文件,那么字符流当然也有. 同样的思路: 数据源: a.txt -- 读取数据 -- 字符转换流 -- InputStreamReader目的地: b.txt -- 写出数据 -- ...
- java字符流实现文件间的内容复制
package com.io.demo1; import java.io.FileReader; import java.io.FileWriter; public class TestFileSTr ...
- java中的File文件读写操作
之前有好几次碰到文件操作方面的问题,大都由于时间太赶而没有好好花时间去细致的研究研究.每次都是在百度或者博客或者论坛里面參照着大牛们写的步骤照搬过来,之后再次碰到又忘记了.刚好今天比較清闲.于是就在网 ...
- io流对文件读写操作
public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedRead ...
- java字符流读取文件
package ba; import java.io.*; public class zifuTest { public static void main(String[] args) { FileI ...
- JAVA基于缓冲的文件读写操作
File f2 = new File("e://index.java"); BufferedReader reader = new BufferedReader(new Input ...
- Java字符流和字节流对文件操作
记得当初自己刚开始学习Java的时候,对Java的IO流这一块特别不明白,所以写了这篇随笔希望能对刚开始学习Java的人有所帮助,也方便以后自己查询.Java的IO流分为字符流(Reader,Writ ...
- Java 字符流文件读写
上篇文章,我们介绍了 Java 的文件字节流框架中的相关内容,而我们本篇文章将着重于文件字符流的相关内容. 首先需要明确一点的是,字节流处理文件的时候是基于字节的,而字符流处理文件则是基于一个个字符为 ...
- Java IO之字符流和文件
前面的博文介绍了字节流,那字符流又是什么流?从字面意思上看,字节流是面向字节的流,字符流是针对unicode编码的字符流,字符的单位一般比字节大,字节可以处理任何数据类型,通常在处理文本文件内容时,字 ...
随机推荐
- WCF摘记
//绑定形式 NetTcpBinding bind = new NetTcpBinding(); //地址 EndpointAddress address = new EndpointAddress( ...
- VS2012无法安装cocos2d-x-2.1.4 解决方法及VS2012新建coco2d-x项目(一)
转自:http://www.cnblogs.com/wangpei/admin/EditPosts.aspx?opt=1 (注:此方法是可行,仅供参考,建议大家直接看我的 一见命令解决vs安装并创建c ...
- js中获取函数的参数
刘海波 2015/2/11 9:11:43 tmp = tmp.replace(/\{(\d+)\}/g, function($1, $2) {return addrow.arguments[pars ...
- Redis 分区
分区是分割数据到多个Redis实例的处理过程,因此每个实例只保存key的一个子集. 分区的优势 通过利用多台计算机内存的和值,允许我们构造更大的数据库. 通过多核和多台计算机,允许我们扩展计算能力:通 ...
- 遍历List remove方法,雨露均沾
/** * 要求:去掉List中为 0 的元素 */ //创建数组和空List Integer[] ars = {1,0,0,0,5,0,8,9,0,0,0,65,3,0,0}; List<In ...
- flume+kafka+storm
centos06.6+JDK1.7 flume1.4+kafka2.10+storm0.9.3 zookeeper3.4.6 集群: 192.168.80.133 x01 192.168.80.134 ...
- C# 自定义重绘DataGridView
using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using Syste ...
- IOS UIButton 自定义的补充学习
一直自定按钮 遇到两个做的不好的地方. 1 是按钮响应方法时候状态自定义不好看 按压感不明显 2 是button上的title 是我自己用label写上去的 而不是用button的属性 这两天终于运用 ...
- Tested work with China Digiprog 3 4.94 mileage programmer
I was thinking about buying a Digiprog3 clone from China I know that YANHUA Digiprog 3 is the best a ...
- seajs 源码解读
之前面试时老问一个问题seajs 是怎么加载js 文件的 在网上找一些资料,觉得这个写的不错就转载了,记录一下,也学习一下 seajs 源码解读 seajs 简单介绍 seajs是前端应用模块化开发的 ...