Java操作文件常用的9种方法

一、

//1、创建文件夹

public static boolean createFolder(String pathName){
    try {
        File folderPath = new File(pathName);
        if(!folderPath.exists()){
            folderPath.mkdir();
        }
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println("新建文件夹时,出错!");
        return false;
    }
}

//2、创建文件

public static boolean createFlie(String filePath){
    try {
        File file = new File(filePath);
        if(!file.exists()){
            file.createNewFile();
        }
        FileWriter fw = new FileWriter(file);
        PrintWriter pw = new PrintWriter(fw);
        pw.println(filePath);
        fw.close();
        pw.close();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        System.err.println("创建文件时,出错!");
        return false;
    }
}

//3、删除空文件夹

public static boolean deleteFolder(String floderPath){
    try {
        File delFloder = new File(floderPath);
        delFloder.delete();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println("删除文件夹时,出错!");
        return false;
    }

}

//4、删除指定文件

public static boolean deleteFile(String filePath){
    try {
        File delFlie = new File(filePath);
        delFlie.delete();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println("删除文件时,出错!");
        return false;
    }
}

//5、删除某文件夹下的所有空文件夹

public static boolean deleteFloderHasContent(String floderPath){
    try {
        File delFloderC = new File(floderPath);
        File[] files = delFloderC.listFiles();
        System.err.println(delFloderC.length());
        for (int i = 0; i < files.length; i++) {
            if(files[i].isDirectory()){
                files[i].delete();
            }
        }
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println("删除有内容的文件夹时,出错!");
        return false;
    }
}

//6、记事本写入文件

public static boolean readFile(String sourcePath, String content){
    try {
        File file = new File(sourcePath);
        FileWriter fw = new FileWriter(file);
        fw.write(content);
        fw.flush();
        fw.close();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println("写入文件时,出错!");
        return false;
    }
}

//7、读取文件属性

public static void readFileMsg(String sourcePath){
    File file = new File(sourcePath);
    if(file.exists()){
        System.err.println("文件名称" + file.getName());
        System.err.println(file.isFile()?"文件":"不是文件");
        System.err.println(file.isDirectory()?"文件夹":"非文件夹");
        System.err.println(file.canRead()?"可读取":"不可读取");
        System.err.println(file.canWrite()?"是隐藏文件":"不是隐藏文件");
        System.err.println("最终修改时间"+new Date(file.lastModified()));
        System.err.println(file.canExecute()?"是执行文件":"不是执行文件");
        System.err.println("文件大小"+file.getTotalSpace());
    }
}

//8、复制文件夹

public static boolean copyFloder(String sourcePath, String totalPath){
    File file = new File(sourcePath);
    String[] fileLst = file.list();
    File tarFile = new File(totalPath);
    if(!tarFile.exists()){//创建,目标复制文件夹
        tarFile.mkdir();
    }
    for (int i = 0; i < fileLst.length; i++) {
        if((new File(sourcePath + file.separator + fileLst[i])).isDirectory()){//若文件夹中包含文件夹,则递归调用
            copyFloder(sourcePath + file.separator + fileLst[i], totalPath + file.separator + fileLst[i]);
        }
        if((new File(sourcePath + file.separator + fileLst[i])).isFile()){//文件夹内的文件
            System.err.println(sourcePath + file.separator + fileLst[i]);
            copyFile(sourcePath + file.separator + fileLst[i], totalPath + file.separator + fileLst[i]);
        }
    }
    return false;
}

//9、复制文件

public static boolean copyFile(String sourcePath, String targetPath){
    try {
        File file = new File(sourcePath);
        int byteread = 0;
        if(file.exists()){
            FileInputStream fi = new FileInputStream(file);//读取源文件
            FileOutputStream fo = new FileOutputStream(new File(targetPath));//写入目标文件
            byte[] buffer = new byte[1024];
            while ((byteread = fi.read(buffer)) != -1) {
                fo.write(buffer, 0, byteread);
            }
            fi.close();
            fo.close();
        }
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println("文件复制时,出错!");
        return false;
    }
}

二、测试文件夹的复制

我喜欢音乐,因此就拿这个文件夹做测试用例吧

String floderPath = "D:\\red_ant_file\\20181106\\KGMusic";
        String totalPath = "D:\\red_ant_file\\20181106\\KGMusicCopy";
        AllServiceIsHere.copyFloder(floderPath, totalPath);

Java操作文件常用的9种方法

Java操作文件常用的9种方法

运行后

Java操作文件常用的9种方法

Java操作文件常用的9种方法

Java操作文件常用的9种方法

完工!

转载于:https://blog.51cto.com/13479739/2313637

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值