COMPLEX-B

导航

I/O dempo

标准读取写入

package io_stream;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FilePwd {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		fileIn();
	}
	public static void fileIn() {
//		FileInputStream r = null;
//		FileOutputStream w = null;
		try (BufferedInputStream r = new BufferedInputStream(new FileInputStream("src/file/file02.txt"));
			BufferedOutputStream w =new BufferedOutputStream(new FileOutputStream("src/file/pwd.txt")))
		{
			byte[] bytes = new byte[20];// 定义每次读取字节数量
			int temp;
			while ((temp = r.read()) != -1) {// 判断是否读完
				System.out.println(temp);
				w.write(temp);// 写入文件
                                w.write(temp^77);// 文件加密,解密时异或相同的数字即可
				//System.out.println("写入成功");
			}
			
		}catch (FileNotFoundException e) {
				e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}
	
	}

}

.read()返回的是整数类型,.write()整数类型时,写入对应ascll码的字符,并且只能写入整数类型和char类型
字符流:每次读取一个字符FileReader fr = new FileReader("word.txt");
缓冲字符流:每次读取一行字符,
字符流不能用于非文本文件,如图片
结构图

包装类

  • 包装字节流为字符流
InputStreamReader isr = new InputStreamReader(System.in); // 将标准字节输入流包装成字符输入流, system.in为标准字节输入流
InputStreamReader isr1 = new InputStreamReader(new FileInputStream("a")); // 将标准字节输入流包装成字符输入流
  • 包装普通流为高效缓冲流
    字节:BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("a"));
    字符:BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // 包装成缓冲字符输入流

posted on 2019-04-16 21:11  COMPLEX-B  阅读(155)  评论(0编辑  收藏  举报