IO流分享

IO流

它的目的就是要跟文件产生联系.
流:一个连串流动的数据,管道
数据源:=目的地
以谁为中心
=以程序为中心
流的分类:
流向:
输入流
输出流

操作单元:
字节流
字符流

功能
节点流:直接从数据源到目的地
功能流:经过包装的.

字节流也叫万能流

字节输入流

InputStream 流向:输入流 操作单元:字节流, 功能:节点流
此抽象类是表示字节输入流的所有类的父类. 是不能实例化的.,
public class FileInputStream extends InputString
从文件系统 中某个文件或得输入字节,
read()每次从输入流中读入一个字节的内容,想要读入多个,需要一个字节一个字节的读入 .

public class ByteDemo01 {
	public static void main(String[] args) throws IOException {
		//1.建立联系
		File src=new File("D:/test.txt");
		//2.选择流
		InputStream is=new FileInputStream(src);
		//3.操作  读入
		/*read() 每次从输入流中读入一个字节的内容,想要读入多个 手动一个字节一个字节读入
		 * int num=is.read();
		System.out.println((char)num);
		
System.out.println((char)(is.read()));
		System.out.println((char)(is.read()));*/
				//重复读取   不确定次数
		int num=-1;
		while((num=is.read())!=-1){
			System.out.println((char)num);
		}
		//4.关闭
		is.close();
	}
}

如果数据量大可以使用数组读入
int read(byte[] b) :一个字节数组一个字节数组的读入.

