package com.platform.utils; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; /** * 文件读写操作帮助类 * * @author wuming * */ public class FileOperateHelper { /** * 以追加的方式将信息写入文件 * * @param path * @param message */ @SuppressWarnings("resource") public static void fileWrite(String path, String message) { try { File file = new File(path); if (file.exists()) file.createNewFile(); FileOutputStream out = new FileOutputStream(file, true); // 如果追加方式用true StringBuffer sb = new StringBuffer(); sb.append(message).append("\n"); out.write(sb.toString().getBytes("utf-8")); } catch (IOException e) { // TODO: handle exception } } /** * 文件读取方法 * @param path * @return */ @SuppressWarnings("resource") public static String fileReader(String path) { StringBuffer sb = new StringBuffer(); String tempString = ""; try { File file = new File(path); if (!file.exists()) return ""; FileInputStream fis = new FileInputStream(file); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); while ((tempString = br.readLine()) != null) { sb.append(tempString); } } catch (Exception e) { // TODO: handle exception } return sb.toString(); } }