以前寫的一個java操作文件的常用方法,包括建立目錄,刪除目錄,建立文件,刪除文件,讀取文件內容等
package com.life.common;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
/**
* <p>Title: 文件操作</p>
* <p>Description:用于文件操作,如建立目錄,刪除目錄,建立文件,刪除文件等</p>
*
*/
public class PFile {
private static File f=null;
/**
* 建立目錄,給定目錄路徑,一層層判斷該目錄是否存在,不存在則建立,再判斷下一目錄
* @param sPath 要創建的目錄
* @return 建立成功返回空,否則返回錯誤信息
*/
public static String CreateFolder(String sPath){
String sErr="";
String sTmpPath;
String[] aP;
sTmpPath=sPath;
sTmpPath=sTmpPath.replaceAll("\\\\","/");
if (sTmpPath.indexOf("/")==-1) {
sErr="路徑錯誤!";
}
else{
aP=sPath.split("/");
try{
for (int i=0;i<aP.length ;i++ ) {
sTmpPath="";
for (int j=0;j<=i;j++){
sTmpPath+=aP[j] +"/";
}
f=new File(sTmpPath);
if (!f.exists()){
f.mkdir();
}
}
}
catch(Exception e){
sErr=e.getMessage();
}
}
return sErr;
}
/**
* 刪除目錄,給定目錄路徑,不存在則返回錯誤,否則刪除此目錄,
*@param sPath 要刪除的目錄
*@return 出錯返回出錯信息, 成功返回空
*/
public static String DeleteFolder(String sPath){
String sErr="";
f=new File(sPath);
try{
if (f.exists()) {
f.delete();
}
else{
sErr="要刪除的目錄不存在!";
}
}
catch(Exception e){
sErr=e.getMessage();
}
return sErr;
}
/**
* 新建文件,并向文件中寫入文件內容
*@param sFilePath 要生成的文件路徑,文件名包括在其中
*@param sContent 要寫入文件的內容
*@return 操作成功返回空,否則返回出錯信息
*/
public static String CreateFile(String sFilePath,String sContent){
String sErr="";
try{
FileWriter fw=new FileWriter(sFilePath);
fw.write(sContent);
fw.close();
}
catch(Exception e){
sErr=e.getMessage();
}
return sErr;
}
/**
* 刪除文件,刪除指定路徑的文件
* @param sFilePath 要刪除的文件的路徑
* @return 操作成功返回空,否則返回錯誤信息
*/
public static String DeleteFile(String sFilePath){
String sErr="";
try{
f=new File(sFilePath);
if (f.exists()){
f.delete();
}
else{
sErr="文件不存在";
}
}
catch(Exception e){
sErr=e.getMessage();
}
return sErr;
}
/**
* 生成靜態頁面
* @param filepath 包含文件名的文件路徑
* @param url 要生成靜態頁面的路徑
* @return 返回執行的錯誤信息,成功返回空
*/
public static String createHTML(String filepath,String url){
String errmsg="";
try {
URL u=new URL(url);
//System.out.println(url);
URLConnection uc=u.openConnection();
BufferedReader br=new BufferedReader(new InputStreamReader(uc.getInputStream()));
String templine="";
StringBuffer sb=new StringBuffer();
while((templine=br.readLine())!=null){
sb.append(templine+"\n");
}
//System.out.println("sb:"+sb);
errmsg=PFile.CreateFolder(filepath.substring(0,filepath.lastIndexOf("/"))+"/");
errmsg+=CreateFile(filepath,sb.toString());
br.close();
} catch (Exception e) {
System.out.println("錯誤信息:"+e.toString());
errmsg=e.toString();
}
return errmsg;
}
/**
*TODO 讀取文件內容
*@param sFilePath 要讀取的文件
*@return 返回讀取的文件內容
*/
public static String ReadFile(String sFilePath) throws IOException{
String s="";
File f=new File(sFilePath);
if (f.exists())
{
FileReader fr = new FileReader(sFilePath);
BufferedReader br = new BufferedReader(fr);
StringBuffer dd=new StringBuffer();
String sLine = br.readLine(); //讀取一行數據
while(sLine != null) //判斷讀取得的數據是否為null
{
dd.append(sLine+"\n");
sLine = br.readLine(); //讀取一行數據
if (sLine==null) break;
}
br.close(); //關閉BufferedReader對象
fr.close(); //關閉檔案
s=dd.toString();
}
return s;
}
/**
* 重命名文件
* @param oldname 舊文件名
* @param newname 新文件名
* @return 操作成功返回空,否則返回錯誤信息
*/
public static String RenameFile(String oldname,String newname){
String sErr="";
try{
f=new File(oldname);
if (f.exists()){
f.renameTo(new File(newname));
}
else{
sErr="文件不存在";
}
}
catch(Exception e){
sErr=e.getMessage();
}
return sErr;
}
}