IO之字符流

1.字符输出流

public abstract class Writer extends Object implement Appendable,Closeabel,Flushable

写入字符流的抽象类。子类必须实现的方法仅有write(char[ ],int,int)、flush()和close()。但是,多数子类将重写此处定义的一些方法,以提供更高效和/或其他功能。

方法名称描述
public void write(String str) throws IOException直接将字符串写入输出
public void write(char[ ] cbuf) throws IOException输出字符数组
public abstract void close( ) throws IOException关闭
public abstract void flush( ) throws IOException刷新

与OutputStream一样 ,对文件的操作使用:

FileWrite类完成,此类的构造方法如下:

方法名称描述
public File Writer(File file) throws IOException根据File类构造FileWriter实例
public FileWriter(File file,boolean append)throws IOException根据File类构造File Writer实例,可以追加内容

2.字符输入流

public abstract class Reader extends Object implements Readable,Closeable

用于读取字符流的抽象类。子类必须实现的方法只有read(char[ ],int,int)和cloase( )。

但是,多数子类将重写此处定义的一些方法,以提供更高的效率和/或其他功能。

方法名称描述
public int read( ) throws IOException读取一个内容
public int read (char[ ] cbuf)throws IOException读取一组内容,返回读入的大小
public abstract  void close() throws IOException关闭

使用FileReader类进行实例化操作,FileReader类中的构造方法定义如下:

方法名称描述
public FileReader(File file)throws FileNotFoundException接收File类的实例


3.字节流与字符流的区别

在所有的流操作里。字节永远是最基础的。任何基于字节的操作都是正确的。无论你是文本文件还是二进制的文件。


如果确定流里面只有可打印的字符,包括英文的和各种国家的文字,也包括中文,那么可以考虑用字符流。


由于编码不同,多字节的字符可能占用多个字节。比如GBK的汉字就占用2个字符,而UTF-8的汉字就占用3个字节。


所以,字符流是根据指定的编码,将1个或多个字节转化为java里面的unicode的字符,然后进行操作。


字符操作一般使用Writer,Reader等,字节操作一般都是InputStream,OutputStream以及各种包装类,比如BufferedInputStream和BufferedOutputStream等


总结:如果你确认你要处理的流是可打印的字符,那么使用字符流会看上去简单一点,如果不确认,那么用字节流中是不会错的。



package com.vince.charstream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

public class ReaderDemo {

	/**
	 * 字符输入流方式一:使用指定大小的字符数组输入
	 */
	public static void reader1(){
		File f = new File("d:\\董学姐.txt");
		try {
			//构造一个字符输入流对象
			Reader in = new FileReader(f);
			char[] cs = new char[20];
			int len = -1;
			StringBuffer sb = new StringBuffer();
			while((len = in.read(cs))!=-1){
				sb.append(new String(cs,0,len));
			}
			//关闭流
			in.close();
			System.out.println(sb);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 使用字节流读取文本文件
	 */
	public static void byteReader(){
		File f = new File("d:\\董学姐.txt");
		try {
			InputStream in = new FileInputStream(f);
			byte[] bytes = new byte[20];
			int len = -1;
			StringBuffer sb = new StringBuffer();
			while((len = in.read(bytes))!=-1){
				sb.append(new String(bytes,0,len));
			}
			in.close();
			System.out.println(sb);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	public static void main(String[] args) {
//		byteReader();
		reader1();
	}

}


package com.vince.charstream;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class WriterDemo {

	
	/**
	 * 字符输出流方式一:以字符数组方式输出
	 */
	public static void writer1(){
		File f = new File("d:\\2.txt");
		try {
			//构造一个字符输出流对象(true表示追加输出)
			Writer out = new FileWriter(f,true);
			
			String info = "good good study,day day up!";
			//向文件中输出
//			out.write(info.toCharArray());
			out.write(info);
			//关闭流
			out.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		writer1();
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值