本文介绍了我怎样才能写在一个特定的行号在Java文件的txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在写我的学校项目,要求我读写txt文件。我可以正确地读取它们,但是我只能在附加的FileWriter中写入它们。我希望能够通过首先删除行上的数据,然后写入新的数据,在行数上覆盖我的txt文件中的东西。我尝试使用这种方法... $ / $>
pre $ public $ overFriteFile(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);
}
但是我相信seek方法是以字节而不是行数来移动的。任何人都可以帮忙在此先感谢:)
file.lineNumber方法返回旧数据的确切线,所以我已经有了需要写入的行号。
编辑:找到Soloution!谢谢你们:)如果任何人有兴趣,我会发布下面的soloution
public void overWriteFile(String dataType,String newData,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);
else if(path ==res / users.txt)
{
data = fileRead.getUsernameData(dataType,dataOrder);
else if(path ==(res / playerdata /+ team.teamname +.txt))
{
// data = fileRead.getPlayerData( dataType,team.teamname,dataOrder);
}
BufferedReader file = new BufferedReader(new FileReader(path));
字符串行;
String input =; ((line = file.readLine())!= null)
(
input + = line +'\\\
';
}
input = input.replace(dataType.toUpperCase()+:+ data,dataType.toUpperCase()+:+ newData);
FileOutputStream out = new FileOutputStream(path);
out.write(input.getBytes());
catch(Exception e)
{
System.out.println(Error覆盖文件:+ path);
e.printStackTrace();
$解析方案
A快速和肮脏的解决方案将是使用 Files.readAllLines
和 Files.write
方法读取所有行,更改一个你想改变,并覆盖整个文件:
List< String> lines = Files.readAllLines(file.toPath());
lines.set(line,dataType.toUpperCase()+:+ newData);
Files.write(file.toPath(),lines); //你也可以添加一个字符集和其他选项
当然,如果它不是一个好主意一个非常大的文件。请参阅,了解如何在这种情况下逐行复制文件。
不管你怎么做,如果你要改变行的字节长度,你需要重写整个文件(AFAIK)。 RandomAcessFile允许你移动文件并覆盖数据,但不能插入新的字节或删除现有的字节,所以文件的长度(以字节为单位)将保持不变。
I'm currently writing my project for school in which requires me to read and write to txt files. I can read them correctly but I can only write to them at the end from an appended FileWriter. I would like to be able to overwrite things in my txt files on line numbers by first deleting the data on the line and then writing in the new data. I attempted to use this method...
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);
}
but I believe that the seek method moves along in bytes rather than line numbers. Can anyone help. Thanks in advance :)
P.S. the file.lineNumber method returns the exact line that the old data was on so I already have the line number that needs to be written to.
EDIT: Soloution found! Thanks guys :) I'll post the soloution below if anyone is interested
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);
}
else if(path == "res/users.txt")
{
data = fileRead.getUsernameData(dataType, dataOrder);
}
else if(path == ("res/playerdata/" + team.teamname + ".txt"))
{
//data = fileRead.getPlayerData(dataType, team.teamname, dataOrder);
}
BufferedReader file = new BufferedReader(new FileReader(path));
String line;
String input = "";
while((line = file.readLine()) != null)
{
input += line + '\n';
}
input = input.replace(dataType.toUpperCase() + ":" + data, dataType.toUpperCase() + ":" + newData);
FileOutputStream out = new FileOutputStream(path);
out.write(input.getBytes());
}
catch(Exception e)
{
System.out.println("Error overwriting file: " + path);
e.printStackTrace();
}
}
解决方案
A quick and dirty solution would be to use the Files.readAllLines
and Files.write
methods to read all lines, change the one you want to change, and overwrite the whole file:
List<String> lines = Files.readAllLines(file.toPath());
lines.set(line, dataType.toUpperCase() + ":" + newData);
Files.write(file.toPath(), lines); // You can add a charset and other options too
Of course, that's not a good idea if it's a very big file. See this answer for some ideas on how to copy the file line by line in that case.
Regardless of how you do it, though, if you are changing the byte length of the line, you will need to rewrite the whole file (AFAIK). RandomAcessFile allows you to move around the file and overwrite data, but not to insert new bytes or removes existing ones, so the length of the file (in bytes) will stay the same.
这篇关于我怎样才能写在一个特定的行号在Java文件的txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!