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用户, ...
随机推荐
- k-nearest-neighbor算法
1. kNN 1.1 基本的kNN模型 kNN(k-nearest neighbor)的思想简单来说就是,要评价一个未知的东西U,只需找k个与U相似的已知的东西,并通过k个已知的,对U进行评价.假如要 ...
- windows 修改hosts 脚本
@echo off echo "请注意你的杀毒软件提示,一定要允许" echo. & pause @del C:\Windows\System32\drivers\etc\ ...
- 英文分词算法(Porter stemmer)
http://blog.csdn.net/whuslei/article/details/7398443 最近需要对英文进行分词处理,希望能够实现还原英文单词原型,比如 boys 变为 boy 等. ...
- ubuntu下eclipse遇到The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
JSP页面顶端出现“红色”的报错信息:The superclass "javax.servlet.http.HttpServlet" was not found on the Ja ...
- 第5章 jQuery对表单、表格的操作及更多应用
本章主要是对前面4章的小结和应用. 一. 表单form应用 表单分为3个组成部分 (1)form标签 表单可包含文本域,复选框,单选按钮等等.表单用于向指定的 URL 传递用户数据. (2)表单域 - ...
- C#----使用WindowsMediaPlayer 同时播放多个声音
使用Windows Media Player 其实就是使用组件AxWindowsMediaPlayer. 添加两个引用:Interop.WMPLib.dll和AxInterop.WMPLib.dll. ...
- php 下载文件的函数
通过函数完成下载文件的PHP功能代码 function download($url, $filename) { // 获得文件大小, 防止超过2G的文件, 用sprintf来读 $filesize = ...
- WinForm使用皮肤图文步骤
Winfrom本身样式提供的是Windows经典样式.. 不说多丑也绝称不上好看..有时为了用户体验就不得不需要想办法弄漂亮一点..皮肤包会是一个不错的选择.. 不废话了..开整.. 首先从网上下载免 ...
- Yii 框架里数据库操作详解-[增加、查询、更新、删除的方法 'AR模式']
public function getMinLimit () { $sql = "..."; $result = yii::app()->db-& ...
- angularjs 过滤器详解
https://segmentfault.com/a/1190000002758481 app.controller('testC',function($scope,$filter){ $sc ...