字节流OutputStream、InputStream、文件拷贝,BufferedInputStream、BufferedInputStream Java第十六天(一)

本文深入探讨Java中的字节流概念,包括OutputStream和InputStream的基本用法,如何使用字节流进行文件的读写操作,以及如何利用BufferedInputStream和BufferedOutputStream提高文件处理效率。

字节流

 输出OutputStream

  由于OutputStream是抽象类,所以要使用它的子类FileOutputStream。

  常用构造器(覆盖写入)

FileOutputStream(File file) 
创建文件输出流以写入由指定的 File对象表示的文件。 
例:
		OutputStream out = new FileOutputStream(new File("文件名.txt"));
---------------------------------------------------------------------------------
FileOutputStream(String name) 
创建文件输出流以指定的名称写入文件。
		OutputStream out = new FileOutputStream("文件名.txt");

  常用方法

void 			write(byte[] b) 
将 b.length个字节从指定的字节数组写入此文件输出流。  
---------------------------------------------------------------------------------
void 			write(byte[] b, int off, int len) 
将 len字节从位于偏移量 off的指定字节数组写入此文件输出流。  
---------------------------------------------------------------------------------
void 			write(int b) 
将指定的字节写入此文件输出流。 

   使用字节流向一个文件中写入字符串

  步骤:
