How can I read an existing text file and modify some of its value based on some condition like if I get some text like
Apple 1 3 5
Now, here whenever I get an integer value I have to increase its count by 1 and save back to the same file again.
For example, if I read some content like above I should be able to save
Apple 2 4 6
back to the same file again
My code looks like
try{
FileWriter fstream = new FileWriter(“c:\Apple.txt”,true);
FileReader inputFileReader = new FileReader("c:\\Apple.txt");
BufferedReader inputStream = new BufferedReader(inputFileReader);
PrintWriter outputStream = new PrintWriter(fstream);
while((inline=inputStream.readLine())!=null)
{
values=inline.split("-");
Integer l = Integer.parseInt(values[2])+1;
outputStream.print((inline.replaceAll(values[2],l.toString())));
}
outputStream.close();
}
So, I get an output like
Apple 1 3 5 Apple 1 4 5
But my required output is
Apple 1 4 5
Thanks in advace
You can’t do it interactively on the same file (or at least maybe you can but it would be really tricky).. you could