2021-01-27 字符流

文件分为 文本文件 二进制文件

字符流

一般用于文本文件操作
基类Writer Reader
实现类 FileWriter FileReader
通过 字符串数组 读取
int xxx.read(); 返回字符ascii码
int xxx.read(char[] c); 返回读到的长度

@Test
	public void in() {
		//1.创建文件对象,准备需要读取的文件
		File file = new File("D:"+File.separator+"IO_test"+File.separator+"File"+File.separator+"pet.template");
		//2.准备输入流
		//jdk1.7之后,可以直接放在括号中,结束时自动释放资源
		try (Reader read = new FileReader(file);){
			//3.准备需要读取内容的数组
			char[] c = new char[1];
			//设置读取长度初始值
			int len = 0;
			//4.读取内容
			while((len = read.read(c))!=-1) {
				System.out.println(Arrays.toString(c));
			}
			//5.关闭资源
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

很非主流的一条线

	@Test
	public void copy() {
		try (Reader reader = new FileReader("D:"+File.separator+"IO_test"+File.separator+"File"+File.separator+"hello.txt");
				//true表示将内容拼接到内容的末尾        默认为false  表示直接替换源文件
			 Writer writ = new FileWriter("D:"+File.separator+"IO_test"+File.separator+"File"+File.separator+"test1.txt",true); 
			){
			char[] c = new char[1024];
			int len = 0;  //长度
			while((len=reader.read(c))!=-1) {
				//将读取的内容通过输出流输出到文件
				writ.write(c, 0, len);   //输出的长度为读取内容长度,而非整个数组长度
			}
			writ.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

字符缓冲流

基类Writer Reader
实现类 注意单词拼写有ed
BufferedReader
BuffereddWriter
可以通过 字符数组, 字符串 读取
void close() 关闭流,先刷新。
void flush() 刷新流。
void newLine() 写一行行分隔符。

void write(char[] cbuf, int off, int len)
写入字符数组的一部分。
void write(int c)
写一个字符
void write(String s, int off, int len)
写一个字符串的一部分。

换名称的骚操作

@Test
	public void in() {
		//1.创建文件对象,准备需要读取的文件
		File file = new File("D:"+File.separator+"IO_test"+File.separator+"File"+File.separator+"pet.template");
		//2.准备输入流
		//jdk1.7之后,可以直接放在括号中,结束时自动释放资源
		try (Reader read = new FileReader(file);
			BufferedReader br = new BufferedReader(read);
		){
			//3.准备需要读取内容的数组
			char[] c = new char[1024];
			//设置读取长度初始值
			String str = null;
			//4.读取内容
			while((str = br.readLine())!=null) {
				if(str.indexOf("{name}")>=0) {
					str = str.replace("{name}", "欧欧");
				}
				if(str.indexOf("{type}")!=-1) {
					str = str.replace("{type}", "雪纳瑞犬");
				} 
				if(str.indexOf("{master}")!=-1) {
					str = str.replace("{master}", "波哥");
				}
				System.out.println(str);
			}
			//5.关闭资源
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	@Test
	public void out() {
		Scanner in = new Scanner(System.in);
		File file = new File("D:"+File.separator+"IO"+File.separator+"File"+File.separator+"hello.txt");
		try(Writer writ = new FileWriter(file);
			BufferedWriter bw = new BufferedWriter(writ);
		){
			String c = "";
			do {
				System.out.println("请输入要保存的内容:");
				String str = in.next();
				bw.write(str);  //缓冲流输出
				bw.newLine();	//另起一行
				System.out.println("是否继续?y继续");
				c = in.next();
			}while(c.equals("y"));
			bw.flush();  //清空缓存
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	@Test
	public void copy() {
		try (Reader reader = new FileReader("D:"+File.separator+"IO"+File.separator+"File"+File.separator+"hello.txt");
				//true表示将内容拼接到内容的末尾        默认为false  表示直接替换源文件
			 Writer writ = new FileWriter("D:"+File.separator+"IO"+File.separator+"File"+File.separator+"test1.txt",true); 
			){
			char[] c = new char[1024];
			int len = 0;  //长度
			while((len=reader.read(c))!=-1) {
				//将读取的内容通过输出流输出到文件
				writ.write(c, 0, len);   //输出的长度为读取内容长度,而非整个数组长度
			}
			writ.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值