我有一个javascript,我需要使用java进行编辑所以在第15行我有var = 12.0001并且我想用其他值替换它,所以我使用了这段代码
FileInputStream fs= new FileInputStream("D://maps2.html");
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
for(int i = 0; i < 15; ++i)
br.readLine();
String lineIWant = br.readLine();
lineIWant=lineIWant.substring
System.out.println(lineIWant);
String line;String input = "";
System.getProperty("line.separator");
//the replace code
while ((line = br.readLine()) != null) input += line + "\r\n";
System.out.println(input);
input = input.replace(lineIWant, "15.0001");
br.close();
所以问题就是它确实正确获取了lineIwant字符串()但是当我运行它时,它会删除文件的前16行。当我传递值时,即&#34; 12.0001&#34;在替换函数而不是lineIWant它正常工作。
答案 0 :(得分:0)
for循环是多余的,请尝试:
FileInputStream fs= new FileInputStream("D://maps2.html");
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
String line;String input = "";
while ((line = br.readLine()) != null) {
input += line + "\r\n";
}
System.out.println(input);
input = input.replace("12.0001", "15.0001"); // simply replace the value you want
br.close();