Java字符串写入文件三种方式的实现
Java字符串写入文件三种方式的实现
1、使用FileWriter
Stringstr="helloworld!";
FileWriterwriter;
try{
writer=newFileWriter("E:/token.txt");
writer.write(str);
writer.flush();
writer.close();
}catch(IOExceptione){
e.printStackTrace();
}
2、使用FileOutPutStream
Filetxt=newFile("E:/log1.txt");
if(!txt.exists()){
txt.createNewFile();
}
bytebytes[]=newbyte[512];
bytes=str.getBytes();
intb=bytes.length;//是字节的长度,不是字符串的长度
FileOutputStreamfos=newFileOutputStream(txt);
fos.write(bytes,0,b);
fos.write(bytes);
fos.close();
3、使用FileOutPutStream追加写入文件
FileOutputStreamfos=newFileOutputStream("E:/log.txt",true);
//true表示在文件末尾追加
fos.write(log.getBytes());
fos.close();
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短