[Java] java io流写入文本文件的方法 →→→→→进入此内容的聊天室

来自 , 2020-06-17, 写在 Java, 查看 122 次.
URL http://www.code666.cn/view/2a084e55
  1.                 /**
  2.          * 向指定路径的文本中写入数据
  3.          *
  4.          * @param path
  5.          *            文本路径
  6.          * @param text
  7.          *            需要写入的内容
  8.          * @return true表示成功,false表示失败
  9.          */
  10.         public static boolean writeToFileByPath(String path, String text) {
  11.                 if (null == path || null == text) {
  12.                         return false;
  13.                 }
  14.                 File file = new File(path);
  15.                 if (!file.exists()) {
  16.                         try {
  17.                                 file.createNewFile();
  18.                         } catch (IOException e) {
  19.                                 e.printStackTrace();
  20.                         }
  21.                 }
  22.                 OutputStreamWriter writer = null;
  23.                 BufferedWriter bw = null;
  24.                 try {
  25.                         writer = new OutputStreamWriter(new FileOutputStream(file), "gbk");
  26.                         bw = new BufferedWriter(writer);
  27.                         bw.write(text);
  28.                         return true;
  29.                 } catch (Exception e) {
  30.                         e.printStackTrace();
  31.                         System.out.println("写入文件错误");
  32.                         return false;
  33.                 } finally {
  34.                         if (null != bw) {
  35.                                 try {
  36.                                         bw.close();
  37.                                 } catch (IOException e) {
  38.                                         e.printStackTrace();
  39.                                 }
  40.                         }
  41.                         if (null != writer) {
  42.                                 try {
  43.                                         writer.close();
  44.                                 } catch (IOException e) {
  45.                                         e.printStackTrace();
  46.                                 }
  47.                         }
  48.                 }
  49.         }

回复 "java io流写入文本文件的方法"

这儿你可以回复上面这条便签

captcha