简体   繁体   English

Java在不同的行上写入相同的txt文件

[英]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,". 我在两个类中打开一个文本文件,一次保存一个用户名,例如“ user242”,另一个保存一个数字“ 42421”。 The way I have set this out it saves on different lines such as: 我设定的方式保存在不同的行中,例如:

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

But every time I run the classes again it doesnt go in the format: line: 1 - user242, user114, line: 2 - 42421, 42525 但是每次我再次运行类时,它都不会采用以下格式:行:1-user242,user114,行:2-42421,42525

It instead messes up and goes: 相反,它搞砸了,然后走了:

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

I think this is because of my use in newLine(); 我认为这是因为我在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. 您可以使用RandomAccessFile覆盖数据。 open the file, read the contents, modify and write them back. 打开文件,阅读内容,修改并写回。

you can also look at this Tutorial 您也可以查看此教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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