package cn.xx.io.util;
import java.io.*;
import java.util.*;
import javax.servlet.jsp.PageContext;
import com.kingter.common.httpclient.*;
/**
* <p>Title: 文件管理</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2010</p>
* <p>Company: </p>
* @author
* @version 1.0
*/
public class FileManagerService {
public FileManager() {
}
/**
* 创建文件
* @param fileName String 文件名,包含文件路径
* @param buf byte[] 文件内容
* @throws java.lang.Exception 创建文件失败,抛出异常
*/
public void createFile(String fileName, byte[] buf) throws Exception {
String sDir = fileName.substring(0, fileName.lastIndexOf("/"));
//System.out.println("dir == " + sDir);
//创建目录
File dir = new File(sDir);
if (!dir.exists()) {
dir.mkdirs();
}
//创建文件
File file = new File(fileName);
//if(!file.exists()) {
//System.out.println("文件长度 == " + buf.length);
if (buf != null && buf.length > 0) {
FileOutputStream fOut = new FileOutputStream(fileName);
fOut.write(buf, 0, buf.length);
fOut.close();
}
//}
}
/**
* 删除文件
* @param fileName String 文件名,包含文件路径
* @return int 成功 >0;失败 -1
*/
public int deleteFile(String fileName) {
int results = 0;
File file = new File(fileName);
if (file.exists()) {
if (file.delete()) {
results = results + 1;
}
}
return results;
}
/**
* 删除文件夹
* @param dirName String 文件夹名称,包含文件夹路径
* @return int 成功 >0;失败 -1
*/
public int deleteDir(String dirName) {
int results = 0;
File dir = new File(dirName);
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) { //删除目录下的所有文件
if (files[i].exists()) {
files[i].delete();
}
}
if (dir.exists()) { //删除目录
if (dir.delete()) {
results = results + 1;
}
}
return results;
}
/**
* 批量删除物理文件
* @param arrfile String[] //被删除文件数组
* @throws Exception
* @return int 成功返回被删除文件个数,失败返回0
*/
public int deletePhysicsFile(String[] arrfile) throws Exception {
int results = 0;
for (int i = 0; i < arrfile.length; i++) {
File fl = new File(arrfile[i]);
if (fl.exists()) {
if (fl.delete()) {
results = results + 1;
}
}
}
return results;
}
/**
* 写文件方法。
* @param filename String 文件名
* @param arrcontent String[] 内容数组
* @param isappend boolean 是否追尾
* @throws Exception
*/
public void WriteFile(String filename, String strcontent,
boolean isappend) throws Exception {
File f1 = new File(filename);
if (!f1.exists()) {
f1.createNewFile();
}
FileWriter fr = new FileWriter(filename, isappend);
BufferedWriter w1 = new BufferedWriter(fr);
w1.write(strcontent);
w1.newLine();
w1.close();
fr.close();
}
/**
* 根据相对路径获得绝对路径
* @param pagecontext PageContext
* @param dirPath String 相对路径
* @return String 绝对路径
*/
public String getPath(PageContext pagecontext, String dirPath) {
String tempPath = pagecontext.getServletContext().getRealPath("");
Properties properties = System.getProperties();
String separator = properties.getProperty("file.separator");
String path = "";
if (separator.equals("\\")) {
path = tempPath.substring(0, tempPath.lastIndexOf("\\")) + dirPath;
path = path.replaceAll("\\\\", "/");
}
else {
path = tempPath.substring(0, tempPath.lastIndexOf("/")) + dirPath;
}
return path;
}
/**
* 创建目录
* @param dirPath String 目录
*/
public void mkdir(String dirPath) {
java.io.File dir = new java.io.File(dirPath);
if (!dir.exists()) {
dir.mkdirs();
}
}
public static String getValue(String parament) {
String textContent = "";
File file = new File(parament);
try {
BufferedReader input = new BufferedReader(new FileReader(file));
StringBuffer buffer = new StringBuffer();
String text;
int lines = 0;
while ( (text = input.readLine()) != null) {
buffer.append(text + "\n");
lines += 1;
}
input.close();
textContent = buffer.toString();
}
catch (Exception ex) {
}
return textContent;
}
/**
* 读取源文件内容
* @param filename String 文件路径
* @throws IOException
* @return byte[] 文件内容
*/
public byte[] readFileToByte(String filename) throws IOException {
File file =new File(filename);
if(filename==null || filename.equals(""))
{
throw new NullPointerException("无效的文件路径");
}
long len = file.length();
byte[] bytes = new byte[(int)len];
BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream(file));
int r = bufferedInputStream.read( bytes );
if (r != len)
throw new IOException("读取文件不正确");
bufferedInputStream.close();
return bytes;
}
public static void main(String[] args) {
}
}