您的位置:首页 > 编程语言 > Java开发

java Io 笔记2

2016-03-31 11:18 447 查看
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.junit.Test;

/**
*
* 利用 FileInputStream  和 FileOutputStream 实现文件的复制  。
*
* 先读取到内存中  再写入到另一个磁盘路径下。
*     源文件  源文件必须要存在,否则 报异常。
*           要复制的目标文件   文件可以不存在,  直接复制;若文件已经存在, 则 覆盖。
*
*
*   读入jvM的过程 :
*
*   1、File 创建 需要读取的 File对象     [文件必须存在 ]
*   2、FileInputStream  把 File对象 传入  FileInputStream文件输入流 的构造器      创建  FileInputStream [字节流--处理音频、视频 、图片等字节存储的文件,文本文件需要用Reader 字符流 处理]对象
* 3、定义节点流 每次 传输的 字节个数 数组  byte[]  b = new  byte[1024]; 数组大小根据读取文件的大小 自行设定。
*      --当数组设定为偶数时  读取文本文件含中文<占俩字节> 没问题, 但不建议如此使用。    数组时奇数时 读取中文会有乱码 !!  一般FileInputStream 只处理非文本文件。
*   4、 while循环 读取 每个装载的byte数组的内容 <如果装载数组没满,只需要遍历到 数组具体装载的个数 len,而不是 数组的长度 !!>
*      用 String st =new String(b,0,len);接收 进行处理//或者进行 复制。
*   5、 流是稀缺资源  在使用完成后  需要进行关闭  。
*
*   从 JvM 写出到磁盘的过程:
*   1、File 创建 需要读取的 File对象     [文件可以不存在 ,在执行过程中创建] 若文件已经存在, 则进行覆盖
*   2、FileOutputStream  把 File对象 传入  FileOutputStream文件输入流 的构造器      创建  FileOutputStream对象
*     [字节流--处理音频、视频 、图片等字节存储的文件,纯文本文件需要用Reader 字符流 处理]对象
*     byte[] b =  new String("this  is  output  to  txt !").getBytes();
*   3、output.write(new byte[]); 输出流    用字节数组的形式把内容写入到文件中。
*   4、用完流之后 进行关闭。   先关闭 输出流 。。。。 再关闭输入流。   关闭 要放在 finally
*
*
*
*
*
*
* @author Administrator
*
*/
public class InputStreamOutputStreamCopyFile {

/**
* 节点流      中的     字节流   ------  测试文件 复制方法。
*/
@Test
public  void  testcopy(){
// 源文件  源文件必须要存在,否则 报异常。
String   src = "D:\\io\\1.wmv";
// 要复制的文件   文件可以不存在,  直接复制;若文件已经存在, 则 覆盖。
String   dest = "D:\\io\\1_copy2.wmv";
long start = System.currentTimeMillis();
this.copyFile(src, dest);
long end  = System.currentTimeMillis();

System.out.println("耗时:ms "+(end - start));//298毫秒

}

/**
* 利用  输入输出流     实现  ----------文件的复制   ----文件的大小  和字节数      完全 一样
* @param src
* @param dest
*/
public  void  copyFile(String src ,String dest){
// File  srcFile =new File("D:\\io\\1.jpg");
// File  destFile = new  File("D:\\io\\1_copy.jpg");
File  srcFile =new File(src);
File  destFile = new  File(dest);

FileInputStream fis =null;
FileOutputStream fos =null;
try {
fis = new  FileInputStream(srcFile);
fos = new FileOutputStream(destFile);

// 此处建立的b[]数组       每次 传输    20个字节  = 即 20个Byte 。
// 一个字母和数字 占一个字节,一个 汉字 占两个字节 按照字节读取,一个汉字被拆成两个字节 会出现乱码。
// byte[] b = new byte[20];

// 读取视频文件或者 音频文件时 可以扩大 字节数组     每次集齐 1KB的字节 然后 磁盘和jVm交互一次, 减少交互频次,提高效率。
byte[] b = new byte[1024];

int len;
while((len =fis.read(b))!= -1){
fos.write(b, 0, len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

if(fis != null){
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

if(fos != null){
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

/**
* 先读取到jvm 虚拟机 内存中,  然后再   进行 写入到磁盘中。
*
*
*/
@Test
public  void  testCopyFile(){
File  srcFile =new File("D:\\io\\1.jpg");
File  destFile = new  File("D:\\io\\1_copy.jpg");
// File  srcFile =new File(src);
// File  destFile = new  File(dest);

FileInputStream fis =null;
FileOutputStream fos =null;
try {
fis = new  FileInputStream(srcFile);
fos = new FileOutputStream(destFile);

// 此处建立的b[]数组       每次 传输    20个字节  = 即 20个Byte 。
// 一个字母和数字 占一个字节,一个 汉字 占两个字节 按照字节读取,一个汉字被拆成两个字节 会出现乱码。
// byte[] b = new byte[20];

// 读取视频文件或者 音频文件时 可以扩大 字节数组     每次集齐 1KB的字节 然后 磁盘和jVm交互一次, 减少交互频次,提高效率。
byte[] b = new byte[1024];

int len;
while((len =fis.read(b))!= -1){
fos.write(b, 0, len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

if(fis != null){
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

if(fos != null){
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: