字节流的扩展


public class ByteArryFileInputStream {
// FileInputStream() Input是从硬盘到内存 FileOutputStream() 而output是从内存到硬盘,所以实现了复制粘贴。
//read() 调用方法读取 \r\n 换行
//使用字节子类进行数据的操作,这种方式是以1024个字节传输的速度
@Test
public void testFileByteReadArry() {
	// currentTimeMillis 返回以毫秒为单位的当前时间。
	long s = System.currentTimeMillis();
	// 设置对象
	FileInputStream fis = null;
	FileOutputStream fos = null;
	try {
		fis = new FileInputStream("F:\\cxf框架.zip");
		fos = new FileOutputStream("F:\\soap\\cxf框架.zip");
		//创建一个字节数组,他的字节传输速度是1024*10。
		byte[] b = new byte[1024];
		//定义一个数据类型。
		int len = 0;
		//写入判断语句。按照字节数组的传输速度进行传送。
		while ((len = fis.read(b)) != -1) {
			// 按照b的速度,从0开始,一直到结束。
			fos.write(b, 0, len);
		}
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	// 最终执行的代码段。
	finally {
		// 判断是否执行,如果执行过之后就结束释放资源。
		// 如果没有执行过,就跳过If语句。
		if (fis != null) {
			try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (fos != null) {
					try {
						fos.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		// 计算结束时间
		long c = System.currentTimeMillis();// 这种方式复制需要61秒
		System.out.println(c - s);
	}
}


/*
 * 
 * 如果要从网络中下载文件时,我们知道网络是不稳定的,也就是说网络下载时,read()方法是阻塞的
 * 说明这时我们用inputStream.available()获取不到copy文件的总大小。网络中有其他方式获取文件字节大小
 */
// 但是这种available()方法也不可取因为返回的是一个int值 如果文件很大就会导致下载的数据不全,但是速度是超快
@Test
public void testByteRead() {
	long s = System.currentTimeMillis();
	File file = new File("F:\\cxf框架.zip");
	InputStream input = null;
	OutputStream out = null;
	try {
		input = new FileInputStream(file);
		out = new FileOutputStream("F:\\soap\\cxf框架.zip");
		int len = 0;
		// input.available()这个应该是获取字节数的多少
		System.out.println("使用文件夹的方式获取字节大小" + file.length());
		System.out.println("使用流的方式获取字节的大小" + input.available());
		byte[] byt = new byte[input.available()];
		len = input.read(byt);
		out.write(byt, 0, len);
	} catch (IOException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
	// 计算结束时间
	long c = System.currentTimeMillis();// 这种方式复制需要9秒
	System.out.println(c - s);
}



// 这种方式获取字节大小是比较可取的,但是读取的速度比较慢 ,他合适比较大的文件传输,因为他是long大的数据,再加上缓冲流就比较完美
@Test
public void testGetSize() {
	long s = System.currentTimeMillis();
	File file = new File("F:\\cxf框架.zip");
	InputStream input = null;
	OutputStream out = null;
	long size = 0;
	if (file.exists() && file.isFile()) {
		String fileName = file.getName();
		size = file.length();
		System.out.println("文件" + fileName + "的大小是:" + size);
	}
	try {
		System.out.println(size + "****************");
		input = new FileInputStream(file);
		out = new FileOutputStream("F:\\soap\\cxf框架.zip");
		// 以下的方式是把long转换为字节数组
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		dos.writeLong(size);
		byte[] buf = baos.toByteArray();
		System.out.println(buf.length + "********");

		// ByteBuffer buffer = ByteBuffer.allocate(8);
		// buffer.putLong(0, size);
		int len = 0;
		// byte[] buf = buffer.array();

		while ((len = input.read(buf)) != -1) {
			// System.out.println("************");
			out.write(buf, 0, len);
		}
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	long c = System.currentTimeMillis();// 这种6164秒 特别慢
	System.out.println(c - s);
}



// 这种方式也是可以得到原来的资源和现在的资源一样,不过读取的速度太慢不可取,一个字节读是最慢的 不可取
@Test
public void testFileByteRead() {
	long s = System.currentTimeMillis();
	FileInputStream input = null;
	FileOutputStream out = null;
	try {
		// 指定要复制的文件及路径。Input是从硬盘到内存。
		input = new FileInputStream("F:\\cxf框架.zip");
		// 指定要粘贴的文件及路径。而output是从内存到硬盘,所以实现了复制粘贴。
		out = new FileOutputStream("F:\\soap\\cxf框架.zip");
		int len = 0; // 定义一个数据类型。
		// 定义判断条件。
		// read()方法:读取文件的一个字节,当执行到文件内容末尾时返回-1
		while ((len = input.read()) != -1) {
			out.write(len);
		}
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		if (input != null) {
			try {
				input.close();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (out != null) {
					try {
						out.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		System.out.println("*************");
		long c = System.currentTimeMillis();// 这种49641秒 最慢的
		System.out.println(c - s);
	}
}
}