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???
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.