最近搜索

java写txt

浏览:507
管理员 2023-01-09 15:23



 

 

/**
    * filePath : c:\a.txt
    * content:  这里就可以写入要追加的内容了
    * 在txt中写入数据,如果文件不存会创建。  文件夹如果不存在不行。要加判断文件夹是否存在的方法。
    * 写完自动换行。
    */
   public static void txt_write(String filePath,String content) {
      FileWriter ff= null;
      try {
//查看C盘是否有a.txt文件来判定是否创建
         File f=new File(filePath);
         ff = new FileWriter(f, true);//将字节写入文件末尾处,相当于追加信息。
      } catch (IOException e) {
         e.printStackTrace();
      }
      PrintWriter p = new PrintWriter(ff);
      p.println(content);//此处为追加内容
      p.flush();
      try {
         ff.flush();
         p.close();
         ff.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }


联系站长

站长微信:xiaomao0055

站长QQ:14496453