我正在写我的学校项目,其中要求我读取和写入txt文件.我可以正确读取它们但我只能在附加的FileWriter中写入它们.我希望能够通过首先删除行上的数据然后写入新数据来覆盖行号上的txt文件中的内容.我试图使用这种方法......
public void overWriteFile(String dataType, String newData) throws IOException
{
ReadFile file = new ReadFile(path);
RandomAccessFile ra = new RandomAccessFile(path, "rw");
int line = file.lineNumber(path, dataType);
ra.seek(line);
ra.writeUTF(dataType.toUpperCase() + ":" + newData);
}
Run Code Online (Sandbox Code Playgroud)
但我相信搜索方法以字节而不是行号移动.谁能帮忙.提前致谢 :)
PS file.lineNumber方法返回旧数据所在的确切行,因此我已经有了需要写入的行号.
编辑:Soloution发现!谢谢大家:)如果有人有兴趣,我会在下面发布soloution
public void overWriteFile(String dataType, String newData, Team team, int dataOrder) throws IOException
{
try
{
ReadFile fileRead = new ReadFile(path);
String data = "";
if(path == "res/metadata.txt")
{
data = fileRead.getMetaData(dataType);
}
else if(path == "res/squads.txt")
{
data = fileRead.getSquadData(dataType, dataOrder);
} …Run Code Online (Sandbox Code Playgroud)