Redian新闻
>
How to append something on a file?
avatar
How to append something on a file?# Java - 爪哇娇娃
w*l
1
Hi,
I am using the following codes to create a file and put some strings (or some
other format data) into this file. I hope to save some extra strings into this
file when I run this program again. Of course, I don't want to delete the
original strings (or data). How can I finish this function?
File data_File = new File("image_data.txt");
FileOutputStream data_Stream = new FileOutputStream(data_File);
PrintWriter data_write_Stream = new PrintWriter(data_Stream);
data_write_Stream.print("abc");
Th
avatar
s*m
2

some
this
I got this somewhere on the web.
Open your file as RandomAccessFile, then go to the end of last line and start
writing.
java.io.RandomAccessFile data_file = new java.io.RandomAccessFile(fileName,
"rw");
data_file.seek(data_file.length());
data_file.write...(....);
data_file.close();

【在 w*l 的大作中提到】
: Hi,
: I am using the following codes to create a file and put some strings (or some
: other format data) into this file. I hope to save some extra strings into this
: file when I run this program again. Of course, I don't want to delete the
: original strings (or data). How can I finish this function?
: File data_File = new File("image_data.txt");
: FileOutputStream data_Stream = new FileOutputStream(data_File);
: PrintWriter data_write_Stream = new PrintWriter(data_Stream);
: data_write_Stream.print("abc");
: Th

avatar
u*r
3
I hope I have understood the problem right -- read the Javadoc for
FileOutputStream's constructor:
public FileOutputStream(String name,
boolean append)
throws FileNotFoundException
Creates an output file stream to write to the file with the specified name. If the second argument is true, then bytes will be written to the end of the file rather than the beginning. A new FileDescriptor object is created to represent this file
connection.

【在 w*l 的大作中提到】
: Hi,
: I am using the following codes to create a file and put some strings (or some
: other format data) into this file. I hope to save some extra strings into this
: file when I run this program again. Of course, I don't want to delete the
: original strings (or data). How can I finish this function?
: File data_File = new File("image_data.txt");
: FileOutputStream data_Stream = new FileOutputStream(data_File);
: PrintWriter data_write_Stream = new PrintWriter(data_Stream);
: data_write_Stream.print("abc");
: Th

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。