Java 文件复制行而不是替换行
我试图通过替换文件中的3行来编辑build.prop文件。以下方法保留原始行,并将新行复制到新文件中。我需要替换这些行,而不是添加新的行。 从系统中提取原始文件并创建备份。以下是输出文件的一部分Java 文件复制行而不是替换行,java,android,Java,Android,我试图通过替换文件中的3行来编辑build.prop文件。以下方法保留原始行,并将新行复制到新文件中。我需要替换这些行,而不是添加新的行。 从系统中提取原始文件并创建备份。以下是输出文件的一部分 ro.product.model=Galaxy Nexus ro.product.model=SCH-I605 ro.product.brand=samsung ro.product.name=yakju ro.product.name=t0ltevzw ro.product.device=Maguro
ro.product.model=Galaxy Nexus
ro.product.model=SCH-I605
ro.product.brand=samsung
ro.product.name=yakju
ro.product.name=t0ltevzw
ro.product.device=Maguro
ro.product.device=t0ltecdma
我已经更新了代码以使用下面显示的“else if”
public void PropEdit() throws InterruptedException,IOException {
String origFile="data/data/app.props/build.orig.prop";
String propFile="data/data/app.props/build.new.prop";
try{
suProcess=Runtime.getRuntime().exec("su");
DataOutputStream os=new DataOutputStream(suProcess.getOutputStream());
os.writeBytes("chmod 777 /data/data/app.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();
}
}
尝试添加最后一个
else
语句add将最后一个println
放在那里
像这样
(...)
} else if (params[0].equalsIgnoreCase("ro.product.model")) {
out.println(params[0]+"="+"Galaxy Nexus");
/**out.println(line); **/
out.flush();
} else {
out.println(line);
}
(...)
请更正缩进。你试过调试你的代码吗?@我用过电话,很抱歉缩进问题,我一到笔记本电脑就会解决。