Java基础突击第八天0020(字符流)

21 篇文章 0 订阅

一个字符相当于两个字节。

-------->Writer

字符输出流Writer

public abstract class Writer extends Object implements Appendable,Closeable,Flushable

毕竟是抽象类,需要实现它的子类,比如FileWriter

public FileWriter(File file) throws IOException

public FileWriter(File file,boolean appendable) throws IOException

Writer类的方法

public abstract void close() throws IOException 

public void write(String str) throws IOException

public void write(char[] buf) throws IOException

public abstract void flush() throws IOException

Writer与OutputStream不同,可以直接输出字符串,不用将字符串变为byte数组在输出。

import java.io.File;
import java.io.Writer;
import java.io.FileWriter;
public class TestJava{
}
class Demo{
		public static void main(String[] args) throws Exception{
				File fileW = new File("C:"+File.separator+"JavaStudy"+File.separator
								+"JavaIO"+File.separator+"test04.txt");
			  Writer rSwitch = new FileWriter(fileW);
				String strTemp01 = "Hello World!";
				rSwitch.write(strTemp01);
				rSwitch.close();
				Writer rSwitchAppend = new FileWriter(fileW,true);
				String strTemp02 = "\r\nAppen!\r\nHello World!";
				rSwitchAppend.write(strTemp02);
				rSwitchAppend.close();
		}
}//Demo

output:

在test04.txt中存入文字。并追加文字。

-------->Reader

public abstract class Reader extends Object implements Readable,Closeable

FileReader

public FileReader(File file) throws FileNotFoundException

Reader常用方法

public abstract void close() throws IOException

public int read() throws IOException    //读取单个字符

public int read(char[] cbuf) throws IOException  //读取字符数组并返回长度

在知道数组长度时,直接输出所有数据在字符数组,并用read(字符数组)读取,同时返回数组长度

import java.io.File;
import java.io.Reader;
import java.io.FileReader;
public class TestJava{
}
class Demo{
		public static void main(String[] args) throws Exception{
				File fileR = new File("C:"+File.separator+"JavaStudy"+File.separator+
						"JavaIO"+File.separator+"test04.txt");
				Reader rSwitch = new FileReader(fileR);
				char[] cReady = new char[1024];//put all data to char array
				int lencReady = rSwitch.read(cReady);
				rSwitch.close();
				System.out.println("Content:\n"+new String(cReady,0,lencReady));

				//if you don't know the length of the array you can print the letter
				//one by one until (the end == -1)
		}
}//Demo

output:

Content:
Hello World!
Appen!

Hello World!

不知道数组长度,可循环输出,InputStream和Reader的int read()一个数字一个数字的读取,再(char)显示转换。

import java.io.File;
import java.io.Reader;
import java.io.FileReader;
public class TestJava{
}
class Demo{
		public static void main(String[] args) throws Exception{
				File fileR = new File("C:"+File.separator+"JavaStudy"+File.separator+
						"JavaIO"+File.separator+"test04.txt");
				Reader rSwitch = new FileReader(fileR);
				int lencReady =0 ;
				char[] cReady= new char[1024];
				int tempRec = 0;
				while((tempRec = rSwitch.read())!=-1){
						cReady[lencReady] = (char)tempRec;
						lencReady++;
				}
				rSwitch.close();
				System.out.println("Content:\n"+new String(cReady,0,lencReady));
		}
}//Demo

output:

Content:
Hello World!
Appen!

Hello World!

字符流和字节流的区别:

字节流不会用到缓冲区(内存),是文件本身直接操作的。字符流用到了缓冲区。

不关闭字节流操作,文件中也能依然输出内容,证明字节流是直接操作文件本身。

字符流不关闭,缓冲区内容无法输出。强行输出缓冲区的数据可以用flush数据(rSwitch.flush())。

字节流还是字符流好?

字节流。所有的文件在硬盘中都是字节方式存储。字符是只有在内存中才形成。所以字节流较为广泛。

使用Java字节流完成文件复制:

import java.io.File;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;

public class TestJava{
}
class Demo{
		public static void main(String[] args){
				if(args.length!=2){
						System.out.println("Input Error!");
						System.exit(1);
				}
				File fS = new File(args[0]);
				File fT = new File(args[1]);
				if(!fS.exists()){
						System.out.println("Source file doesn't exist.");
						System.exit(1);
				}
				InputStream iF = null;
				OutputStream oF = null;
				try{
						iF = new FileInputStream(fS);
				}catch(FileNotFoundException e){
						e.printStackTrace();
				}
				try{
						oF = new FileOutputStream(fT);
				}catch(FileNotFoundException e){
						e.printStackTrace();
				}
				if(iF!=null && oF!=null){
						int recTemp=0;
						try{
								while((recTemp = iF.read())!=-1){
										oF.write(recTemp);
								}
								System.out.println("Copy over!");
						}catch(IOException e){
								e.printStackTrace();
						}
						try{
								iF.close();
								oF.close();
						}catch(IOException e){
								e.printStackTrace();
						}
				}
		}
}//Demo

在控制台输入:java Demo C:\JavaStudy\JavaIO\test04.txt C:\JavaStudy\JavaIO\test03.txt

output: Copy over

同时在文件夹复制test04.txt到test03.txt。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值