package test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import static java.lang.System.out;
public class testString {
public static void main(String arg [])
{
File file = new File("e:/widget config sample.xml");
FileInputStream fileInputStream;
BufferedReader reader = null;
try {
fileInputStream = new FileInputStream(file);
reader = new BufferedReader(new InputStreamReader(fileInputStream));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String xmlString = "";
String temp ;
try {
while((temp = reader.readLine())!=null )
{
xmlString += temp;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.println(xmlString);
String one = "abcdefghij.klmnop.qrstuvwxyz";
String two = "abc.defghij.klmnop.qrstuvwxyz";
xmlString = xmlString.replaceAll(".*.<entries ws='", "").replaceAll("' />.*</widgets>", "/ws/maps") ;
one = one.replaceAll("cde.*xy", "");
two = two.replaceAll("cde.*xy", "");
out.println(xmlString);
out.println(one);
out.println(two);
}
}
输出结果:
<?xml version="1.0" encoding="UTF-8"?><widgets> <widget name='employee'><datasource name='titles' type='map'><entries ws='http://www.sina.com'/></datasource></widget></widgets>
http://www.sina.com/ws/maps
abz
abc.defghij.klmnop.qrstuvwxyz
.表示任何字符, *表示0个-无穷个
[b]
replaceAll第一个参数是正则,split也是使用正则[/b]