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编码的字符流,字符的单位一般比字节大,字节可以处理任何数据类型,通常在处理文本文件内容时,字 ...
随机推荐
- Linux - wc统计文件行数、单词数或字节数
一 wc简单介绍 wc命令用来打印文件的文本行数.单词数.字节数等(print the number of newlines, words, and bytes in files).在Windows的 ...
- 表ADT
表一般不用简单数组来实现,通常将其实现为链表.在链表中要不要使用表头则属于个人兴趣问题.在下面的例程中我们都使用表头. 按照C的约定,作为类型的List(表)和Position(位置)以及函数的原型都 ...
- spark1.2.0编译
spark 有三种编译方式:SBT.MAVEN.make-distribution.sh.SBT.MAVEN两种方式打出来的包比较大,不适合部署使用.因此我们通常使用第三种方式打包. ./make-d ...
- [Java] JAVA程序员您需要学习的25个标准
(1) 你需要精通面向对象分析与设计(OOA/OOD).涉及模式(GOF,J2EEDP)以及综合模式.你应该了解UML,尤其是class,object,interaction以 及statediagr ...
- Laravel自学第一课:laravel下载与安装
本地安装laravel,php环境要配置好,推荐xmapp一键搭建. 1.程序包直接从官方下载,官方开源地址:https://github.com/laravel/laravel(当然也可从此网站:h ...
- 6. Android框架和工具之 JSON解析
Android进阶笔记17:3种JSON解析工具(org.json.fastjson.gson)
- 在C#中使用NPOI2.0操作Excel2003和Excel2007
Excel2003: #region Excel2003 /// <summary> /// 将Excel文件中的数据读出到DataTable中(xls) /// </summary ...
- leetcode 题解Merge Two Sorted Lists(有序链表归并)
题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...
- Android 珍藏(二)
一.如何控制Android LED等?(设置NotificationManager的一些参数) 代码如下: final int ID_LED=19871103; NotificationManage ...
- Ngnix 安装、信号量、虚拟主机配置
ngnix的安装很简单 1.先从ngnix官网下载ngnix压缩包 wget http://nginx.org/download/nginx-1.6.2.tar.gz 2.解压并进入其目录 tar - ...