简体   繁体   中英

Java Writing to the Same txt File On Different Lines

I dont have any worthwhile code to show but I am trying to understand the concept.

I am opening a text file in two classes, one time saves a username like "user242," and the other class saves a number "42421,". The way I have set this out it saves on different lines such as:

line: 1 - user242, line: 2 -42421,

But every time I run the classes again it doesnt go in the format: line: 1 - user242, user114, line: 2 - 42421, 42525

It instead messes up and goes:

line: 1 - user242, line: 2 - 42421, user114, line: 3 - 4252

I think this is because of my use in newLine(); so is this any way I can fix this so it writes one variable always to the top line and the second always to the second line?

you can't insert data into a file. you can overwrite data using RandomAccessFile though. open the file, read the contents, modify and write them back.

you can also look at this Tutorial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM