关于资料输入输出对象的初步理解

关于文件输入输出对象的初步理解。
   关于输入和输出是以内存为对象的,把内存外部的东西调入内存即为输入,将内存里面的内容储存到硬盘等操作则为输出。
    内存和硬盘就好比两个储水的容器,所以要实现里面的内容互换,就必须要管道连接,这就是输入和输出流对象的作用。
    以代码为例:
    package io0222mwj;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileTest {

public static void main(String args[]) {

String path = "d:\\aa\\jdk1.6.CHM";
String path1 = "d:\\aa\\复制jdk1.6.CHM";
// 读取文件,得到读取的字符串
byte[] bs = FileTest.readFile(path);

FileTest.writeFile(path1,bs);

long start=System.currentTimeMillis();

FileTest.copyFile2Buffer(path,path1);
long end=System.currentTimeMillis();

long result=end-start;

System.out.println("复制完毕,耗费:"+result+"ms");


}
/**
* 读取文件
* @param path
*             要读取文件的地址
* @return
*/


public static byte[] readFile(String path){
try{
//根据文件地址创建一个文件输入流
java.io.FileInputStream fis=new java.io.FileInputStream(path);
//得到流中的字节数
int size=fis.available();

//定义一个数组,用来存储读取的字节
byte[] bs=new byte[size];
int i=0;
//读取一个字节
int t=fis.read();
while (t!=-1){
//将读取的字节放入数组
bs[i]=(byte)t;
i++;
//读取下一个字节
t=fis.read();
}
//将字节数组包装成字符串
//String str=new String(bs)
//String.out.println(str)

fis.close();
return bs;
} catch (Exception ef){
ef.printStackTrace();
}
return null;
}
/**
* 将读取的文件内容写入指定的文件
* @param path
*            写入的文件
* @param bs
*            已读取、要写入的数据
*/

public static void writeFile(String path,byte[] bs){
try{
//创建文件输出流
java.io.FileOutputStream fos=new java.io.FileOutputStream(path);

for(int i=0;i<bs.length;i++){
byte b=bs[i];
//写出字节
fos.write(b);
}
//强制输出
fos.flush();
//关闭流
fos.close();

}catch(Exception ef){
ef.printStackTrace();
}
}
/**
* 拷贝文件的方法
* @param src
*           要拷贝的源文件地址
* @param path
*            拷贝后的文件地址
*/
public static void copyFile(String src,String path){
try{
//创建文件输入流和输出流
FileInputStream fis=new FileInputStream(src);
FileOutputStream fos=new FileOutputStream(path);

//读取一个字节
int b=fis.read();
while(b!=-1){
//将读取的字节写到文件
fos.write(b);
//读取下一个字节
b=fis.read();
}

fos.flush();
fos.close();
fis.close();
}catch(Exception ef){
ef.printStackTrace();
}
}

public static void copyFile2Buffer(String src,String path){
try{
//创建文件输入输出流
FileInputStream fis=new FileInputStream(src);
FileOutputStream fos=new FileOutputStream(path);

//将文件包装成缓冲流
BufferedInputStream bis=new BufferedInputStream(fis);
BufferedOutputStream bos=new BufferedOutputStream(fos);

//读取一个字节
int b=bis.read();
while(b!=-1){
//将读取的字节写到文件
bos.write(b);
//读取下一个字节
b=bis.read();
}

fos.flush();
fos.close();
fis.close();
}catch(Exception ef){
ef.printStackTrace();
}
}

}


注意点:byte可以自动转成int,因为需要无符号的字节;
        每个方法都只会执行到return,就不会执行了