2
\$\begingroup\$

So I need to update some certain values within text file based on actual values there. Any advice on making it more effective or just readable is appreciated.

Example text file:

Lalala 
There is tons of text in this file log-12345sr
And somewhere within
There are log file names log-23456sr
Some more text here
I want to edit log file names within Java code
to change the log file names log-34567sr some text here as well
But I want to edit based on current value
For example add 100 to the number in the name
So log-45678sr 
becomes log-45778sr
I can safely assume there is no more than one on a single line

My code:

public static void main(String[] args) throws Exception {
 doModification(new File("test.txt"));
}
public static ArrayList<String> findAllOccurrences(File file, String prefix, String suffix) throws IOException {
 ArrayList<String> occurrences = new ArrayList<>();
 BufferedReader br = new BufferedReader(new FileReader(file));
 String line;
 while ((line = br.readLine()) != null) {
 if (line.contains(prefix) && line.contains(suffix)) {
 occurrences.add(line.split(prefix)[1].split(suffix)[0]);
 }
 }
 return occurrences;
}
public static HashMap<String,String> buildReplacementMap(ArrayList<String> values, Function<String, String> modification) throws Exception {
 HashMap<String,String> replacementMap = new HashMap<>();
 for(String value: values){
 replacementMap.put(value, modification.apply(value));
 }
 return replacementMap;
}
public static void doModification(File file) throws Exception {
 ArrayList<String> occurrences = findAllOccurrences(file, "log-", "sr");
 HashMap<String, String> replacementMap = buildReplacementMap(occurrences, (value) -> String.valueOf(Long.parseLong(value) - 100));
 String content = new String(Files.readAllBytes(file.toPath()));
 for (Map.Entry<String,String> pair : replacementMap.entrySet()){
 content = content.replaceAll(pair.getKey(), pair.getValue());
 }
 Files.write(file.toPath(),content.getBytes());
}

Text file after modification:

Lalala 
There is tons of text in this file log-12445sr
And somewhere within
There are log file names log-23556sr
Some more text here
I want to edit log file names within Java code
to change the log file names log-34667sr some text here as well
But I want to edit based on current value
For example add 100 to the number in the name
So log-45778sr 
becomes log-45878sr
I can safely assume there is no more than one on a single line
asked Jun 13, 2016 at 9:46
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

You could simplify with Regular Expressions:

Pattern pattern = Pattern.compile("log-(\\d+)sr");
Matcher matcher = pattern.matcher(input);
StringBuffer buffer = new StringBuffer();
while(matcher.find()) {
 Long value = Long.parseLong(matcher.group(1), 10);
 Long increased = value + 100;
 matcher.appendReplacement(buffer, increased.toString());
}
System.out.println(buffer.toString());
answered Jun 13, 2016 at 16:48
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.