Java-读写文件

/**
 * 将字符串写入指定文件 (当指定的父路径中文件夹不存在时,会最大限度去创建,以保证保存成功!)
 * 
 * @param res
 *            原字符串
 * @param filePath
 *            文件路径
 * @return 成功标记
 */
public static boolean writeString2File(String res, String filePath) {
   boolean flag = true;
   BufferedReader bufferedReader = null;
   BufferedWriter bufferedWriter = null;
   try {
      File distFile = new File(filePath);
      if (!distFile.getParentFile().exists())
         distFile.getParentFile().mkdirs();
      bufferedReader = new BufferedReader(new StringReader(res));
      bufferedWriter = new BufferedWriter(new FileWriter(distFile));
      char buf[] = new char[1024];
      int len;
      while ((len = bufferedReader.read(buf)) != -1) {
         bufferedWriter.write(buf, 0, len);
      }
      bufferedWriter.flush();
      bufferedReader.close();
      bufferedWriter.close();
   } catch (IOException e) {
      e.printStackTrace();
      flag = false;
      return flag;
   } finally {
      if (bufferedReader != null) {
         try {
            bufferedReader.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
   return flag;
}

/**
 * 从文件读取字符串
 * 
 * @param file
 *            文本文件
 * @param encoding
 *            编码类型
 * @return 转换后的字符串
 * @throws IOException
 */
public static String readStringFromFile(File file, String encoding) {
   InputStreamReader reader = null;
   StringWriter writer = new StringWriter();
   try {
      if (encoding == null || "".equals(encoding.trim())) {
         reader = new InputStreamReader(new FileInputStream(file), encoding);
      } else {
         reader = new InputStreamReader(new FileInputStream(file));
      }
      char[] buffer = new char[1024];
      int n = 0;
      while (-1 != (n = reader.read(buffer))) {
         writer.write(buffer, 0, n);
      }
   } catch (Exception e) {
      throw new AOSException("从文件读取字符串失败", e);
   } finally {
      if (reader != null)
         try {
            reader.close();
         } catch (IOException e) {
            throw new AOSException("从文件读取字符串失败", e);
         }
   }
   if (writer != null)
      return writer.toString();
   else
      return null;

}

///下面方法可以指定编码

/**
 * 读取txt文件转为字符串
 * @param filePath
 * @return
 * @throws MyException
 */
public static String readTxtFile2String(String filePath) throws MyException {
    StringBuffer res = new StringBuffer();
    BufferedReader reader = null;
    try{

        reader = new BufferedReader(new InputStreamReader(
                new FileInputStream(filePath), "UTF-8"));

        int len = -1;
        char [] byt=new char[1024];
        while ((len = reader.read(byt)) != -1) {
            String s = new String(byt, 0, len);
            res.append(s);
        }

        return res.toString();
    }catch (Exception e){
        throw new MyException("读取txt文件失败-"+filePath);
    }finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

/**
 * 写入字符串到指定输出目录
 * @param outputPath
 * @param content
 * @throws MyException
 */
public static void writeToTxt(String outputPath,String content) throws MyException {
    BufferedWriter bw = null;
    try {
        File file = new File(outputPath);
        if (!file.exists()) {
            file.createNewFile();
        }
        bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,true),"UTF-8"));//指定编码
        bw.write(content);
    }catch (IOException e) {
            throw new MyException("写入文件失败-"+outputPath);
    }finally {
        try {
            if(bw!=null){
                bw.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}

你可能感兴趣的:(java,java工具类)