文件读写通用工具类

package com.at21.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;

/**
* <p>Title:FileOperate.java</p>
* <p>Description: </p>
* <p>Copyright: CopyRight (c) 2017 </p>
* <p>Company: 21at</p>
* @author 林剑斌
* @createdate 2017年7月27日下午1:13:16
* @version 1.0
*/
public class FileOperate {
/**
* <p>功能描述: 清空文件内容</p>
* <p>输入参数: </p>
* <p>返回结果: </p>
* <p>时间:2017-09-24下午7:07:08</p>
* @author 林剑斌
* @version 1.0
*/
public static void clearInfoForFile(String fileName) {
File file =new File(fileName);
try {
if(!file.exists()) {
file.createNewFile();
}
FileWriter fileWriter =new FileWriter(file);
fileWriter.write("");
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*@description 判断文件是否存在
*@parameter
*@return
*@throws
*@date 2017年9月21日上午10:10:16
*@version 1.0
*@author linjb
*/
public static boolean isExist(String srcPath){
boolean flag=false;
File file=new File(srcPath);
if(file.exists()){
flag=true;
}
return flag;
}
/**
*@description 拷贝文件或文件夹
*@parameter srcPath原复制文件路径,destDir目标路径
*@date 2017年7月27日下午3:22:21
*@version 1.0
*@author linjb
*/
public static boolean copyGeneralFile(String srcPath,String destDir){
boolean flag=false;
File file=new File(srcPath);
if(!file.exists()){
//throw new RuntimeException("未找到指定文件:"+srcPath);
return flag;
}
if(file.isFile()){
flag=copyFile(srcPath,destDir);
}else if(file.isDirectory()){
flag=copyDirectory(srcPath, destDir);
}
return flag;
}
/**
*@description 拷贝文件
*@parameter srcPath原复制文件路径,destDir目标路径
*@date 2017年7月27日下午1:47:15
*@version 1.0
*@author linjb
*/
public static boolean copyFile(String srcPath,String destDir){
boolean flag=false;
File srcFile=new File(srcPath);
//判断源文件是否存在
if(!srcFile.exists()){
//throw new RuntimeException("源文件不存在:"+srcPath);
return flag;
}
String fileName=srcPath.substring(srcPath.lastIndexOf(File.separator));
//目标路径+文件名
String destPath=destDir+fileName;
if(destPath.equals(srcPath)){
flag=true;
return flag;
}
File destFile=new File(destPath);
//是否有同名文件
if(destFile.exists()){
//throw new RuntimeException("目标文件夹中存在同名文件:"+destPath);
return flag;
}
//目标路径
File destFileDir=new File(destDir);
//目标路径不是文件夹
if(destFileDir.isFile()){
//throw new RuntimeException("目标路径不是文件夹:"+destDir);
return flag;
}
if(!destFileDir.exists()){
destFileDir.mkdirs();
}
try {
FileInputStream fis=new FileInputStream(srcPath);
FileOutputStream fos=new FileOutputStream(destFile);
byte[] buf=new byte[1024];
int c;
while((c=fis.read(buf))!=-1){
fos.write(buf,0,c);
}
fis.close();
fos.close();
flag=true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
public static boolean copyDirectory(String srcPath,String destDir){
boolean flag=false;
File srcFile=new File(srcPath);
if(!srcFile.exists()){
return flag;
}
//获取待复制的文件夹的名称
String dirName=getDirName(srcPath);
//目标文件夹的完整路径
String destPath=destDir+File.separator+dirName;
if(destPath.equals("srcPath")){
flag=true;
return flag;
}
File destDirFile=new File(destPath);
if(destDirFile.exists()){
return flag;
}
destDirFile.mkdirs();
File[] fileList=srcFile.listFiles();
if(fileList.length==0){
flag=true;
return flag;
}else{
for(File temp:fileList){
if(temp.isFile()){
flag=copyFile(temp.getAbsolutePath(),destPath);
}else if(temp.isDirectory()){
flag=copyDirectory(temp.getAbsolutePath(), destPath);
}
if(!flag){
break;
}
}
}
return flag;
}
/**
*@description 获取待复制文件夹的文件名
*@date 2017年7月27日下午3:54:50
*@version 1.0
*@author linjb
*/
public static String getDirName(String dir){
if(dir.endsWith(File.separator)){
//如果文件夹路径以“//”结尾,则先去除末尾的“//”
dir=dir.substring(0,dir.lastIndexOf(File.separator));
}
return dir.substring(dir.lastIndexOf(File.separator)+1);
}
/**
*@description 删除文件或文件夹
*@parameter 要删除的文件或文件夹的路径
*@date 2017年7月27日下午3:28:54
*@version 1.0
*@author linjb
*/
public static boolean deleteGeneralFile(String path){
boolean flag=false;
File file=new File(path);
if(!file.exists()){
return flag;
}
if(file.isDirectory()){
//删除目录操作
flag=deleteDirectory(file.getAbsolutePath());
}else if(file.isFile()){
//删除文件操作
flag=deleteFile(file);
}
return flag;
}
/**
*@description 删除文件
*@date 2017年7月27日下午3:31:48
*@version 1.0
*@author linjb
*/
public static boolean deleteFile(File file){
return file.delete();
}
/**
*@description 删除文件夹
*@date 2017年7月27日下午3:42:34
*@version 1.0
*@author linjb
*/
public static boolean deleteDirectory(String path){
boolean flag=false;
File dirFile=new File(path);
if(!dirFile.isDirectory()){
return flag;
}
File[] files=dirFile.listFiles();
for(File file:files){
if(file.isFile()){
flag=deleteFile(file);
}else if(file.isDirectory()){
flag=deleteDirectory(file.getAbsolutePath());
}
if(!flag){
break;
}
}
flag=dirFile.delete();
return flag;
}
public static boolean cutGeneralFile(String srcPath,String destDir){
if(!copyGeneralFile(srcPath, destDir)){
return false;
}
if(!deleteGeneralFile(srcPath)){
return false;
}
return true;
}
public static void main(String[] args) {
// copyFile("D:\\Downloads\\1.tar.gz","D:\\move");
// System.out.println(isExist("D:\\Downloa"));
// clearInfoForFile("D:\\aria2\\aria2.session");
}

}

猜你喜欢

转载自blog.csdn.net/qq_26629277/article/details/78080933