Java 替换文件中不工作的多行
我试图通过替换文件中的3行来编辑build.prop文件。以下方法保留原始行,并将新行复制到新文件中。我需要更换线路,而不是添加新线路 我已经更新了代码以使用下面显示的“else if”Java 替换文件中不工作的多行,java,android,Java,Android,我试图通过替换文件中的3行来编辑build.prop文件。以下方法保留原始行,并将新行复制到新文件中。我需要更换线路,而不是添加新线路 我已经更新了代码以使用下面显示的“else if” /**Changing build.prop values here * @throws IOException */ public void PropEdit() throws InterruptedException, IOException { String origFile = "data/data/
/**Changing build.prop values here
* @throws IOException */
public void PropEdit() throws InterruptedException, IOException
{
String origFile = "data/data/vzw.versatile1.props/build.orig.prop";
String propFile = "data/data/vzw.versatile1.props/build.new.prop";
try {
suProcess = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
os.writeBytes("chmod 777 /data/data/vzw.versatile1.props/build.new.prop\n");
BufferedReader in = new BufferedReader(new FileReader(origFile));
PrintWriter out = new PrintWriter(new File(propFile));
String line;
String params [];
while ((line = in.readLine()) != null) {
params = line.split("=");
if
(params[0].equalsIgnoreCase("ro.product.device")) {
out.println(params[0] + "=" + "Maguro");
/**out.println(line); **/
out.flush();
} else
if
(params[0].equalsIgnoreCase("ro.product.name"))
{
out.println(params[0] + "=" + "yakju");
/**out.println(line); **/
out.flush();
} else if (params[0].equalsIgnoreCase("ro.product.model"))
{
out.println(params[0] + "=" + "Galaxy Nexus");
/**out.println(line); **/
out.flush();
}
out.println(line);
}
boolean successful;
{
out.close();
in.close();
os.flush();
os.close();
suProcess.waitFor();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "ERROR: " + e.getMessage(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
更改此代码:
if ("ro.device.name".equals(params[0]))
out.println(params[0] + "=" + Galaxy Nexus);
} else {
out.println(line);
}
到(请注意if
行末尾的{
:
if ("ro.device.name".equals(params[0])) {
out.println(params[0] + "=" + Galaxy Nexus);
} else {
out.println(line);
}
编辑:如果我理解正确,应该这样做:
//your while loop
// if you have exact 3 cases, you can do this.
while ((line = in.readLine()) != null) {
params = line.split("=");
if ("ro.device.name".equalsIgnoreCase(params[0])) {
out.println(params[0] + "=" + "Galaxy Nexus");
} else if ("ro.device.product".equalsIgnoreCase(params[0])) {
out.println(params[0] + "=" + "some String");
} else if ("ro.device.whatever".equalsIgnoreCase(params[0])) {
out.println(params[0] + "=" + "another String");
} else {
out.println(line);
}
}
也许你有区分大小写的问题。你可以试试
"ro.device.name".equalsIgnoreCase(params[0])
或者,我认为replaceAll也适用于这类任务
line = line.replaceAll("(?i)" + "ro.device.name=Galaxy Nexus",);
或
注意“(?i)”表示忽略大小写。您所说的“with the params[]是什么意思“?向我们展示您迄今为止尝试过的代码!@Versatile1您的实现看起来不错,那么问题出在哪里?如果您同时显示文件和预期输出,可能会有所帮助。我回家后会抓取其余的代码和文件/输出。问题是当我尝试添加更多行时,它会循环,但不会抛出错误。我有一个cath3行所以我不明白。@Versatile1“你的意思是你只需要处理前3行。”你的意思是你只想替换前3行中的东西,对吗?我有点困惑…我实际上使用了.equalsIgnoreCase(params[0]),我已经打过那场仗了:)但是谢谢you@Versatile1好的,请看我的编辑。希望这次我能正确地理解你。你建议的编辑效果很好。唯一的问题是在方法写入新代码后原始行仍然存在line@EntryLevelDev21你知道为什么是复制而不是替换吗?你能看看你的日志吗?打印出来的是什么?
public static void replace(String oldstring, String newstring, File in, File out)
throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(in));
PrintWriter writer = new PrintWriter(new FileWriter(out));
String line = null;
while ((line = reader.readLine()) != null)
writer.println(line.replaceAll("(?i)" + oldstring,newstring));
reader.close();
writer.close();
}