Java:字符串追加写入到txt文件中

Java:字符串追加写入到txt文件中


前言

本文主要分享了一种在Java中,如何字符串追加写入到 txt 文件中。


一、代码实例

BufferedWriter bw = null;
  try {
    
    
       bw = new BufferedWriter(new FileWriter("C:\\Users\\user\\Desktop\\结果输出.txt", true));
       for (String out : outPutString) {
    
    
            try {
    
    
                bw.newLine();
                bw.write(out);
                bw.flush(); //很重要,要不然可能输出的字符串不完整!
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
        }
    }
  }catch (Exception e){
    
    
      e.printStackTrace();
  }
  finally {
    
    
	      try {
    
    
	           bw.close();
	       } catch (IOException e) {
    
    
	           e.printStackTrace();
	       }
  }

猜你喜欢

转载自blog.csdn.net/qq_46119575/article/details/129426257
今日推荐