文件操作(三)《转载》

来源:互联网 发布:artrage 5 mac 编辑:程序博客网 时间:2024/05/13 06:51

文件操作(三)《转载》

package file;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FilePath {
public static void main(String args[]){
 //java在应用的固定文件夹目录/nd/freck下,创建新的文件夹
//    test1();
    //删除文件夹目录/nd/freck下的所有东西
//  test2();
    //将制定文件从一个文件夹拷贝到另一个文件夹
    test3();
}
 
 private static void test1(){
     String path = "";
     String folder1 = "/upload";
     String folder2 = "/aa";
        //工程下已经存在文件夹结构nd/freck
        path = "."+File.separator + "nd" + File.separator + "freck";
        String sourthpath1 = path+folder1;
        String sourthpath2 = path+folder2;
        File file1 =  new File(sourthpath1);
        File file2 = new File(sourthpath2);
        if (!file1.isDirectory()) {
      System.out.println("Create source Folder1");
      file1.mkdirs();
     }
        if (!file2.isDirectory()) {
      System.out.println("Create source Folder2");
      file2.mkdirs();
     }
    }
 private static void test2() {
     String destinationPath = "."+File.separator + "nd" + File.separator + "freck";
     File path = new File(destinationPath);//查询路径
        String[] strList;//存放所有查询结果
        strList = path.list();//执行查询
        String ref="";
        for(int i=0;i           System.out.println("will delete-"+strList[i]);//输出这个文件或者目录的名称
          String fileTree = destinationPath+"/"+strList[i];//确定真实路径
          File turePath = new File(fileTree);
          boolean reBool = turePath.delete();//删除成功为ture
          System.out.println(reBool);//输出结果
        }
 }
 private static void test3() {
  //将nd/freck/copy1目录下的123.txt拷贝到nd/freck/copy2下
  String sourcepath = "."+File.separator + "nd" + File.separator + "freck" + File.separator + "copy1"
  +File.separator +"123.txt";
  String targetpath = "."+File.separator + "nd" + File.separator + "freck" + File.separator + "copy2"
  +File.separator +"123.txt";
  FileInputStream sFile = null;
        BufferedInputStream sBuf = null;
        FileOutputStream tFile = null;
        BufferedOutputStream tBuf = null;
        try
        {
            byte myBuffer[] = new byte[2048];
            sFile = new FileInputStream(sourcepath);
            sBuf = new BufferedInputStream(sFile);
            tFile = new FileOutputStream(targetpath);
            tBuf = new BufferedOutputStream(tFile);
            for(int nBytes = sBuf.read(myBuffer, 0, 2048); nBytes > -1; nBytes = sBuf.read(myBuffer, 0, 2048))
                tBuf.write(myBuffer, 0, nBytes);
        }
        catch(IOException exception)
        {
            System.out.println("Unable to complete the file copy./n" + exception + "/nsourcepath: " + sourcepath + "/ntargetpath: " + targetpath);
        }
        finally
        {
            try
            {
                sBuf.close();
            }
            catch(Exception ignore) { }
            try
            {
                tBuf.close();
            }
            catch(Exception ignore) { }
            try
            {
                tFile.close();
            }
            catch(Exception ignore) { }
            try
            {
                sFile.close();
            }
            catch(Exception ignore) { }
        }
 }
}