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 生日广州男生祝福语简短