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用户, ...
随机推荐
- INADDR_ANY
INADDR_ANY就是指定地址为0.0.0.0的地址,这个地址事实上表示不确定地址,或“所有地址”.“任意地址”. 一般来说,在各个系统中均定义成为0值. 外文名 INADDR_ANY 别 名 所有 ...
- ArcGIS Server 10.1 for Linux典型问题总结
关闭开启server服务: [gis@localhost ArcGISServer]$ cd /home/gis/arcgis/server [gis@localhost server]$ ./sto ...
- python开发_++i,i += 1的区分
python开发_++i,i += 1的区分 在很多编程语言(C/C++,Java等)中我们都会碰到这样的语法: 1 int i = 0; 2 ++ i; // -- i; 这样的语法在上述编程语言中 ...
- Cloudservice程序设置Idle timeout
部署的云服务程序,默认的idle timeout是4分钟,意味着如果你通过一个workerrole发布了wcf服务,客户端第一次调用服务方法后,再过4分钟尝试去重新调用服务,会报错,具体测试如下: 1 ...
- ecshop 点击领取红包
思路: 1.领取红包:也就是传递一个红包ID,和user_id然后update更新过来 2.获取未领取过的红包列表:然后随机函数array_rand($arr,1),随机返回一个数组,从而获得红包列表 ...
- ecshop订单-》待付款,待发货,待收货,收货确认
// 订单 待付款.待发货.待收货.确认收货 public function get_serch_order($type/*,$limit_statrt,$limit_end,$serch*/){ $ ...
- iOS7以上图片模糊效果
模糊后的效果 模糊后的效果 框架UIImage+BlurredFrame里的 applyLightEffectAtFrame方法 例如 bgImage = [bgImage applyLightEff ...
- ElasticSearch之二——集群
ElasticSearch 集群 首先看下ElasticSearch(ES)的架构: 术语解释: cluster:代表一个集群,集群中有多个节点,其中有一个master节点,master通过选举自动产 ...
- java 获取文件的最后编辑时间
还是日志的问题,需要把日志文件的一些信息给显示出来,其中就需要显示最后的编辑时间,在网上找的答案... File f = new File(path); SimpleDateFormat sdf = ...
- oarcle数据库导入导出,创建表空间
oracle导入导出(前提,本机已经安装oracle和oracle客户端)导出命令: exp username/password@192.168.1.138/ORCL file=c:\test.dmp ...