文件管理
Java中的对文件的管理,通过java.io包中的File类实现
Java中文件的管理,主要是针对文件或是目录路径名的管理
- 文件的属性信息
- 文件的检查
- 文件的删除等
 不包括文件的访问
File类
File类的构造方法:
- 
通过将给定路径名字符串转换成抽象路径名来创建一个新 File 实例 Flie file=new File("D:\\io\\test.txt");
2.通过将给定File的url转换成抽象路径名来创建一个新 File 实例
File file=new File("test.txt");
3.根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例
File file=new file("D:\\io","test.txt");
这里需要注意的是上面的新建File实例的书写路径方式是在Windows操作系统下支持。其中“\\”也可以用“/”代替。
    //以下两种路径书写方式Windows操作系统下都可以
    File path1=new File("d:/io");
    File path2=new File("d:\\io");
    //Linux下路径是
    File linux_path=new File("/root/home");
    //兼容操作系统
    File path_ty = new File("d:"+File.separator+"io");
上面列举了不同操作系统下的新建Path,新建File实例可以类比。
File类的相关常用方法




流的概念及API
流的概念
流(Stream )的概念代表的是程序中数据的流通
数据流是一串连续不断的数据的集合
在Java程序中,对于数据的输入/输出操作是以流(Stream)的方式进行的
- 输入流 — 流入程序的数据
- 输出流 — 流出程序的数据
 在java程序中,从输入流读取数据(读到内存中),而从输出流输出数据(从内存存储到文件或显示到屏幕上)
流的分类
1.按流向分:
输入流:从文件------------------>内存或程序(read in)
输入流:从程序或内存------------------>文件(write out)
2.按处理数据单位不同:
字节流:以字节形式Byte[]读取,一般可以是音视频文件、word或excel等。
字符流:char或者String,一般可以是纯文本类型的文件。
3.按功能不同
节点流(直接与文件交互的,是低级流)。
处理流(缓冲流,是高级流)。
高级流要用到低级流的功能  
java.io包中有两大继承体系
以byte处理为主的Stream类,他们的命名方式是XXXStream
以字符处理为主的Reader / Writer类,他们的命名方式XXXReader或XXXWriter
InputStream、OutputStream、Reader、Writer这四个类,是这两大继承体系的父类  
普通文件拷贝:
package com.neusoft.chap12;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import org.junit.Test;
public class SimplyCopyFile {
    @Test
    public void test(){
        FileReader fr=null;
        BufferedReader br=null;
        FileWriter fw=null;
        PrintWriter pw=null;
        try {
            fr=new FileReader(new File("e:\\io\\test.txt"));
            br=new BufferedReader(fr);
            fw=new FileWriter(new File("d:\\io\\test.txt"));
            pw=new PrintWriter(fw);
            String str;
            while((str=br.readLine())!=null){
                System.out.println(str);
                pw.write(str);
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                fw.close();
                fr.close();
                br.close();
                pw.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
Stream文件拷贝:
package com.neusoft.chap12;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.junit.Test;
public class StreamFileCopy {
@Test
public void test(){
    FileInputStream fis=null;
    BufferedInputStream in=null;
    FileOutputStream fos=null;
    BufferedOutputStream out=null;
    try {
        fis=new FileInputStream(new File("e:\\io\\test.doc"));
        in=new BufferedInputStream(fis);
        fos=new FileOutputStream(new File("d:\\io\\test.doc"));
        out=new BufferedOutputStream(fos);
        int len;
        try {
            //先预估能读取多少字节的文件
            len=in.available();
            //初始化一个byte数组来接收读取到的字节
            byte[] b=new byte[len];
            //开始读取,num表示读取的数量
            int num=in.read(b);
            //如果读取的数量不为-1
            if(num!=-1){
                //表示没有到达文件尾,开始写入
                out.write(b, 0, num);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
        try {
            /*fis.close();*/
            in.close();
        /*  fos.close();*/
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
        }
    }
}