import java.io.*;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FileToString {
public static String readFile(String fileName) {
String output = "";
File file = new File(fileName);
if (file.exists()) {
if (file.isFile()) {
try {
BufferedReader input = new BufferedReader(new FileReader(
file));
StringBuffer buffer = new StringBuffer();
String text;
while ((text = input.readLine()) != null)
buffer.append(text + "\n");
output = buffer.toString();
} catch (IOException ioException) {
System.err.println("File Error!");
}
} else if (file.isDirectory()) {
String[] dir = file.list();
output += "Directory contents:\n";
for (int i = 0; i < dir.length; i++) {
output += dir[i] + "\n";
}
}
} else {
System.err.println("Does not exist!");
}
return output;
}
public static void main(String args[]) {
String str = readFile("D:/style.txt");
Pattern pattern = Pattern.compile("<a href=\"(.+?)\" rel=\"prettyPhoto");
Matcher matcher = pattern.matcher(str);
Map<String, String> map=new LinkedHashMap<String, String>();
while (matcher.find()) {
map.put(matcher.group(1), matcher.group(1));
}
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
System.out.println(entry.getValue());
}
}
}
读取文件and正则表达式提取
最新推荐文章于 2022-07-15 22:35:04 发布