public class ByteDemo02 {
	public static void main(String[] args){
		//1.选择流
		InputStream is=null;
		try {
			is = new FileInputStream("D:/test.txt");
			//2.准备卡车
			byte[] car=new byte[5];  //一般使用1024或1024的整数倍
			//3.操作  读入   read(byte[]) 读到字节数组中多少个数的数据返回值为多少,没有读到返回-1
//			int len=is.read(car);
				//读取  重读读入  通过字节数组
			int len=-1;
			while((len=is.read(car))!=-1){
				//4.处理数据
//				System.out.println(len);
				System.out.print(new String(car,0,len));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			//5.关闭
			try {
				if(is!=null){
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

字节输出流

字节输出流:OutputStream
此抽象类是表示输出字节流的所有类的父类.
FileoutputStrarm 文件输出流是用于将数据写入file的输出流.

public class ByteDemo03 {
public static void main(String[] args) throws IOException {
	//1.选择流  如果目的地的文件不存在系统会自动创建指定文件,文件夹不会自动创建
	OutputStream os=new FileOutputStream("D:/hehe.txt",true);  //boolean append  true追加 false不追加(默认)
	//2.准备数据
	int num=97;
	String str="周杰伦--可爱女人";
	byte[] car=str.getBytes();
	//3.写出
	//os.write(num);
	//os.write(98);
	os.write(car);
	//4.强制刷出
	os.flush();
	//5.关闭
	os.close();
}
}

字节流的文件拷贝

文件的拷贝:首先数据源 ---------------程序--------------目的地
1.选择流
输入流: InputStream  is  =new FileintputStream("D:/text.txt);
输出流:OutputStream os =new FileOutputStream("E:/text.txt);
2.准备字节数组
byte[] car =new byte[1024];
3.读入写出
int len=-1;//读入到数组中数据个数.
While((len=is.read(car))!=-1){
os.write(car,0,length)};
4.刷出
os.flush();
5.关闭       后开的先关
os.close
is.close.

字符流:

只能操作纯文本的内容 .txt .html 节点流 字符流
Reader 字符输入流 抽象类 FileReader 输入流
read()| read(char[]) + close()
Writer 字符输出流 抽象类 FileWriter 输出流
FileWriter write() | write(char[]) | append() +flush() + close()
写入

public class CharDemo01 {
public static void main(String[] args) throws IOException {
	//1.选择流
	Reader read=new FileReader("D:/hehe.txt");
	//2.准备卡车
	char[] car=new char[1024];
	//3.读入
	System.out.println((char)(read.read()));
	int len=-1;
	while((len=read.read(car))!=-1){
		System.out.println(new String(car,0,len));
	}
	//4.关闭
	read.close();
}
}

写出
public class CharDemo02 {
public static void main(String[] args){
	//1.选择流
	Writer write=null;
	try {
		write=new FileWriter("D:/heihei.txt"); //追加
		//2.准备数据
		String str="千千阙歌";
		char[] arr={'尚','学','堂'};
		//3.写出
		write.append(str);
		write.write(arr);
		//4.刷出
		write.flush();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} finally{
		if(write!=null){
			try {
				write.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
}

字符流实现文件的拷贝

1.文件----程序     输入
2.程序----文件     输出
public class CopyFile03 {
public static void main(String[] args) {
	//1.选择流
	Reader read=null;
	Writer write=null;
	try {
		read=new FileReader(new File("D:/test.txt"));
		write=new FileWriter(new File("D:/dest.txt"),true);
		//2.准备卡车 字符数组
		char[] car=new char[1024];
		int len=-1;
		while(-1!=(len=read.read(car))){
			write.write(car, 0, len);
		}
		//3.刷出
		write.flush();
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}  finally{
		//关闭
		if(write!=null){
			try {
				write.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if(read!=null){
			try {
				read.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
}

功能流的一种

缓冲流:增强功能,提高性能
使用:功能流(节点流)
字节缓冲流:
字节输入缓冲流 BufferedInputStream
字节输出缓冲流 BufferedOutputStream
无新增方法,可以发生多态.

public class BufferedInputStreamDemo {
public static void main(String[] args) {
	//1.选择流
	InputStream  is=null;
	OutputStream os=null;
	try {
		is=new BufferedInputStream(new FileInputStream("D:/test.txt"));
		os=new BufferedOutputStream(new FileOutputStream("E:/dest.txt"));
		//2.准备卡车
		 byte[] car=new byte[1024];
		 //3.读入写出
		 int len=-1;  //读入到小卡车中数据的个数
		 while((len=is.read(car))!=-1){
			 os.write(car,0,len);
		 }
		//4.刷出
		os.flush();
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} finally{
		//5.关闭   先打开的后关闭
		try {
			//多行添加try..catch=>alt+shift+z
			if(os!=null){
				os.close();
			}
			if(is!=null){
				is.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
}

字符缓冲流:
BufferedReader 新增方法 readLine() 读取一行
BufferedWriter 新增方法 newLine() 写出换行符
不能发生多态,因为父类引用对子类新增方法不可见

public class BufferedReaderDemo {
public static void main(String[] args) {
	//1.选择流
	BufferedReader read=null;
	BufferedWriter write=null;
	try {
		read=new BufferedReader(new FileReader("D:/haha.txt"));
		write=new BufferedWriter(new FileWriter("D:/aaa.txt"));
		//2.读入写出
		String msg=null;
		while(null!=(msg=read.readLine())){
			write.write(msg);
			write.newLine();
		}
		//3.刷出
		write.flush();
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}  finally{
		//关闭
		if(write!=null){
			try {
				write.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if(read!=null){
			try {
				read.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
}	

基本数据类型流

基本数据类型流:读写带有基本数据类型的数据+String
 基本数据类型流(字节节点流)
     DataInputStream(inputStream):     新增方法:readXXX()
     DataoutputStream(outputStream):   新增方法:writeXXX()
  EOFException:文件存在,但是内部无法读取,不是源文件.

转化流

  可以把字节流转为字符流使用
  InputStreamReader(InputStream,String charset)
  OutputSteamWriter(OutputSteam,String charset)
  
  可能出现乱码的情况:
  1.字符编码格式不统一
  2.缺少字节个数
  不同编码格式汉字所占字节大小:
  ISO-8859-1  1个字节
  GBK			2个字节
  GB2312		2个字节
  UTF-8   	3个字节
  
  序列化:将对象信息转为可存储或者可传输的信息格式
  对象流:
  序列化输出流   ObjectOutputStream 
  反序列化输入流   ObjectInputStream
  新增方法  readXxx()   writeXxx()
  先序列化后反序列化
  不是所有的类都能序列化     实现一个空接口    java.io.Serializable
  不是所有的属性都需要序列化   transient
  静态的内容不能被序列化 默认值
  如果父类实现了Serializable,子类中所有的内容都可以序列化
  如果子类实现Serializable,父类中的内容没有序列化能力,只有子类的内容有
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值