Java 替换不工作的行代码
我有一个javascript,我需要用java编辑它,所以在第15行我有var=12.0001,我想用其他值替换它,所以我使用了这段代码Java 替换不工作的行代码,java,io,Java,Io,我有一个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 li
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();
FileInputStream fs=newfileinputstream(“D://maps2.html”);
BufferedReader br=新的BufferedReader(新的InputStreamReader(fs));
对于(int i=0;i<15;++i)
br.readLine();
字符串lineIWant=br.readLine();
lineIWant=lineIWant.substring
System.out.println(lineIWant);
弦线;字符串输入=”;
System.getProperty(“line.separator”);
//替换代码
而((line=br.readLine())!=null)输入+=line+“\r\n”;
系统输出打印项次(输入);
输入=输入。替换(lineIWant,“15.0001”);
br.close();
因此,问题是它确实正确地获取了lineIwant字符串(),但当我运行它时,它会删除文件的前16行。当我在replace函数中传递值ie“12.0001”而不是LineI时,如果它工作正常。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();
请修正你的缩进,告诉我们你的文件里有什么,然后问一个问题。例如,
lineIWant=lineIWant.substring
应该做什么?For的第一个迭代前15行,下面的字符串lineIWant=br.readLine()
读取第16行,因此您将从第17行开始进入line@ElliottFrischlineIwant将是var lat=12.0001我只是想要这个值,所以我做了一个子字符串,得到了12.0001correctly@alfasin那么这个问题的解决方案是什么呢???@PrateekSaxena对不起,我不知道你想问什么。扔掉前15行和行分隔符。你的缩进很糟糕,你还没问一个问题。