字符流:
 
常识:在java中一个字符等于两个字节;
 
操作字符流的两个类:Writer,Reader
 
API文档介绍(Writer):
public abstract class Writer extends Object implements Appendable, Closeable, Flushable
 
发现此类依然是抽象类,如果使用子类还是需要使用该子类,FileWriter;
 
Writer类的常用方法:
 
关闭该流,但要先刷新它;
public abstract void close() throws IOException
刷新该流缓冲;
public abstract void flush() throws IOException
写入字符数组
public void write(char[] cbuf) throws IOException
写入字符数组的一部分
public abstract void write(char[] cbuf,int off,int len)throws IOException
写入字符串
public void write(String str) throws IOException
写入字符串的一部分
public void write(String str,int off,int len)throws IOException
 
FileWriter常用构造方法:
 

根据给定的 File 对象构造一个 FileWriter 对象

public FileWriter(File file)throws IOException

根据给定的文件名构造一个 FileWriter 对象

public FileWriter(String fileName)throws IOException
 
实例01:

package cn.itcast04;

import java.io.FileWriter;
import java.io.IOException; public class FileWriterDemo01 {
public static void main(String[] args) throws IOException { //创建IO对象
FileWriter fw = new FileWriter("d.txt" ); String s = "I love JAVA";
//写入字符串
fw.write( "Hello World"); //写入字符串的一部分
fw.write(s, 1,5); //写入字符数组
char[] chars = new char[]{ 'a', 'c', 'b', 'e'};
fw.write(chars); //写入字符数组的一部分
fw.write(chars,0,3); fw.close();
}
}

  

字符输入流(Reader)
 
API文档介绍(Reader):
public abstract class Reader extends Object implements Readable, Closeable
 
FileReader类常用方法:
 
构造方法:
 
根据给定的 File 对象构造一个 FileWriter 对象
public FileReader(File file)throws IOException
根据给定的文件名构造一个 FileWriter 对象
public FileReader(String fileName)throws IOException
 
常用方法:
 
读取单个字符:
public int read()throws IOException
将字符读入数组
public int read(char[] cbuf)throws IOException
读取数组中的一部分
public abstract int read(char[] cbuf,int off,int len)throws IOException
实例02:
package cn.itcast04; import java.io.File;
import java.io.FileReader;
import java.io.IOException; public class FileReaderDemo01 {
public static void main(String[] args) throws IOException {
File file= new File("d.txt" );
FileReader fr = new FileReader(file); //读取单个字符
int c;
while((c=fr.read())!=-1)
{
System. out.println((char)c);
}
fr.close();
System. out.println("================================" );
File file2= new File("d.txt" );
FileReader fr2 = new FileReader(file2);
//读入一个数组
char[] chars = new char[( int)file2.length()];
int len;
while((len=fr2.read(chars))!=-1)
{
String s = new String(chars,0,len);
System. out.println(s);
}
fr2.close(); }
}

  

JavaIO(04)字符流--Writer and Reader的更多相关文章

  1. Java IO(十二) 字符流 Writer 和 Reader

    Java IO(十二) 字符流 Reader和 Writer 一.介绍 涉及到文件(如果是纯文本文件形式)操作时,Java除了提供 FIle(文件和目录路径名的抽象表示形式) 和 FileDescri ...

  2. [Java] 字符流 Writer,输出字符数据PrintWriter

    package test.stream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.Fi ...

  3. javaIO流--Writer,Reader

    Writer /** *<li> Writer中定义的一个重要的方法: * public void writer(String str)throws IOException; */ pac ...

  4. [19/03/31-星期日] IO技术_四大抽象类_字符流( 字符输入流 Reader、 字符输出流 Writer )(含字符缓冲类)

     一.概念 Reader Reader用于读取的字符流抽象类,数据单位为字符. int read(): 读取一个字符的数据,并将字符的值作为int类型返回(0-65535之间的一个值,即Unicode ...

  5. Java基础知识强化之IO流笔记34:OutputStreamWriter(Writer字符流的子类)5种write数据方式

    1. OutputStreamWriter (转换流) OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的 charset 将要写入流中的字符编码成字节. 同时OutputS ...

  6. IO(二)----字符流

    计算机并不区分二进制文件与文本文件.所有的文件都是以二进制形式来存储的,因此,从本质上说,所有的文件都是二进制文件.所以字符流是建立在字节流之上的,它能够提供字符层次的编码和解码. 常见的码表 ASC ...

  7. Java IO5:字符流

    字符流 字节流提供了处理任何类型输入/输出操作的功能(因为对于计算机而言,一切都是0和1,只需把数据以字节形式表示就够了),但它们不可以直接操作Unicode字符,因为上一篇文章写了,一个Unicod ...

  8. Java IO3:字符流

    字符流 字节流提供了处理任何类型输入/输出操作的功能(对于计算机而言,一切都是0 和1,只需把数据以字节形式表示就够了),但它们不可以直接操作Unicode字符,一个Unicode字符占用2个字节,而 ...

  9. Java IO 转换流 字节转字符流

    Java IO 转换流 字节转字符流 @author ixenos 字节流 输入字节流:---------| InputStream 所有输入字节流的基类. 抽象类.------------| Fil ...

随机推荐

  1. 网站常见问题及解决方法(div/css)

    18.<a> 在IE6,7 下面重新定义宽和高的代码:{  display:block; display:-moz-inline-stack; display:inline-block;  ...

  2. windows 下 apache设置

    apache,apache配置,配置端口 mac下apache配置 添加虚拟主机: Alias /selftest/ "D:/self-test/" <Directory & ...

  3. Git for windows 中文乱码解决方案

    1.git status时显示乱码,如下: \316\304\261\276\316\304\265\265.txt 解决方案: $ git config --global core.quotepat ...

  4. Eclipse中设置在创建新类时自动生成注释

    方法一:Eclipse中设置在创建新类时自动生成注释 windows-->preference Java-->Code Style-->Code Templates code--&g ...

  5. URAL 1992

    CVS Description Yoda: Visit I will the cloners on Kamino... And see this army they have created for ...

  6. EF4.0和EF5.0增删改查的写法区别及执行Sql的方法

    EF4.0和EF5.0增删改查的写法区别 public T AddEntity(T entity) { //EF4.0的写法 添加实体 //db.CreateObjectSet<T>(). ...

  7. MongoDB入门分享-笔记整理精选

    最近在学习MongoDB,怕以后忘记,自己做了一个整理,给不知道的小伙伴一起分享学习一下. 第一步> 首先到官网下载,安装MongoDB.(注意MongoDB还有一个可视化管理工具叫: Mong ...

  8. Java [Leetcode 205]Isomorphic Strings

    题目描述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...

  9. squid+nginx+apache

    一.前言 二.编译安装 三.安装MySQL.memcache 四.安装Apache.PHP.eAccelerator.php-memcache 五.安装Squid 六.后记 一.前言,准备工作当前,L ...

  10. MySQL基础之第10章 查询数据

    10.1.基本查询语句 SELECT 属性列表 FROM 表名和视图列表[WHERE条件表达式1][GROUPBY 属性名1 [HAVING条件表达式2]][ORDERBY 属性名2[ASC|DESC ...