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
\$\endgroup\$
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());
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.