当前位置: 首页 > news >正文

mkdirs与createNewFile区别,Java复制文件工具类

mkdirs 与 createNewFile

mkdirs: 创建【多级目录文件夹】
mkdir:创建【单级目录文件夹】,如果上级文件夹不存在则报错
createNewFile:创建【文件】(如.txt)

例:桌面新建file1/file2/file3文件夹,并在里面新建test1.txt文件
String separator = File.separator;//分割符
System.out.println("separator==========" + separator);//桌面新建file1/file2/file3文件夹,并在里面新建test1.txt文件
String dir = "C:\\Users\\Administrator\\Desktop\\file1\\file2\\file3";
String fileName = "test1.txt";
File dirFile = new File(dir);
if (!dirFile.exists()) {System.out.println("创建文件夹");dirFile.mkdirs();//父级目录不存在可以创建,且只能创建文件夹
//            dirFile.mkdir();//父级目录不存在异常
} else {System.out.println("文件夹已存在");
}File file = new File(dir, fileName);
if (!file.exists()) {System.out.println("创建文件");try {file.createNewFile();} catch (IOException e) {e.printStackTrace();}
} else {System.out.println("文件已存在");
}boolean isDirectory = dirFile.isDirectory();
boolean isFile = file.isFile();
System.out.println("isDirectory========" + isDirectory);
System.out.println("isFile==========" + isFile);

复制文件工具类(四种方法)


import org.apache.commons.io.FileUtils;import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.file.Files;public class CopyUtil {/** 1. 使用FileStreams复制* 这是最经典的方式将一个文件的内容复制到另一个文件中。* 使用FileInputStream读取文件A的字节,使用FileOutputStream写入到文件B。* */public static void copyFileUsingFileStreams(File source, File dest)throws IOException {InputStream input = null;OutputStream output = null;try {input = new FileInputStream(source);output = new FileOutputStream(dest);byte[] buf = new byte[1024];int bytesRead;while ((bytesRead = input.read(buf)) > 0) {output.write(buf, 0, bytesRead);}} finally {if (input != null) {input.close();}if (output != null) {output.close();}}}/** 2. 使用FileChannel复制* Java NIO包括transferFrom方法,根据文档应该比文件流复制的速度更快。* */public static void copyFileUsingFileChannels(File source, File dest) throws IOException {FileChannel inputChannel = null;FileChannel outputChannel = null;try {inputChannel = new FileInputStream(source).getChannel();outputChannel = new FileOutputStream(dest).getChannel();outputChannel.transferFrom(inputChannel, 0, inputChannel.size());} finally {if (inputChannel != null) {inputChannel.close();}if (outputChannel != null) {outputChannel.close();}}}/** 3. 使用Commons IO复制* //<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->*   //<dependency>*    //    <groupId>commons-io</groupId>*   //    <artifactId>commons-io</artifactId>*   //    <version>2.6</version>*  //</dependency>* */public static void copyFileUsingApacheCommonsIO(File source, File dest)throws IOException {FileUtils.copyFile(source, dest);}/** 4. 使用Java7的Files类复制* */public static void copyFileUsingJava7Files(File source, File dest)throws IOException {Files.copy(source.toPath(), dest.toPath());}
}
例:将jar和aar拷贝到指定文件夹中
public class TestFile {private static String source = "C:\\Users\\Administrator\\.gradle\\caches\\modules-2\\files-2.1";private static String targer = "C:\\Users\\Administrator\\Desktop\\libss2222";public static void main(String[] args) {check(targer);copy(source, targer);}private static void check(String targer) {File dirFile = new File(targer);if (!dirFile.exists()) {System.out.println("创建文件夹");dirFile.mkdirs();//父级目录不存在可以创建
//            dirFile.mkdir();//父级目录不存在异常} else {System.out.println("文件夹已存在");}}public static void copy(String source, String targer) {File dir = new File(source);File[] files = dir.listFiles(); // 该文件目录下文件全部放入数组if (files != null) {for (File file : files) {if (file.isDirectory()) { // 判断是文件还是文件夹copy(file.getAbsolutePath(), targer); // 获取文件绝对路径} else {if (file.getAbsolutePath().endsWith(".jar")  || file.getAbsolutePath().endsWith(".aar")) {try {//java.io.FileNotFoundException: //C:\Users\Administrator\Desktop\libss2222 (拒绝访问。)//需要在目标文件夹指定要复制的文件名System.out.println("name==============================" + file.getName());CopyUtil.copyFileUsingFileStreams(file, new File(targer, file.getName()));} catch (IOException e) {e.printStackTrace();}}}}}}
}
http://www.xdnf.cn/news/813187.html

相关文章:

  • 素数环
  • 微信小程序-wxml和wxss样式
  • Linux常用命令_(文件权限)
  • [笔记]vs2015 编写汇编masm32之使用MASM32库
  • 天龙八部万象归一第8版单机安装教程+GM工具+虚拟机一键端
  • Ant下载及配置安装
  • 这回,数据库事务4种隔离级别及7种传播行为,终于说明清楚了
  • 机器学习实战(入门级) ------ Kaggle 泰坦尼克号幸存者预测 (随机森林,KNN,SVM)
  • 因 Cannot resolve com.lowagie:itext:2.1.7.js6,选择手动安装 Jar 包进 Maven 仓库
  • linux:账号管理
  • C语言:从零基础到精通—轻松写出第一个C语言程序
  • QuickTime专业版 pro 注册码
  • Windows 2000 Server系统下载!全套镜像下载!
  • 简单的鱼群算法实现
  • 数据采集网关的功能和应用场景
  • 用户'NT AUTHORITY/NETWORK SERVICE' 登录失败解决办法
  • adb shell 命令详解
  • 大学课程 | 《微机原理与接口技术》知识点总结
  • RadHat搭建内网YUM源服务器
  • 网络优化的实践: 如何优化网络架构设计
  • 如何扩容C盘?6种扩展C盘方法!
  • 【分享】School Rumble校园迷糊大王PSP姐姐事件+PS2游戏第一,二学期【带VNR翻译教程】...
  • ActionListener的用法
  • 第一章 1.The Basic (CCNA)
  • 最新海康摄像机、NVR、流媒体服务器、回放取流RTSP地址规则说明
  • [转]缓冲区溢出攻击(含示例)
  • Google原生输入法LatinIME词库构建流程分析(二)
  • 硬件开发笔记(二十六):AD21导入电感原理图库、封装库和3D模型
  • BT下载原理简介
  • android 五大应用开发框架,2024年最新html5移动开发即学即用网盘