文件操作Java工具類

c9cv 12年前發布 | 2K 次閱讀 PHP庫
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

/**
 * 文件工具類
 * FileUtils
 * @author LXY
 * @version 1.0
 */
public class FileUtils {

    /**
     * 讀取文件內容(使用UTF-8編碼)
     * @param filePath 輸出文件路徑
     * @return
     * @throws Exception
     */
    public static String readFileUTF8(String filePath) throws Exception{
        File file = new File(filePath);
        FileInputStream fis = new FileInputStream(file);
        BufferedReader br = new BufferedReader(new InputStreamReader(fis,"UTF-8"));
        String fileContent = "";
        String temp = "";
        while ((temp = br.readLine()) != null) {
            fileContent = fileContent+temp;
        }
        br.close();
        fis.close();
        return fileContent;
    }

    /**
     * 將文件內容寫入文件(使用UTF-8編碼)
     * @param content 文件內容
     * @param filePath  輸出文件路徑
     * @throws Exception
     */
    public static void writeFileUTF8(String content,String filePath) throws Exception{
        createDir(filePath);
        File file = new File(filePath);
        FileOutputStream fos = new FileOutputStream(file);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos,"UTF-8"));
        bw.write(content);
        bw.flush();
        bw.close();
        fos.close();
    }

    /**
     * 寫文件
     * @param outputPath 輸出文件路徑
     * @param is 輸入流
     * @param isApend 是否追加
     * @throws IOException
     */
    public static void writeFile(InputStream is,String outputPath,boolean isApend) throws IOException{
        FileInputStream fis = (FileInputStream)is;
        createDir(outputPath);
        FileOutputStream fos = new FileOutputStream(outputPath,isApend);
        byte[] bs = new byte[1024*16];
        int len = -1;
        while ((len = fis.read(bs)) != -1) {
            fos.write(bs, 0, len);
        }
        fos.close();
        fis.close();
    }

    /**
     * copy文件
     * @param is 輸入流
     * @param outputPath 輸出文件路徑
     * @throws Exception
     */
    public static void writeFile(InputStream is,String outputPath) throws Exception{
        InputStream bis=null;
        OutputStream bos=null;
        createDir(outputPath);
        bis=new BufferedInputStream(is);
        bos=new BufferedOutputStream(new FileOutputStream(outputPath));
        byte[] bs = new byte[1024*10];
        int len = -1;
        while ((len = bis.read(bs)) != -1) {
            bos.write(bs, 0, len);
        }
        bos.flush();
        bis.close();
        bos.close();
    }

    /**
     * 寫文件
     * @param outputPath 輸出文件路徑
     * @param inPath 輸入文件路徑
     * @throws IOException
     */
    public static void writeFile(String inPath,String outputPath,boolean isApend) throws IOException{
        if(new File(inPath).exists()){
            FileInputStream fis = new FileInputStream(inPath);
            writeFile(fis,outputPath,isApend);          
        }else {
            System.out.println("文件copy失敗,由于源文件不存在!");
        }
    }

    /**
     * 將字符串寫到文件內
     * @param outputPath 輸出文件路徑
     * @param msg 字符串
     * @param isApend  是否追加
     * @throws IOException
     */
    public static void writeContent(String msg,String outputPath,boolean isApend) throws IOException{
        BufferedWriter bw = new BufferedWriter(new FileWriter(outputPath,isApend));
        bw.write(msg);
        bw.flush();
        bw.close();
    }

    /**
     * 刪除文件夾下的所有內容,包括本文件夾
     * @param path 刪除文件路徑
     * @throws IOException
     */
    public static void delFileOrDerectory(String path)throws IOException{ 
        File file = new File(path); 
        if(file.exists()){ 
            if(file.isDirectory()){ 
                File[] files = file.listFiles(); 
                for(int i=0;i<files.length;i++){ 
                    File subFile = files[i]; 
                    delFileOrDerectory(subFile.getAbsolutePath());
                } 
                file.delete(); 
            }else{ 
                file.delete(); 
            } 
        } 
    }

    /**
     * 如果欲寫入的文件所在目錄不存在,需先創建
     * @param outputPath 輸出文件路徑
     */
    public static void createDir(String outputPath){
        File outputFile = new File(outputPath);
        File outputDir = outputFile.getParentFile();
        if(!outputDir.exists()){
            outputDir.mkdirs();
        }
    }       
}
 本文由用戶 c9cv 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!