How do you replace a line in a text file?
For example you have 1.@@@ and want to replace it with 1.###
I have program this prgram at the moment. You search through a list and if you find a string, that you want. You write the string to another file. My porblem is that I don't know how to replace a line in an existing text file.
private static BufferedReader br;
public static void main(String[] args) throws Exception{
try{
FileInputStream fstream = new FileInputStream("C:\\Users\\Timmic\\workspace\\Foutverbeterende codes\\genereren append testbinair apart.txt");
br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
String tokens[] = strLine.split(";");
int x = Integer.parseInt(tokens[2]);
if(x<2){
tokens[3]="###";
String a1 = Arrays.toString(tokens);
String a2 = a1.replaceAll(" ", "");
String a3 = a2.replaceAll(" ", "");
String a6 = a3.replaceAll(",", ";");
String a7 = a6.replaceAll("[<>\\[\\],-]", "");
String a8 = a7 + ";";
System.out.println(a8);
FileWriter fwriter = new FileWriter("d is 2.txt", true);
PrintWriter outputFile = new PrintWriter(fwriter);
outputFile.println(a8);
outputFile.close();
}
}
}
catch(Exception e){}
}
and this is the list.
0; 000;0;*;0;0;0;
1; 001;1;*;0;0;1;
2; 010;1;*;0;1;0;
3; 011;2;*;0;1;1;
4; 100;1;*;1;0;0;
5; 101;2;*;1;0;1;
6; 110;2;*;1;1;0;
7; 111;3;*;1;1;1;