1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > java 追加写入txt文件_ava如何追加写入txt文件

java 追加写入txt文件_ava如何追加写入txt文件

时间:2023-08-28 16:28:52

相关推荐

java 追加写入txt文件_ava如何追加写入txt文件

(二)方法1

publicvoidmethod1(){

FileWriterfw=null;

try{

//如果文件存在,则追加内容;如果文件不存在,则创建文件

Filef=newFile("E:\dd.txt");

fw=newFileWriter(f,true);

}catch(IOExceptione){

e.printStackTrace();

}

PrintWriterpw=newPrintWriter(fw);

pw.println("追加内容");

pw.flush();

try{

fw.flush();

pw.close();

fw.close();

}catch(IOExceptione){

e.printStackTrace();

}

}

(三)方法2

publicstaticvoidmethod2(Stringfile,Stringconent){

BufferedWriterout=null;

try{

out=newBufferedWriter(newOutputStreamWriter(

newFileOutputStream(file,true)));

out.write(conent+"\r\n");

}catch(Exceptione){

e.printStackTrace();

}finally{

try{

out.close();

}catch(IOExceptione){

e.printStackTrace();

}

}

}

(四)方法3

publicstaticvoidmethod3(StringfileName,Stringcontent){

try{

//打开一个随机访问文件流,按读写方式

RandomAccessFilerandomFile=newRandomAccessFile(fileName,"rw");

//文件长度,字节数

longfileLength=randomFile.length();

//将写文件指针移到文件尾。

randomFile.seek(fileLength);

randomFile.writeBytes(content+"\r\n");

randomFile.close();

}catch(IOExceptione){

e.printStackTrace();

}

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。