File文件管理

/**
 * 实现文件和文件夹管理.
 */
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


import com.fahon.framework.exception.ServiceException;


/**
 * 实现文件和文件夹管理.<br>
 * 处理文件拷贝、文件夹拷贝、删除整个目录、强制删除指定目录、删除文件<br>
 * 得到无后缀名的文件名、得到文件后缀名.
 * 
 * 
 */
public class FileHelper {


/**
* 日期
* 版本:
* 描述:createDirectiory(创建目录.) <br>
* 主要方法:
* @param path
*            void
* @Exception 异常对象 <br>
*/
public static void createDirectiory(String path) {
try {
File folder = new File(path);
if (folder == null)
return;
if (folder.isFile())
return;// 目标不是文件夹
if (!folder.exists()) {
folder.mkdirs();
}
} catch (Exception e) {


}
}


/**
* 文件夹拷贝.<br>
* 将目标文件夹下所有的目录和文件全部拷贝到.

* @param sourcePath
*            源文件夹
* @param targetPath
*            目标文件夹
* @throws IOException
*             抛出输出异常
* @throws ServiceException
*/
public static void copyDirectiory(String sourcePath, String targetPath)
throws IOException, ServiceException {
File sourceFileD = new File(sourcePath);
if (sourceFileD.exists() && sourceFileD.canRead()
&& sourceFileD.isDirectory()) {
(new File(targetPath)).mkdirs();
File[] file = (new File(sourcePath)).listFiles();
FileInputStream input = null;
FileOutputStream output = null;
for (int i = 0; i < file.length; i++) {
if (file[i].isFile()) {
try {
input = new FileInputStream(file[i]);
output = new FileOutputStream(targetPath + "/"
+ file[i].getName());
byte[] b = new byte[1024 * 5];
int len;
while ((len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
throw new ServiceException(e);
} finally {


if (output != null) {
try {
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (input != null) {
try {
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
if (file[i].isDirectory()) {
copyDirectiory(sourcePath + "/" + file[i].getName(),
targetPath + "/" + file[i].getName());
}
}
}
}


/**
* 实现文件拷贝.

* @param sourcePath
*            原文件路径S
* @param newFolder
*            目标文件夹路径
* @throws ServiceException
* @throws IOException
*/
public static File copyFile(String sourcePath, String newFolder)
throws ServiceException {
File f = new File(sourcePath);
if (!f.exists())
return null;// 不存在返回失败
if (f.isDirectory())
return null;// 文件夹返回失败


File folder = new File(newFolder);
if (folder == null)
return null;
// if(folder.isFile())return null;//目标不是文件夹
if (!folder.exists()) {
folder.mkdirs();
}
FileOutputStream output = null;
FileInputStream in = null;
File pathToSave = null;
try {
in = new FileInputStream(f);
String fileName = f.getName();
String nameWithoutExt = getNameWithoutExtension(fileName);
String ext = getExtension(fileName);
pathToSave = new File(newFolder, fileName);
// String newName ="";
int counter = 1;
while (pathToSave.exists()) {
fileName = nameWithoutExt + "(" + counter + ")" + "." + ext;
pathToSave = new File(newFolder, fileName);
counter++;
}
output = new FileOutputStream(pathToSave);
byte[] b = new byte[1024 * 5];
int len;
while ((len = in.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
in.close();
in = null;
output = null;


} catch (Exception e) {
throw new ServiceException(e);
} finally {


if (output != null) {
try {
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return pathToSave;
}


/**
* 删除目录(文件夹)以及目录下的文件.

* @param sPath
*            被删除目录的文件路径
* @return 目录删除成功返回true,否则返回false
*/
public static boolean deleteDirectory(String sPath) {
// 如果sPath不以文件分隔符结尾,自动添加文件分隔符
if (!sPath.endsWith(File.separator)) {
sPath = sPath + File.separator;
}
File dirFile = new File(sPath);
// 如果dir对应的文件不存在,或者不是一个目录,则退出
if (!dirFile.exists() || !dirFile.isDirectory()) {
return true;
}
boolean flag = true;
// 删除文件夹下的所有文件(包括子目录)
File[] files = dirFile.listFiles();
for (int i = 0; i < files.length; i++) {
// 删除子文件
if (files[i].isFile()) {
flag = deleteFile(files[i].getAbsolutePath());
if (!flag)
break;
} // 删除子目录
else {
flag = deleteDirectory(files[i].getAbsolutePath());
if (!flag)
break;
}
}
if (!flag)
return false;
// 删除当前目录
if (dirFile.delete()) {
return true;
} else {
return false;
}
}


/**
* 删除单个文件.

* @param sPath
*            被删除文件的文件名
* @return 单个文件删除成功返回true,否则返回false
*/
public static boolean deleteFile(String sPath) {
boolean flag = false;
File file = new File(sPath);
// 路径为文件且不为空则进行删除
if (file.isFile() && file.exists()) {
file.delete();
flag = true;
}
return flag;
}


/**
* 根据路径删除指定的目录或文件,无论存在与否.

* @param sPath
*            要删除的目录或文件
* @return 删除成功返回 true,否则返回 false。
*/
public static boolean DeleteFolder(String sPath) {
boolean flag = false;
File file = new File(sPath);
// 判断目录或文件是否存在
if (!file.exists()) { // 不存在返回 false
return flag;
} else {
// 判断是否为文件
if (file.isFile()) { // 为文件时调用删除文件方法
return deleteFile(sPath);
} else { // 为目录时调用删除目录方法
return deleteDirectory(sPath);
}
}
}


/**
* 获得文件的后缀名.<br>
* 例如 test.doc 使用该方法可获得后缀名"doc".

* @param fileName
*            文件全名
* @return 返回文件后缀名
*/
public static String getExtension(String fileName) {
int i = fileName.lastIndexOf(".");
if (i < 0)
return "";
return fileName.substring(fileName.lastIndexOf(".") + 1);
}


/**
* 获得文件名,该文件名不存在有后缀名.<br>
* 例如 test.doc使用该方法可获得文件名"test".

* @param fileName
*            文件全名
* @return 返回不含有后缀名的文件名
*/
public static String getNameWithoutExtension(String fileName) {
int i = fileName.lastIndexOf(".");
if (i < 0)
return fileName;
return fileName.substring(0, fileName.lastIndexOf("."));
}


/**
* 写文件.

* @param data
*            byte数据
* @param filePath
*            写文件目录
* @param fileName
*            具体文件名
* @throws IOException
*             抛出输出异常
*/
public static void writeToFile(byte[] data, String filePath, String fileName)
throws IOException {
// 如果filePath不以文件分隔符结尾,自动添加文件分隔符
if (!filePath.endsWith(File.separator)) {
filePath = filePath + File.separator;
}
// 如何文件夹不存,则创建一个
File f = new File(filePath);
if (!f.exists()) {
f.mkdir();
}
// 写入文件
FileOutputStream output = null;
byte[] b = data;
try {
output = new FileOutputStream(filePath + fileName);
output.write(b);
output.flush();
output.close();
} finally {


if (output != null) {
output.close();
}
}
}


/**
* 日期:
* 版本:
* 描述:readFile(根据文件地址读文件.) <br>
* 主要方法

* @param filepath
* @return String
* @Exception 异常对象 <br>
*/
public static String readFile(String filepath) {
InputStreamReader reader = null;
BufferedReader in = null;
String html = "";
InputStream content = null;
try {
content = new FileInputStream(filepath);
reader = new InputStreamReader(content);
in = new BufferedReader(reader);
String jsonString = "";
while ((jsonString = in.readLine()) != null) {
html += jsonString;
}
} catch (Exception e) {


} finally {
if (null != reader) {
try {
reader.close();
} catch (IOException e) {


}
}
if (null != in) {
try {
in.close();
} catch (IOException e) {
}
}
if (null != content) {
try {
content.close();
} catch (IOException e) {


}
}


}
return html;


}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值