You can work with the method contains () to detect the line you want to modify.
Example of use:
if (linea.contains("PBW=333")){
...
}
With split ( ) Trozeas the line by storing it in an array of String
String[] trozos = linea.split(",\s*"); //cogeremos posición 1 y 3
We make the modification of the line in the respective pieces
trozos[1] = String.valueOf(Integer.parseInt(trozos[1]) * 2);
trozos[3] = String.valueOf(Integer.parseInt(trozos[3]) * 2);
We save the line in a String by putting the commas back with everything modified, using the join () and save it in StringBuilder
String str = String.join(",", trozos);
builder.append(str).append("\r\n");
I leave the code to the full ..
public class ModificarArchivo {
static StringBuilder builder = new StringBuilder();
public static void main(String[] args) {
leerFichero();
escribirEnFichero();
}
public static void leerFichero(){
try (BufferedReader br=new BufferedReader(new FileReader("archivos/fichero1.txt"))) {
String linea = br.readLine();
while(linea != null){
if (linea.contains("PBW=333")){
String[] trozos = linea.split(",\s*"); //cogeremos posición 1 y 3
trozos[1] = String.valueOf(Integer.parseInt(trozos[1]) * 2);
trozos[3] = String.valueOf(Integer.parseInt(trozos[3]) * 2);
String str = String.join(",", trozos);
builder.append(str).append("\r\n");
linea = br.readLine();
}
builder.append(linea).append("\r\n");
linea = br.readLine();
}
} catch (FileNotFoundException ex) {
System.out.println("Archivo no encontrado");
} catch (IOException ex) {
System.out.println("Error en el flujo de lectura");
}
}
public static void escribirEnFichero(){
try(BufferedWriter bw = new BufferedWriter(new FileWriter("archivos/fichero1.txt"))){
bw.write(builder.toString());
bw.flush();
} catch (IOException ex) {
Logger.getLogger(ModificarArchivo.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
And the result of it: