说再多没用 按你的需求 我给你写一个
public class ChangeFileContent {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "body{\n"+
"background: #ffffff;\n"+
"font-size;\n"+
"}\n"+
"hr{\n"+
"}";
//System.out.println(str);
changeContent(str,"body","background","helloworld");
}
//str 读取的文本拼成一个字符串blockType 块名称 attribute 属性名称 content 想要修改的文本
public static void changeContent(String str,String blockType,String attribute,String content){
String blockStr = str.substring(str.indexOf(blockType+"{"),1+str.indexOf("}", str.indexOf(blockType+"{")));
//System.out.println(blockStr);
String attrStr = blockStr.substring(blockStr.indexOf(attribute), 1+blockStr.indexOf(";",blockStr.indexOf(attribute)));
//System.out.println(attrStr);
String oldAttr = attrStr.substring(attrStr.indexOf(":")+1,attrStr.indexOf( ";"));
//System.out.println(oldAttr);
String newAttrStr = attrStr.replace(oldAttr, content);
//System.out.println(newAttrStr);
str = str.replace(attrStr, newAttrStr);
System.out.println(str);
}
}