2013-05-03 40 views
-1

我试图通过替换文件中的3行来编辑build.prop文件。以下方法保留原始行并将新行复制到新文件。我需要更换线路而不是添加新线路。替换文件中的多行不工作

我已经更新了我的代码使用“否则,如果”图所示

/**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(); 
} 
} 
+0

你用'params []'表示什么意思? – 2013-05-03 21:15:45

+1

向我们展示迄今为止尝试的代码! – Smit 2013-05-03 21:16:32

+0

@ Versatile1你的实现看起来不错,那么问题在哪里?如果你显示这两个文件和你的预期输出,可能会有所帮助。 – Smit 2013-05-03 21:43:18

回答

0

编辑:如果我的理解正确,应该这样做:

//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",); 

或者

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(); 
} 

注 “(我)” 是忽略大小写。

+0

我实际上有.equalsIgnoreCase(params [0])在使用中,我已经与那场战斗做了:)但是谢谢 – Versatile1 2013-05-03 22:02:24

+0

好。我只是猜测。 – pt2121 2013-05-03 22:03:51

+0

请参阅我的更新 – pt2121 2013-05-03 22:09:08

0

更改此代码:

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); 
}