Say the following is my file content (hundreds of same pattern lines)
1979,2013-08-07 19:03:35,abc,12345,310012
1980,2013-08-07 19:05:03,fds,12345,310160
.
.
I want to read the file and scan the file line by line and replace the 4th column of the line (12345 which repeats on all the lines) to another value as well as adding a new value at the end of each line.
Finally I need to regenerate the file with the updated values as an output.
here is what I do so far:
URL path = ClassLoader.getSystemResource("test.txt");
File file = new File(path.toURI());
try
{
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine())
{
String line = scanner.nextLine();
// Here I need to do the replacement codes
}
scanner.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
Is it a good idea to split each line to an array and do the processes like that or is there any better solution for this?
I am also not sure how to create an output with the edited content. Any help would be appreciated.