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();
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
希望与广大网友互动??
点此进行留言吧!