JavaIO流之代码实例

字节输入流FileInputStream代码举例

		FileInputStream fis = null;
		try {
			// 1.创建一个File对象
			File file = new File("Hello.txt");
			// 2.创建一个FileInputStream对象,将文件加载到一个输入流中
			fis = new FileInputStream(file);
			//注意:字符输入流这里需FileReader fr = new FileReader(file);
			// 读取到的数据写入到数组
			byte[] b = new byte[5];
			// 注意:字符输入流这里只需换为char[] b = new char[5];
			// 每次读入到byte数组中字节的长度(真实长度)
			int len;
			// 3.调用FileInputStream类的方法,完成对文件的读取
			while((len=fis.read(b)) != -1) {
				/*//第一种方式
				 * for (int i = 0; i < len; i++) {
					System.out.print((char)b[i]);
				}*/
				// 第二种方式
				String str = new String(b, 0, len);
				System.out.print(str);
			}		
			} catch (IOException e) {
				e.printStackTrace();
		}finally {
			try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		FileInputStream fis = new FileInputStream(f1);
		// 3.调用FileInputStream类的方法,完成对文件的读取
		// read():读取文件的一个字节,当执行到文件末尾时返回-1

字节输出流FileOutputStream代码举例

		FileOutputStream fos = null;
		try {
			//1.创建一个File对象,指定写入文件位置
			//(如果指定文件存在将原有位置覆盖,不存在新建)
			File file = new File("hello2.txt");
			if(!file.exists()) {
				file.createNewFile();
			}
			// 2.创建一个FileOutStream对象,将file对象作为形参传递给构造器
			fos = new FileOutputStream(file);
			// 3.写入数据
			String str = "I love you forever and yibainian";
			fos.write(str.getBytes());// 转化为字符类型数组
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

字节输入流、输出流的混合使用

	// 实现文件复制,从硬盘读入文件并写入到另一个位置
	public void CopyFile(String src,String dest) {
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			// 1.提供读入写出的文件
			File f1 = new File(src);
			File f2 = new File(dest);
			// 2.提供对应的输入输出流
			fis = new FileInputStream(f1);
			fos = new FileOutputStream(f2);
			// 3.实现文件的复制(先读数据再写)
			byte[] bs = new byte[24];
			int len;
			while((len = fis.read(bs))!= -1) {
				// 写数据(从头开始写,写的长度是len)
				fos.write(bs, 0, len);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 最早打开,最晚关闭的原则
			if(fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
		}

	@Test
	public void testCopyFile() {
		long start = System.currentTimeMillis();
		// 图片复制到工程里
		String src = "one.jpg";
		String dest = "two.jpg";
		CopyFile(src, dest);
		// 实现文件复制
		long end = System.currentTimeMillis();
		
		System.out.println("花费的时间" + (end - start));
	}

缓冲流的使用案例

package com.hpe.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.junit.Test;
/**
 * flush():清空缓冲区,没有关闭流,刷新之后还可以继续使用流对象进行数据操作。
 * close():关闭流,关闭之前先刷新流,关闭之后流对象不可以再被使用
 * 为什么使用Buffer?(重点)
 * 1.Buffered的核心技术在内部是由一个内存缓冲区,默认大小是8M
 * 2.每次读写数据都是从缓冲区里面读入数据,当缓冲区的数据大小积攒到一定量,一次写入到磁盘IO
 * 3.从内存缓冲区中读取数据远比直接从磁盘等介质效率更高
 * 
 * 注意:	使用缓冲流就是对节点流加了一层封装,内部实现还是依赖于节点流
 * 		使用缓冲流只关闭了缓冲流的输入输出(内部自动关闭了节点流对象)
 */
public class TestBuffered {
	
	@Test
	public void testBufferesInputOutputStream() {
		// 字节输入缓冲流
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			// 1.提供读写文件
			File f1 = new File("one.jpg");
			File f2 = new File("1.jpg");
			// 2.创建响应的节点流
			FileInputStream fis = new FileInputStream(f1);
			FileOutputStream fos = new FileOutputStream(f2);
			// 3.将创建的节点流对象作为形参传递给缓冲流的构造器
			bis = new BufferedInputStream(fis);
			bos = new BufferedOutputStream(fos);
			// 4.实现文件复制的操作
			byte[] b = new byte[1024];
			int len;
			while((len = bis.read(b)) != -1) {
				bos.write(b,0,len);
				// flush():清空缓冲区数据
				bos.flush();
			/*
			*String str;// 每次读一行
			while((str = br.readLine()) != null) {
				bw.write(str);// BufferedWriter写入一行信息
				bw.newLine();// 自动换行
				bw.flush();
			}
			*/
			}
			} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(bos != null) {
				try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(bis != null) {
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	// 使用缓冲流实现文件复制
	public void copyFile(String src,String dest) {
		// 字节输入缓冲流
				BufferedInputStream bis = null;
				BufferedOutputStream bos = null;
				try {
					// 1.提供读写文件
					File f1 = new File(src);
					File f2 = new File(dest);
					// 2.创建响应的节点流
					FileInputStream fis = new FileInputStream(f1);
					FileOutputStream fos = new FileOutputStream(f2);
					// 3.将创建的节点流对象作为形参传递给缓冲流的构造器
					bis = new BufferedInputStream(fis);
					bos = new BufferedOutputStream(fos);
					// 4.实现文件复制的操作
					byte[] b = new byte[1024];
					int len;
					while((len = bis.read(b)) != -1) {
						bos.write(b,0,len);
						// flush():清空缓冲区数据
						bos.flush();
					}
					} catch (Exception e) {
					e.printStackTrace();
				} finally {
					if(bos != null) {
						try {
							bos.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
					if(bis != null) {
						try {
							bis.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
	}
	@Test
	public void testCopyFile() {
		long start = System.currentTimeMillis();
		String src = "one.jpg";
		String dest = "three.jpg";
		copyFile(src, dest);
		long end = System.currentTimeMillis();
		System.out.println(end - start);
  	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值