Java字符串写入文件三种方式的实现

 Java字符串写入文件三种方式的实现

成都创新互联是一家专业提供无棣企业网站建设,专注与成都做网站、网站设计、H5响应式网站、小程序制作等业务。10年已为无棣众多企业、政府机构等服务。创新互联专业网站设计公司优惠进行中。

1、使用FileWriter

String str="hello world!";
    FileWriter writer;
    try {
      writer = new FileWriter("E:/token.txt");
      writer.write(str);
      writer.flush();
      writer.close();
    } catch (IOException e) {
      e.printStackTrace();
    }

2、使用FileOutPutStream

File txt=new File("E:/log1.txt");
       if(!txt.exists()){ 
         txt.createNewFile(); 
      } 
       byte bytes[]=new byte[512];  
       bytes=str.getBytes(); 
       int b=bytes.length;  //是字节的长度,不是字符串的长度
       FileOutputStream fos=new FileOutputStream(txt); 
       fos.write(bytes,0,b); 
       fos.write(bytes);
       fos.close();

3、使用FileOutPutStream追加写入文件

FileOutputStream fos = new FileOutputStream("E:/log.txt",true);
//true表示在文件末尾追加 
fos.write(log.getBytes()); 
fos.close();

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


本文题目:Java字符串写入文件三种方式的实现
网页网址:http://lszwz.com/article/jescgo.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款