我正在研究涉及类似于 CSS 的文件结构但它与
CSS 有点不同的东西。这是文件的结构
<ELEMENT NAME>{
Element attributes..1
Element attributes..2
Element attributes..3
}
我写了一个方法来获取元素名称
public String getElementName(File jSfile){
String elementName=null;
StringBuffer sb = null;
try{
BufferedReader br=new BufferedReader(new FileReader(jSfile));
String line=null;
while((line=br.readLine())!=null){
Pattern element=Pattern.compile("\\<(.+?)\\>",Pattern.DOTALL);
Matcher match=element.matcher(line);
match.find();
return match.group(1);
}
}
catch(Exception e){
return e.getLocalizedMessage();
}
return elementName;
}
并像这样使用它..
public static void main(String arg[]){
CSSReader cs=new CSSReader();
File f=new File("C:/Users/foo/bar/cascade.xyz");
String z=cs.getElementName(f);
System.out.print(z);
}
但它总是说'No match found'
编辑我发现该文件包含超过 1 个具有不同名称的序列。当我删除所有其他的并只保留一个时,代码就可以工作了。
很抱歉在这里是菜鸟.....有人知道我将如何使用多线......非常感谢我哪里错了