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)的更多相关文章

  1. java 21 - 4 字符流的文件复制操作以及简化

    既然字节流可以复制文件,那么字符流当然也有. 同样的思路: 数据源: a.txt -- 读取数据 -- 字符转换流 -- InputStreamReader目的地: b.txt -- 写出数据 -- ...

  2. java字符流实现文件间的内容复制

    package com.io.demo1; import java.io.FileReader; import java.io.FileWriter; public class TestFileSTr ...

  3. java中的File文件读写操作

    之前有好几次碰到文件操作方面的问题,大都由于时间太赶而没有好好花时间去细致的研究研究.每次都是在百度或者博客或者论坛里面參照着大牛们写的步骤照搬过来,之后再次碰到又忘记了.刚好今天比較清闲.于是就在网 ...

  4. io流对文件读写操作

    public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedRead ...

  5. java字符流读取文件

    package ba; import java.io.*; public class zifuTest { public static void main(String[] args) { FileI ...

  6. JAVA基于缓冲的文件读写操作

    File f2 = new File("e://index.java"); BufferedReader reader = new BufferedReader(new Input ...

  7. Java字符流和字节流对文件操作

    记得当初自己刚开始学习Java的时候,对Java的IO流这一块特别不明白,所以写了这篇随笔希望能对刚开始学习Java的人有所帮助,也方便以后自己查询.Java的IO流分为字符流(Reader,Writ ...

  8. Java 字符流文件读写

    上篇文章,我们介绍了 Java 的文件字节流框架中的相关内容,而我们本篇文章将着重于文件字符流的相关内容. 首先需要明确一点的是,字节流处理文件的时候是基于字节的,而字符流处理文件则是基于一个个字符为 ...

  9. Java IO之字符流和文件

    前面的博文介绍了字节流,那字符流又是什么流?从字面意思上看,字节流是面向字节的流,字符流是针对unicode编码的字符流,字符的单位一般比字节大,字节可以处理任何数据类型,通常在处理文本文件内容时,字 ...

随机推荐

  1. java 验证码图片处理类,为验证码识别做准备

    /* * To change this template, choose Tools | Templates * and open the template in the editor. */pack ...

  2. javascript异步延时载入及推断是否已载入js/css文件

    <html> <head> <script type="text/javascript"> /**======================= ...

  3. 龙书(Dragon book) +鲸书(Whale book)+虎书(Tiger book)

    1.龙书(Dragon book)书名是Compilers: Principles,Techniques,and Tools作者是:Alfred V.Aho,Ravi Sethi,Jeffrey D. ...

  4. Golang学习 - unicode 包

    ------------------------------------------------------------ const ( MaxRune = '\U0010FFFF' // Unico ...

  5. PHP Fuzzing行动——源码审计

    目录: Section 1: 20种PHP源码快速审计方式  Section 2: PHP源码审计自动化( PHP Fuzzer )  风险级别: ■ Low ■ Medium ■ High   在开 ...

  6. android 布局之滑动探究 scrollTo 和 scrollBy 方法使用说明

    涉及到滑动,就涉及到VIEW,大家都知道,Android的UI界面都是由一个一个的View以及View的派生类组成,View作为基类,而常用的布局里面的各种布局就是它派生出来的ViewGroup的子类 ...

  7. GOF设计模式之1:单例设计模式

    1.单例设计模式核心作用: 保证一个类只有一个实例,并且提供了访问该实例的全局访问点 2.常见应用场景: window的任务管理器 项目中读取配置文件一般也是一个单例模式 数据库连接池的设计也是采用单 ...

  8. Amazon Launches FBA Export to Expand Beyond Media Categories

    Amazon launched a new program called FBA Export for third-party sellers to help them export products ...

  9. 10. Android框架和工具之 AppMsg(消息提示)

    1. AppMsg 优雅的弹出类似Toast的消息提示,支持3种状态Alert(警告),Confirm(确认)以及Info(消息).        2. AppMsg使用: (1)AppMsg下载地址 ...

  10. create feature from text file

    '''---------------------------------------------------------------------------------- Tool Name: Cre ...