Hallo, ich hab hier etwas, was nicht ganz so toll ist:
Pattern[] pats = new Pattern[]{
Pattern.compile("Mass (.+)"),
Pattern.compile("Stiffness (.+)"),
Pattern.compile("price (.+)"),
Pattern.compile("bhp (.+)"),
Pattern.compile("tank (.+)")
};
String[] reps = {
"500.0",
"10",
"500",
"500",
"500"
};
File[] fa = new File("D:\\").listFiles();
for (File f : fa) {
if (f.getName().startsWith("tractor")) {
System.out.println("f = " + f);
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader(f));
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append("
");
}
br.close();
for (int i = 0; i < pats.length; i++) {
Matcher m = pats**.matcher(sb);
if (!m.find()) {
throw new RuntimeException("noooo");
} else {
System.out.println(m.start(1) + " " + m.end(1));
System.out.println( m.group() + " " + m.group(1));
sb.replace(m.start(1), m.end(1), reps**);
}
}
PrintWriter pw = new PrintWriter(f);
pw.print(sb.toString());
pw.close();
}
}
}```
Erst mal, `Pattern.compile("^Mass (.+)$")` nimmt Java 8 nicht mehr.
Ist es richtig, die komplette Datei einzulesen?
Ginge das nicht mit Linux super einfach?
Grüße