java读取单个文件内容

做个笔记免得每次都去找,重新写
	public static void main(String[] args) throws IOException{
		Pattern pt = Pattern.compile("<div id=\"article\">([\\s\\S]*?)<div class=\"clear\">");
		String web = readFileByLines("C:\\Users\\tianjun\\Desktop\\search\\ch10\\heritrixproject\\jobs\\xiaoxue-20150314050416274\\mirror\\xiaoxue.hujiang.com\\yi\\shuxue\\p324532\\index.html");
		web = new String(web.getBytes("gbk"),"utf-8");
		Matcher mc =pt.matcher(web);
		if(mc.find()){
			String content = mc.group(1).trim();
			System.out.println(content);
		}

	}
 public static String readFileByLines(String fileName) {
        File file = new File(fileName);
        StringBuffer sb=new StringBuffer();
        BufferedReader reader = null;
        try {
            System.out.println("==========================:");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
               sb.append(tempString);
               sb.append("\n");
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
		return sb.toString();
    }


你可能感兴趣的:(java读取单个文件内容)