-1
如何替换文本文件中的一行?用java替换文本文件中的一行
例如你有1。@@@ 并想用1来取代它。###
我有这个程序在prgram的时刻。 您可以通过列表进行搜索,并且如果找到了所需的字符串。您将该字符串写入另一个文件。我的问题是我不知道如何替换现有文本文件中的一行。
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){}
}
这就是这个清单。
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;
可能重复的[爪哇逐行读取文件中的行和替换第n列(http://stackoverflow.com/questions/18478820/java-read-file-line-by-line-and-replace-第n列) –
几天前我回答了一个类似于这个问题。我知道它不是*完全*你在找什么,但它会给你一个关于如何重写'File'的总体思路。所有你需要做的是做一些小的修改。参考[this](http://stackoverflow.com/a/18479057/1255746)。随意问任何你可能有的问题。 –