2022年Java文件剪切 .pdf
《2022年Java文件剪切 .pdf》由会员分享,可在线阅读,更多相关《2022年Java文件剪切 .pdf(7页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、packagecom.file;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;/* Java实现文件复制、剪切、删除操作* 文件指文件或文件夹* 文件分割符统一用/*/public classFileOperateDemo /* 复制文件或文件夹* param srcPath* param destDir*目标文件所在的目录* return*/public static boolean copyGeneralFile(St
2、ring srcPath,String destDir) boolean flag = false;File file = new File(srcPath);if (!file.exists() System.out.println( 源文件或源文件夹不存在!);return false;if (file.isFile() / 源文件System.out.println( 下面进行文件复制!);flag = copyFile(srcPath, destDir); elseif (file.isDirectory()System.out.println( 下面进行文件夹复制!);flag =
3、copyDirectory(srcPath, destDir);return flag;/* 复制文件*名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 7 页 - - - - - - - - - * param srcPath*源文件绝对路径* param destDir*目标文件所在目录* return boolean*/private static boolean copyFile(String srcPath, String destDir) boolean fla
4、g = false;File srcFile = new File(srcPath);if (!srcFile.exists() / 源文件不存在System.out.println( 源文件不存在 );return false;/ 获取待复制文件的文件名String fileName = srcPath.substring(srcPath.lastIndexOf(File.separator);String destPath= destDir + fileName;if (destPath.equals(srcPath) / 源文件路径和目标文件路径重复System.out.println(
5、 源文件路径和目标文件路径重复!);return false;File destFile = new File(destPath);if (destFile.exists() & destFile.isFile() / 该路径下已经有一个同名文件System.out.println( 目标目录下已有同名文件!);return false;File destFileDir = new File(destDir);destFileDir.mkdirs();try FileInputStream fis = new FileInputStream(srcPath);FileOutputStream
6、fos = new FileOutputStream(destFile);byte buf = new byte1024;int c;while (c = fis.read(buf) != -1) fos.write(buf, 0, c);fis.close();fos.close();flag = true; catch (IOException e) /名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 7 页 - - - - - - - - - if (flag) Sy
7、stem.out.println( 复制文件成功 !);return flag;/* param srcPath*源文件夹路径* param destPath*目标文件夹所在目录* return*/private static boolean copyDirectory(StringsrcPath, String destDir) System.out.println( 复制文件夹开始!);boolean flag = false;File srcFile = new File(srcPath);if (!srcFile.exists() / 源文件夹不存在System.out.println
8、( 源文件夹不存在);return false;/ 获得待复制的文件夹的名字,比如待复制的文件夹为E:/dir 则获取的名字为dirString dirName = getDirName(srcPath);/ 目标文件夹的完整路径String destPath= destDir + File.separator + dirName;/ System.out.println( 目标文件夹的完整路径为: + destPath);if (destPath.equals(srcPath)System.out.println( 目标文件夹与源文件夹重复);return false;File destDi
9、rFile = new File(destPath);if (destDirFile.exists() / 目标位置有一个同名文件夹System.out.println( 目标位置已有同名文件夹!);return false;destDirFile.mkdirs();/ 生成目录名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 7 页 - - - - - - - - - File fileList = srcFile.listFiles(); / 获取源文件夹下的子文件和子
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年Java文件剪切 2022 Java 文件 剪切
限制150内