Java文件操作工具类(复制、删除、重命名、创建路径)
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 FileControl { // 文件复制
public static boolean copyFile(String source, String copy) throws Exception {
source = source.replace("\\", "/");
copy = copy.replace("\\", "/"); File source_file = new File(source);
File copy_file = new File(copy); // BufferedStream缓冲字节流 if (!source_file.exists()) {
throw new IOException("文件复制失败:源文件(" + source_file + ") 不存在");
}
if (copy_file.isDirectory()) {
throw new IOException("文件复制失败:复制路径(" + copy_file + ") 错误");
}
File parent = copy_file.getParentFile();
// 创建复制路径
if (!parent.exists()) {
parent.mkdirs();
}
// 创建复制文件
if (!copy_file.exists()) {
copy_file.createNewFile();
} FileInputStream fis = new FileInputStream(source_file);
FileOutputStream fos = new FileOutputStream(copy_file); BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos); byte[] KB = new byte[1024];
int index;
while ((index = bis.read(KB)) != -1) {
bos.write(KB, 0, index);
} bos.close();
bis.close();
fos.close();
fis.close(); if (!copy_file.exists()) {
return false;
} else if (source_file.length() != copy_file.length()) {
return false;
} else {
return true;
} } // 文件重命名
public static boolean renameFile(String url, String new_name) throws Exception {
String old_url = url;
old_url = old_url.replace("\\", "/");
File old_file = new File(old_url);
if (!old_file.exists()) {
throw new IOException("文件重命名失败,文件("+old_file+")不存在");
}
System.out.println(old_file.exists()); String old_name = old_file.getName();
// 获得父路径
String parent = old_file.getParent();
// 重命名
String new_url = parent + "/" + new_name;
File new_file = new File(new_url);
old_file.renameTo(new_file); System.out.println("原文件:" + old_file.getName());
System.out.println("新文件:" + new_file.getName());
new_name = new_file.getName();
old_name = old_file.getName();
if (new_name.equals(old_name)) {
return false;
} else {
return true;
} } // 文件删除
public static boolean deleteFile(String url) throws Exception {
url = url.replace("\\", "/");
File file = new File(url); if (file.isFile()) {
if (file.exists()) {
file.delete();
}
}else{
throw new IOException("文件删除失败:("+file+")错误");
}
if (file.exists()) {
return false;
} else {
return true;
}
} // 创建文件夹
public static boolean createPath(String url) throws Exception {
url = url.replace("\\", "/");
File folder = new File(url);
if(!folder.isDirectory()){
throw new IOException("创建文件夹失败:("+folder+")不是文件夹路径");
} if (!folder.isFile()) {
if (!folder.exists()) {
folder.mkdirs();
}
}
// 检测是否创建成功
if (folder.isDirectory() && folder.exists()) {
return true;
} else {
return false;
} } }
Java文件操作工具类(复制、删除、重命名、创建路径)的更多相关文章
- JAVA文件操作工具类(读、增、删除、复制)
使用JAVA的JFinal框架 1.上传文件模型类UploadFile /** * Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com). * ...
- Java文件操作工具类
import com.foriseland.fjf.lang.DateUtil;import org.apache.commons.io.FileUtils;import org.slf4j.Logg ...
- 文件操作工具类: 文件/目录的创建、删除、移动、复制、zip压缩与解压.
FileOperationUtils.java package com.xnl.utils; import java.io.BufferedInputStream; import java.io.Bu ...
- Python批量复制和重命名文件
Python批量复制和重命名文件 示例代码 #! /usr/bin/env python # coding=utf-8 import os import shutil import time impo ...
- 小米开源文件管理器MiCodeFileExplorer-源码研究(4)-文件操作工具类FileOperationHelper
文件操作是非常通用的,注释都写在源代码中了,不多说~需要特别说明的是,任务的异步执行和IOperationProgressListener.拷贝和删除等操作,是比较费时的,采用了异步执行的方式~ An ...
- Android文件操作工具类(转)
Android文件操作工具类(转) 2014/4/3 18:13:35 孤独的旅行家 博客园 这个工具类包含Android应用开发最基本的几个文件操作方法,也是我第一次发博客与大家分享自己写的东 ...
- (转)Windows重启延迟删除,重命名技术原理
所谓重启延迟删除技术,就是在操作系统启动前删除或者替换文件! 说起重启延迟删除,大家可能都很陌生,但是实际上,该功能已经被各种软件所采用:如安装Windows 补丁程序(如:HotFix.Servic ...
- JAVA文件操作类和文件夹的操作代码示例
JAVA文件操作类和文件夹的操作代码实例,包括读取文本文件内容, 新建目录,多级目录创建,新建文件,有编码方式的文件创建, 删除文件,删除文件夹,删除指定文件夹下所有文件, 复制单个文件,复制整个文件 ...
- docker 部署vsftpd服务、验证及java ftp操作工具类
docker部署vsftpd服务 新建ftp文件存储目录/home/ftp cd /home mkdir ftp 创建一个组,用于存放ftp用户 groupadd ftpgroups 创建ftp用户, ...
随机推荐
- 添加IP安全策略 远离系统Ping漏洞的威胁
懂得网络的人对于Ping这个最基本的网络命令一定很熟悉,它是一个非常好用的TCP/IP工具.它可以向你提供的地址发送一个小的数据包,然后侦听这台机器是否有“回答”.你可以使用机器的 Internet ...
- WinRAR压缩
WinRAR压缩软件: ------------------ 软件官网:http://www.winrar.com.cn/ -------------------------------
- js日期加减
先补充下基础知识: var myDate = new Date(); //myDate默认返回当前时间 myDate.getYear(); //获取当前年份(2位) myDate.getFullYea ...
- 有scp命令,传输文件却显示报错无此命令
今天下午在一台服务器上使用scp命令向另外一台服务器传文件的时候,报此错误 bash: scp: command not found ,lost connection,以为是该服务器没有安装此命令,w ...
- Python + OpenCV2 系列:1 - 配置
Python+OpenCV2+Eclipse+Windos 8.1(32bits): 最初的目的是做图像处理,opencv强大的社区支持,让我想从matlab转到opencv框架下进行试验,而Pyth ...
- PetaPoco 使用总结(一)
PetaPoco 使用总结(一) 前段时间,公司的一个项目希望用一个ORM 的框架,通过对比 Dapper 和 PetaPoco ,虽然Dapper 功能很强大,速度更快. 但是最终还是选择了比较简单 ...
- ] 解决myeclipse中新建javaweb工程,无法使用Web App Libraries问题
] 解决myeclipse中新建javaweb工程,无法使用Web App Libraries问题 标签: myeclipsejavawebWeb App Libraries 2013-10-16 1 ...
- line-height:150%和 line-height:1.5 的区别
父元素line-height为150%或1.5em时,依据父元素的字体大小计算出行高值让子元素继承父元素line-height为1.5时,依据子元素字体大小计算其行高值.
- select2搜索框查询加遍历
<div class="form-group"> <label class="control-label col-sm-1 no-padding-rig ...
- PHP与Javascript的混合测试
js调用php <?php $num=88; ?> <script> var a = <?php echo $num;?>; alert(a); </scri ...