Java中的字节流与字符流概念整理

1.InputStream和Reader

    InputStream和Reader是所有输入流的的抽象基类,本身并不能创建实例来执行输入,但它们将成为所有输入流的模板,它们的方法是所有输入流都可使用的方法。

    InputStream中的三个方法:

        int read():从输入流中读取单个字节,返回其所读取的字节数据(字节数据可直接转换为int类型)。

        int read(byte[]  b):从输入流中最多读取b.length个字节的数据,并将其存储在字节数据b中,返回实际读取的字节数。

        int read(byte[] b,int off,int len):从输入流中最多读取len个字节的数据,并将其存储在字节数组b中,放入数组b中时,并不是从数组起点开始,而是从off位置开始,返回实际读取的字节数。

    Reader中的三个方法:

        int read():从输入流中读取单个字符,返回所读取的字符数据(字符数据可直接转换为int类型)。

        int read(char[] cbuf):从输入流中最多读取cbuf.length个字符的数据,并将其存储在字符数组cbuf中,返回实际读取的字符数。

        int read(char[] cbuf,int off,int len):从输入流中最多读取len个字符的数据,并将其存储在字符数组cbuf中,放入数组cbuf中时,并不是从数组起点开始,而是从off位置开始,返回实际读取的字符数。

    当read(char[] cbuf)或read(byte[] b)方法返回-1,即表明到了输入流的结束点。

    InputStream和Reader分别有一个用于读取文件的输入流:FileInputStream和FileReader,它们都是节点流——会直接和指定文件关联。

public class FileInputStreamTest {
	public static void main(String[] args) throws IOException {
		FileInputStream fis=new FileInputStream("F:\\a.txt");
		byte[] bbuf=new byte[1024];
		int hasRead=0;
		while((hasRead=fis.read(bbuf))>0){
			System.out.println(new String(bbuf,0,hasRead));
		}
		fis.close();
	}
}
public class FileReaderTest {
	@SuppressWarnings("resource")
	public static void main(String[] args) throws IOException {
		try {
			FileReader fr=new FileReader("F:\\a.txt");
			char[] cbuf=new char[32];
			int hasRead=0;
			while((hasRead=fr.read(cbuf))>0){
				System.out.println(new String(cbuf,0,hasRead));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

2.OutputStream和Writer

    void write(int c):将指定字节/字符输出到输出流中,其中C既可以代表字节,也可以代表字符。

    void write(byte[]/char[] buf):将字节数组/字符数组中的数据输出到指定输出流中。

    void write(byte[]/char[] buf,int off,int len):将字节数组/字符数组从off位置开始,长度为len的字节/字符输出到输出流中。

    因为字符流直接以字符作为操作单位,所以Writer可以用字符串来代替字符数组,即以String对象作为参数。Writer中还有以下两个方法:

    void write(String str):将str字符串里包含的字符输出到指定输出流中。

    void wirte(String str,int off,int len):将str字符串中从off位置开始,长度为len的字符输出到指定输出流中。

        @SuppressWarnings("resource")
	public static void main(String[] args) throws IOException {
		try {
			FileInputStream fis=new FileInputStream("F:\\a.txt");
			FileOutputStream fos=new FileOutputStream("F:\\a_copy.txt");
			byte[] bbuf=new byte[1024];
			int hasRead=0;
			while((hasRead=fis.read(bbuf))>0){
				fos.write(bbuf,0,hasRead);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
@SuppressWarnings("resource")
	public static void main(String[] args) {
		try {
			FileWriter fw=new FileWriter("F:\\poem.txt");
			fw.write("锦瑟 - 李商隐");
			fw.write("锦瑟无端五十弦,一弦一柱思华年。\r\n");
			fw.write("庄生晓梦迷蝴蝶,望帝春心托杜鹃。\r\n");
			fw.write("沧海月明珠有泪,蓝田日暖玉生烟。\r\n");
			fw.write("此情可待成追忆,只是当时已惘然。\r\n");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}




  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值