①创建目标文件
②创建字节输出流对象
③将字符串变成字节数组
④写入单个字节(增强for循环)
⑤释放资源

	public static void main(String[] args) {
//		创建字节输出流的对象
		OutputStream out = null;
		try {
//			创建目标文件
			out = new FileOutputStream("Test.txt");
//			定义要写入的字符串
			String str = "HelloWorld!";
//			将字符串转换为字节数组
			byte[] be = str.getBytes();
			
//			将指定byte数组中从指定起始位置及长度的字节写入此文件输出流
			out.write(be, 2, 4);
			
//			将此字节数组长度个字节写入到此文件输出流
			out.write(be);
			
//			写入单个字节
			for(byte b:be) {
//				将指定单个字节写入此文件输出流
				out.write(b);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(out != null) {
				try {
					//释放资源
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

  常用构造器(追加写入)

FileOutputStream(File file, boolean append) 
创建文件输出流以写入由指定的 File对象表示的文件。 
---------------------------------------------------------------------------------
FileOutputStream(String name, boolean append) 
创建文件输出流以指定的名称写入文件。 
	public static void main(String[] args) {
//		创建字节输出流的对象
		OutputStream out = null;
		try {
//			创建目标文件
//			当构造器第二个参数为true时,每运行一次程序就会写入一次
			out = new FileOutputStream("Test.txt",true);
//			定义要写入的字符串
			String str = "HelloWorld!";
//			将字符串转换为字节数组
			byte[] be = str.getBytes();
//			将此字节数组长度个字节写入到此文件输出流
			out.write(be);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

 输入InputStream

  由于InputStream是抽象类,所以要使用它的子类FileInputStream。

  常用构造器

FileInputStream(File file) 
通过打开与实际文件的连接创建一个FileInputStream。该文件由文件系统中的File对象file命名。
例:
  		InputStream in = new FileInputStream(new File("文件名.txt"));
--------------------------------------------------------------------------------- 
FileInputStream(String name) 
通过打开与实际文件的连接来创建一个FileInputStream,该文件由文件系统中的路径名name命名。
例:
		InputStream in = new FileInputStream("文件名.txt");
---------------------------------------------------------------------------------  

  常用方法

int 				read() 
从该输入流读取一个字节的数据。  
---------------------------------------------------------------------------------
int 				read(byte[] b) 
从该输入流读取最多 b.length个字节的数据为字节数组。  
---------------------------------------------------------------------------------
int 				read(byte[] b, int off, int len) 
从该输入流读取最多 len字节的数据为字节数组。
参数列表含义:
			(目标字节数组,存入目标表数组的起始位置,存入字节长度)
---------------------------------------------------------------------------------
	public static void main(String[] args) {
		InputStream in = null;
			try {
				 in = new FileInputStream("Test.txt");
				 
//				 由于此方法是读取单个字节,所以效率低
//				 此方法的返回值是对应的ASCII码或Unicode码
				 int i = in.read();
				 System.out.println((char)i);
				 
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}catch(IOException e){
				e.printStackTrace();
			}finally {
				if(in != null) {
					try {
						in.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
	}

  可以使用以下read方法中的一个替换上方代码中的read方法

//				 此方法将读取到的数据暂存到数组中,并返回读取到数据的字节长度
				 byte[] be = new byte[1024];
				 int len = in.read(be);
				 System.out.println(new String(be,0,len));

//				 此方法从字节输入流的对象中读取指定位置及长度的字节数
				 byte[] bte = new byte[1024];
				 int leng = in.read(bte,0,6);
				 System.out.println(new String(bte,0,leng));

 使用字节流做文件拷贝

 字符流只能限制做文本类型的文件拷贝,但字节流可以拷贝多种类型的文件。
  步骤:
①创建字节流输入、输出流的对象
②读取数据,并写入数据
③关闭资源

public class CopyStreamTest {

	public static void main(String[] args) {
		InputStream in = null;
		OutputStream out = null;
		
		try {
			in = new FileInputStream("Test.jpg");
			out = new FileOutputStream("AAA.jpg");
			byte[] be = new byte[1024];
			int len = -1;
			while((len = in.read(be)) != -1) {
				out.write(be,0,len);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch(IOException e) {
			e.printStackTrace();
		}finally {
				try {
					if(out != null) {
					out.close();
					}
					if(in != null) {
						in.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
}

BufferedInputStream

  BufferedInputStream:高效缓冲区字节输入流。 当创建BufferedInputStream时,将创建一个内部缓冲区数组。 当从流中读取或跳过字节时,内部缓冲区将根据需要从所包含的输入流中重新填充,一次有多个字节。

 常用构造器

BufferedInputStream(InputStream in) 
创建一个 BufferedInputStream并保存其参数,输入流 in ,供以后使用。  
例:
		FileInputStream fin = new FileInputStream("Test.txt");
		InputStream bin = new BufferedInputStream(fin) ;
---------------------------------------------------------------------------------
BufferedInputStream(InputStream in, int size) 
创建 BufferedInputStream具有指定缓冲区大小,并保存其参数,输入流 in ,供以后使用。
		FileInputStream fin = new FileInputStream("Test.txt");
		InputStream bin = new BufferedInputStream(fin,1024) ;  
---------------------------------------------------------------------------------

 由于BufferedInputStream继承于InputStream,所以常用方法一致。

	public static void main(String[] args) {
		InputStream bin = null;
		try {
			bin =  new BufferedInputStream(new FileInputStream("Test.txt")) ;
//			 此方法是读取单个字节,效率高于InputStream单个字节
//			 此方法的返回值是对应的ASCII码或Unicode码
			 int i = bin.read();
			 System.out.println((char)i);		 
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch(IOException e) {
			e.printStackTrace();
		}finally {
			if(bin != null) {
				try {
					bin.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

  可以使用以下read方法中的一个替换上方代码中的read方法

//			 此方法将读取到的数据暂存到数组中,并返回读取到数据的字节长度
			 byte[] be = new byte[1024];
			 int len = bin.read(be);
			 System.out.println(new String(be,0,len));
			 
//			 此方法从字节高效缓冲区输入流的对象中读取指定位置及长度的字节数
			 byte[] bte = new byte[1024];
			 int leng = bin.read(bte,0,6);
			 System.out.println(new String(bte,0,leng));

 BufferedOutputStream

 常用构造方法

BufferedOutputStream(OutputStream out) 
创建一个新的缓冲输出流,以将数据写入指定的底层输出流。  
例:
		OutputStream out = new FileOutputStream("Test.txt");
		BufferedOutputStream bout = new BufferedOutputStream(out);
---------------------------------------------------------------------------------
BufferedOutputStream(OutputStream out, int size) 
创建一个新的缓冲输出流,以便以指定的缓冲区大小将数据写入指定的底层输出流。  
		OutputStream out = new FileOutputStream("Test.txt");
		BufferedOutputStream bout = new BufferedOutputStream(out,1024);
---------------------------------------------------------------------------------

 由于BufferedOutputStream继承于OutputStream,所以常用方法一致。

	public static void main(String[] args) {
		BufferedOutputStream bout = null;
		try {
			bout = new BufferedOutputStream(new FileOutputStream("Test.txt"));
//			定义要写入的字符串
			String str = "HelloWorld!";
//			将字符串转换为字节数组
			byte[] be = str.getBytes();
			
//			将指定byte数组中从指定起始位置及长度的字节写入此文件高效缓冲区输出流
			bout.write(be, 2, 4);
			
//			将此字节数组长度个字节写入到此文件高效缓冲区输出流
			bout.write(be);
			
//			写入单个字节
			for(byte b:be) {
//				将指定单个字节写入此文件高效缓冲区输出流
				bout.write(b);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch(IOException e) {
			e.printStackTrace();
		}finally {
			if(bout != null) {
				try {
					//释放资源
					bout.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值