当前位置: 移动技术网 > IT编程>开发语言>Java > Java字符串写入文件三种方式的实现

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

2019年07月22日  | 移动技术网IT编程  | 我要评论

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

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();

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网