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用户, ...
随机推荐
- Java数据结构——优先级队列
//================================================= // File Name : PriorityQueue_demo //------------ ...
- Android学习笔记——SQLite
该工程的功能是实现关于数据库的操作,即creat.update.insert.query.delete 调试的时候请用模拟器,用真机调试的时候进入cmd-adb shell,再进入cd data/da ...
- C++中引用与指针的区别(详细介绍)
C++中引用与指针的区别(详细介绍) C++中的引用与指针的区别 指向不同类型的指针的区别在于指针类型可以知道编译器解释某个特定地址(指针指向的地址)中的内存内容及大小,而void*指针则只表示一 ...
- UITextField限制输入文字
一.viewDidLoad时监听通知 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [center addO ...
- .net mvc web api 返回 json 内容,过滤值为null的属性
原文:http://blog.csdn.net/xxj_jing/article/details/49508557 版权声明:本文为博主原创文章,未经博主允许不得转载. .net mvc web ap ...
- [问题] UISearchBar 点击取消后跳动的问题
问题详情: 首先是TableView 作为 NavigationController 的 RootViewContrller, 然后UISearchBar 添加到TableView 的 headV ...
- 一些分享微博,qq啥的js
<div class="bdsharebuttonbox" style="float:right"><a href="#" ...
- [c#]RabbitMQ的简单使用
摘要 Message Queue消息队列,简称MQ,是一种应用程序对应用程序的通信方法,应用程序通过读写出入队列的消息来通信,而无需专用连接来链接它们.消息传递指的是程序之间通过在消息中发送数据进行通 ...
- 该不该用inline-block取代float? inline和float的区别?
该不该用inline-block取代float? 请看这篇文章引用: jtyjty99999的博客 让块级元素 水平排列的通常方式是float, 但是float可能会带来很多意外的问题 可以考虑用in ...
- AjaxAnywhere+struts用法
AjaxAnywhere的用法 1,简介 AjaxAnywhere被设计成能够把任何一套现存的JSP组件转换成AJAX感知组件而不需要复杂的JavaScript编码.它利用标签把Web页面简单地划分成 ...