java将字符串写入到文本里

痛定思痛。 2022-06-04 02:26 313阅读 0赞
  1. public void writefile(String path,String content,boolean append)
  2. {
  3. BufferedWriter bw;
  4. File writefile;
  5. boolean addStr = append;
  6. writefile = new File(path);
  7. if(writefile.exists()==false)
  8. {
  9. try {
  10. writefile.createNewFile();
  11. writefile = new File(path);
  12. } catch (IOException e) {
  13. // TODO 自动生成的 catch 块
  14. e.printStackTrace();
  15. }
  16. }
  17. else
  18. {
  19. writefile.delete();
  20. try {
  21. writefile.createNewFile();
  22. writefile = new File(path);
  23. } catch (IOException e) {
  24. // TODO 自动生成的 catch 块
  25. e.printStackTrace();
  26. }
  27. }
  28. try {
  29. FileWriter fw = new FileWriter(writefile,addStr);
  30. bw = new BufferedWriter(fw);
  31. fw.write(content);
  32. fw.flush();
  33. fw.close();
  34. } catch (IOException e) {
  35. // TODO 自动生成的 catch 块
  36. e.printStackTrace();
  37. }
  38. }
  39. }

发表评论

表情:
评论列表 (有 0 条评论,313人围观)

还没有评论,来说两句吧...

相关阅读