JAVA-文件讀寫模板

class FileEntry {
    /**
     * 讀取文件的所有內容
     * @param filePath 文件路徑
     * @param charset 編碼
     * @return 文件的全部內容,作爲一個字符串返回
     */
    public static String readFile(String filePath, String charset) {
        File file = new File(filePath);
        Long filelength = file.length();
        byte[] filecontent = new byte[filelength.intValue()];
        FileInputStream in = null;
        String result = null;
        try {
            in = new FileInputStream(file);
            in.read(filecontent);
            result = new String(filecontent, charset);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        } finally {
            try {
                if (null != in)
                    in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 讀取文件內容
     * @param filePath 文件路徑
     * @param charset 編碼
     * @return 文件的每一行
     */
    public static ArrayList<String> readFileLines(String filePath, String charset) {
        BufferedReader reader = null;
        ArrayList<String> lines = new ArrayList<String>();
        try {
            FileInputStream fis = new FileInputStream(filePath);
            InputStreamReader isr = new InputStreamReader(fis, charset); 
            reader = new BufferedReader(isr);

            String tempString = null;
            // 一次讀入一行,直到讀入null爲文件結束
            while ((tempString = reader.readLine()) != null) {
                lines.add(tempString);
            }
            reader.close();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return lines;
    }

    /**
     * 獲取指定文件夾下的所有文件名稱(遞歸)
     * @param folderPath 文件夾路徑
     * @return 每個文件的絕對路徑
     */
    public static ArrayList<String> getFolderFiles(String folderPath) {
        ArrayList<String> ret = new ArrayList<String>();
        File fileObject = new File(folderPath);
        if (fileObject.isFile())
            ret.add(fileObject.getAbsolutePath());
        else {
            for (String subFilePath: fileObject.list()) {
                ret.addAll(getFolderFiles(folderPath + File.separatorChar + subFilePath));
            }
        }
        return ret;
    }

    /**
     * 將指定內容寫入文件中
     * @param filePath 文件路徑
     * @param content 待寫入內容
     * @param charset 編碼
     */
    public static void writeFile(String filePath, String content, String charset) {
        BufferedWriter bw = null;
        try {
            File file = new File(filePath);
            file.getParentFile().mkdirs();

            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), charset));
            bw.write(content);
            bw.flush();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if (null != bw)
                    bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 在文件尾追加內容
     * @param content 待追加文本
     * @param fileDes 文件路徑
     * @param charset 編碼
     */
    public static void appendToFile(String filePath, String content, String charset) {
        BufferedWriter bw = null;
        try {
            File file = new File(filePath);
            file.getParentFile().mkdirs();

            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath, true), charset));
            bw.write(content);
            bw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != bw)
                    bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }  
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章