How can I write a specific line number in a txt file in Java

I am currently writing my project for a school in which I need to read and write to txt files. I can read them correctly, but I can only write at the end of the added FileWriter. I would like to be able to overwrite things in my txt files with line numbers, first deleting the data on the line and then writing new data. I tried using 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 the search method moves in bytes, not line numbers. Can someone help. Thanks in advance:)

PS the file.lineNumber method returns the exact line that the old data was in, so I already have the line number to be written.

EDIT: found Soloution! Thanks guys :) I will send permission 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(); } } 
+6
source share
2 answers

A quick and dirty solution is to use the Files.readAllLines and Files.write to read all the 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, this is not a good idea if it is a very large file. See this answer for some ideas on how to copy a file in a line in this case.

Regardless of how you do this, if you change the byte length of a string, you will need to rewrite the entire file (AFAIK). RandomAcessFile allows you to move around the file and overwrite data, but not insert new bytes or delete existing ones, so the file length (in bytes) will remain unchanged.

+7
source

Here is a link to the question exactly with a great answer: I want to open a text file and edit a specific line in java

Basically, you cannot just edit this line if it is not the same length.

Instead, you want to copy each line, and then when you reach the line number of the line you want to change, instead of copying over the old line, just enter a new line.

The link I gave you has a great example of how to do this.

Hope this helps ... if not, let me know and I will dwell on this post in detail. Good luck :)

+1
source

Source: https://habr.com/ru/post/974946/


All Articles