源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2020-05-06 08:15 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Java字符串写入文件三种方式的实现
[b] Java字符串写入文件三种方式的实现[/b] [b]1、使用FileWriter[/b]
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();
    }
[b]2、使用FileOutPutStream[/b]
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();
[b]3、使用FileOutPutStream追加写入文件[/b]
FileOutputStream fos = new FileOutputStream("E:/log.txt",true);
//true表示在文件末尾追加 
fos.write(log.getBytes()); 
fos.close();

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部