package com.taotao.banbenhao.config;
import java.io.*;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FileBytes {
public static StringBuffer readFile(File file) throws IOException, ParseException {
InputStreamReader read = null;//考虑到编码格式
String lineTxt = null;
StringBuffer sf = new StringBuffer();
try {
read = new InputStreamReader(new FileInputStream(file), "utf-8");
BufferedReader bufferedReader = new BufferedReader(read);
while ((lineTxt = bufferedReader.readLine()) != null) {
lineTxt += bufferedReader.readLine();
sf.append(lineTxt);
// System.out.println(lineTxt);
}
read.close();
return sf;
} catch (Exception e) {
e.getCause();
}
return sf;
}
public static void main(String[] args) {
File file = new File("D:\\works\\myfile\\gongju\\nginx-1.15.2\\conf\\all-servers\\test.conf");
if (!file.exists()) {
System.out.println("文件不存在!");
return;
}
try {
StringBuffer ym = readFile(file);
//System.out.println(ym);
// Pattern p = Pattern.compile("(?:com\\.cn|net\\.cn|org\\.cn|com|net|org|cn|biz|info|cc|tv)", Pattern.CASE_INSENSITIVE);
Pattern p = Pattern.compile("server_name.*?;"); //以server_name开始 ; 结束
Matcher m = p.matcher(ym);
List<String> result = new ArrayList<String>();
while (m.find()) {
result.add(m.group(0));
}
for (String d : result) {
d = d.replace("server_name", "");
d = d.replace(";", "");
System.out.print(d);
}
// System.out.print(result.toString());
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}