关于如何将文件内容提取(正则表达式)

前端之家收集整理的这篇文章主要介绍了关于如何将文件内容提取(正则表达式)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

test.properties:


region_data UA:444 PPA:444;
comparison_data UA:333 PPA:888;

文件内容类:



public class DataForm {
private String destType;
private String dataBaseRegion;
private String InstanceId;



DataForm(String destType,String dataBaseRegion,String InstanceId){
this.destType = destType;
this.dataBaseRegion = dataBaseRegion;
this.InstanceId = InstanceId;
}

public String getdestType() {
return destType;
}


public void setdestType(String dataType) {
this.destType = dataType;
}


public String getDataBaseRegion() {
return dataBaseRegion;
}




public void setDataBaseRegion(String dataBaseRegion) {
this.dataBaseRegion = dataBaseRegion;
}




public String getInstanceId() {
return InstanceId;
}




public void setInstanceId(String instanceId) {
InstanceId = instanceId;
}




public static void main(String[] args) {
// TODO Auto-generated method stub


}


}

操作类:

import java.io.BufferedReader;

import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; /** * 注意在test.properties文件中要按照格式进行填写 * example : 最后都要以;结束。 * @author linyuan * */ public class FileTest{ private static String regexForRegion = ";"; private static String regexFordata = " "; private static String regexForDetails = ":"; private static String regionString; private static String comparisonString; public static String[] getRegexForRegion(StringBuffer in) { Pattern p = Pattern.compile(regexForRegion); String[] region = p.split(in); return region; } public static String[] getRegexFordata(String in) { Pattern p = Pattern.compile(regexFordata); String[] region = p.split(in); return region; } public static String[] getregexForDetails(String in) { Pattern p = Pattern.compile(regexForDetails); String[] region = p.split(in); return region; } public static void main(String[] args) throws IOException { /** * 读取文件内容 */ List<DataForm> dataSource = new ArrayList<DataForm>(); StringBuffer buffer = new StringBuffer(); BufferedReader reader = null; InputStream is = null; try{ is = new FileInputStream("test.properties"); String line; // 用来保存每行读取的内容 reader = new BufferedReader(new InputStreamReader(is)); line = reader.readLine(); // 读取第一行 while (line != null) { // 如果 line 为空说明读完了 buffer.append(line); // 将读到的内容添加到 buffer 中 buffer.append("\n"); // 添加换行符 line = reader.readLine(); // 读取下一行 } /** * 开始进行文件文件内容匹配 */ String[] region = getRegexForRegion(buffer);//获得数据源和参考数据库字符串 String[] regionDataPair; String[] comparisonDataPair; Pattern p = Pattern.compile(regexForRegion); regionString = region[0].substring("region_data ".length(),region[0].length()); comparisonString = region[1].replaceAll("comparison_data ",""); regionDataPair = getRegexFordata(regionString); comparisonDataPair = getRegexFordata(comparisonString);//获得到数据对 for(int i = 0; i < regionDataPair.length;i++) { String[] data = getregexForDetails(regionDataPair[i]); dataSource.add(new DataForm("region",data[0],data[1])); } for(int i = 0; i < comparisonDataPair.length;i++) { String[] data = getregexForDetails(comparisonDataPair[i]); dataSource.add(new DataForm("comparison",data[1])); } }catch(Exception e) { e.printStackTrace(); } finally{ reader.close(); is.close(); } for(int j = 0; j < dataSource.size();j++) { System.out.println("目标是"+dataSource.get(j).getdestType()+",数据库类型是"+dataSource.get(j).getDataBaseRegion()+",实例代号是"+dataSource.get(j).getInstanceId()+"\n"); } } }

猜你在找的正则表达式相关文章