How can i replace(over write) a line in java???

Hi I wrote a file in java Now i want to update this file, And over write a specific line. How can i do this???

20th Apr 2021, 7:54 AM
Hamed Taherkhani
Hamed Taherkhani - avatar
2 Answers
+ 2
To overwrite a particular line of a file − Read the contents of a file to String − Instantiate the File class. Instantiate the Scanner class passing the file as parameter to its constructor. Create an empty StringBuffer object. add the contents of the file line by line to the StringBuffer object using the append() method. Convert the StringBuffer to String using the toString() method. Close the Scanner object. Invoke the replaceAll() method on the obtained string passing the line to be replaced (old line) and replacement line (new line) as parameters. Rewrite the file contents − Instantiate the FileWriter class. Add the results of the replaceAll() method the FileWriter object using the append() method. Push the added data to the file using the flush() method.
20th Apr 2021, 6:59 PM
Nihar Buliya
Nihar Buliya - avatar
+ 1
Thanks bro
21st Apr 2021, 4:31 AM
Hamed Taherkhani
Hamed Taherkhani - avatar