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用户, ...
随机推荐
- Runner站立会议06
开会时间:21.10~21.30 地点:基教负一 今天做了什么:日历布局,晚善日历 明天准备做什么:完善日历界面 遇到的困难:暂无 燃尽图: 会议图:
- os模块之popen
想查看当前目录下有哪些东西,可以使用os.popen()方法,代码如下: t = (os.popen("dir")) print(t.read()) #运行结果 C:\python ...
- json跨域
很有意思的两种连接 ,效果相同. 不同之处: aehyok({"result":"我是远程js带来的数据"}); <script type="t ...
- 【9-15】python学习笔记01
使用#开启行注释: 命令行:使用ctrl+d 退出
- zend studio 的注册码-php的编辑器
zend studio 12.5 patch包: 组织名(倒过来写)+ jar包名称 : com.zend.verifier-xxx.jar 将破解包中的jar包 覆盖原来就有的那个 verifier ...
- python 运行时报错误SyntaxError: Non-ASCII character '\xe5' in file 1.py on line 2
File "1.py", line 2SyntaxError: Non-ASCII character '\xe5' in file 1.py on line 2, but no ...
- (准备写)URAL1824 Ifrit Bomber 题解
http://acm.timus.ru/problem.aspx?space=1&num=1824 1824. Ifrit Bomber Time limit: 0.5 second Memo ...
- HTML5+CSS3+jquery实现简单的音乐播放器
...最近天热的,感觉就像煎饼...然后别人在把妹子的时候,只有偶们这帮苦逼的程序员在那边撸代码...我日哦! 然后今天晒的是偶早年写的一个播放器...看上去是不是很有感觉的样子!一番宝物,Lisa唱 ...
- 谈谈我眼中的CSDN吧
昨天逛博客园看到了这篇曝光率很高的文章:博客搬家——从CSDN到博客园,一篇短短的文章竟然招致这么多人的讨论,可能程序员就喜欢“Java好还是PHP好”这类型的问题吧,好无聊.由于我一直在使用CSDN ...
- AngularJS API之equal比较对象
使用情况 1 首先,所有满足 a === 3 这种的对象,在angular.equals(a,b)中都会返回真 2 所有对象的类型,以及属性值都相同的,也会返回真 3 NaN和NaN也会返回真(在ja ...