java-File

来源:互联网 发布:天津绫致时装淘宝真假 编辑:程序博客网 时间:2024/05/17 02:32
package com.java;import java.io.File;import java.io.IOException;import java.util.List;public class IOStream {    /**     * @param args     * @throws IOException      */    public static void main(String[] args) throws IOException {        // TODO Auto-generated method stub            File file = new File("C:\\Users\\Administrator\\Desktop\\file");            System.out.println(file.exists());            file.mkdir();//创建文件夹            boolean bool =  deleteFile("C:\\Users\\Administrator\\Desktop\\file");    //      file.createNewFile();    //注:如果存在文件,则不会创建            System.out.println(file.exists());//判断是否是一个文件            System.out.println(file.isDirectory()); //判断是否是一个文件夹//          file.delete();//          System.out.println(file.getAbsolutePath());   //获取文件路径//          System.out.println(file.isFile());    }    //删除文件下的文件或目录(递归)    public static boolean deleteFile(String path){        File file = new File(path);        if(!file.exists()){            return false;        }else if(file.isFile()){            file.delete();            return true;        }else{            File [] files = file.listFiles();            for(int i = 0;i<files.length;i++){                String pathFile = files[i].getAbsolutePath();                System.out.println(pathFile);                deleteFile(pathFile);            }            file.delete();   //文件夹删除            return true;    }        //批量字节拷贝    public static void copyFile(String path1,String path2) throws Exception{        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path1));        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path2));        int a;        while((a=bis.read())!=-1){            bos.write(a);            bos.flush();        }        bis.close();        bos.close();    }}}

还有一些方法:
这里写图片描述

原创粉丝点击