标签:
原始配置文件build.prop 如下图:
我们主要修改红色框框中的属性值
ro.product.model
ro.product.brand
ro.product.board
ro.product.manufacturer
写一段测试程序:如下:
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class ConfigModify { public static void main(String[] args) { File file = new File("c:\\build.prop"); if(file.exists()){ System.out.println("build.prop配置文件存在..."); //得到文件流 FileInputStream fin = null; BufferedReader reader = null; //以行为单位,将字符串放入数组 String[] array = null; //原始配置信息字符串 String originalContent = null; try{ fin = new FileInputStream(file); reader = new BufferedReader(new InputStreamReader(fin)); StringBuilder sb = new StringBuilder(); String str = ""; while((str = reader.readLine())!=null){ sb.append(str+"\n"); } originalContent = sb.toString(); reader.close(); fin.close(); //关闭文件流 System.out.println("读取原始配置信息成功..."); System.out.println("关闭文件输入流..."); }catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); }catch (Exception e) { e.printStackTrace(); } //以换行为分隔 返回字符串数组 array = originalContent.split("\n"); for (int i = 0; i < array.length; i++) { if(array[i].contains("ro.product.model")){ array[i] = "ro.product.model=iPhone 6 Plus"; }else if(array[i].contains("ro.product.brand")){ array[i] = "ro.product.brand=iPhone"; }else if(array[i].contains("ro.product.board")){ array[i] = "ro.product.brand=iPhone"; }else if(array[i].contains("ro.product.manufacturer")){ array[i] = "ro.product.manufacturer=iPhone"; }else { } } FileOutputStream fos = null; BufferedWriter writer = null; try { StringBuilder sBuilder = new StringBuilder(); for (int i = 0; i < array.length; i++) { sBuilder.append(array[i]+"\n"); } String finalMsg = sBuilder.toString(); System.out.println("######################"); fos = new FileOutputStream(file); writer = new BufferedWriter(new OutputStreamWriter(fos)); writer.append(finalMsg); writer.flush(); writer.close(); System.out.println("替换配置信息成功..."); System.out.println("写入新配置信息成功..."); System.out.println("关闭文件输出流..."); fos.close(); }catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); }catch (Exception e) { e.printStackTrace(); } }else { System.out.println("build.prop file is not exist"); } } }
输出信息如下:
替换后的配置文件如下: 可以看到红色框框中的信息已经被修改了。
标签:
原文地址:http://www.cnblogs.com/sphere/p/4745781.html