字符流:
 
常识:在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. BZOJ 2154 Crash的数字表格

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2154 题意: 思路: i64 mou[N]; void init(int N){    ...

  2. libmysqlclient.so.15: cannot open shared object file: No such file or directory

    错误: ./mafsInRegion: error while loading shared libraries: libmysqlclient.so.15: cannot open shared o ...

  3. sscanf() 和 sprintf()的用法。

    因为感觉比较有用. 这几次比赛,用过几次,所以写个程序,总结一下. 如果用sscanf(s, "%d.%d", &a, &b); 的时候,一定要注意是否s里一定有小 ...

  4. poj 1195 Mobile phones(二维树状数组)

    树状数组支持两种操作: Add(x, d)操作:   让a[x]增加d. Query(L,R): 计算 a[L]+a[L+1]……a[R]. 当要频繁的对数组元素进行修改,同时又要频繁的查询数组内任一 ...

  5. Android中LayoutInflater的使用

    Inflater英文意思是膨胀,在Android中应该是扩展的意思吧. LayoutInflater 的作用类似于 findViewById(),不同点是LayoutInflater是用来找layou ...

  6. 函数buf_pool_get

    根据space ,offset 获取 buff pool的实例 下标 /**************************************************************** ...

  7. Qt之进程间通信(共享内存)

    简述 上一节中,我们分享下如何利用Windows消息机制来进行不同进程间的通信.但是有很多局限性,比如:不能跨平台,而且必须两个进程同时存在才可以,要么进程A发了消息谁接收呢? 下面我们来分享另外一种 ...

  8. GridView CommandArgument 绑定多个参数

    我们在使用GridView的时候 有时会需要绑定多个参数 <asp:GridView ID="gvwVoxListAll" runat="server"  ...

  9. LeetCode Binary Tree Preorder Traversal 先根遍历

    题意:给一棵树,求其先根遍历的结果. 思路: (1)深搜法: /** * Definition for a binary tree node. * struct TreeNode { * int va ...

  10. VirtualBox clonevdi文件和修改vdi的uuid

    因为VirtualBox下,不允许有相同的uuid,所以要拷贝一份新的vdi像普通的拷贝是办不到的.需要用VirtualBox自带的一个.exe文件VBoxManage. 1.首先,进入终端或者是命令 ...