Java读取TXT文件方法,和检索链接<a>[url]
2014-03-05 19:45
477 查看
import java.util.*; import java.io.*; import java.util.regex.*; public class AddressDectect { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub StringBuffer buffer = new StringBuffer(); try { String encoding="GBK"; File file=new File("getUrl.txt"); if(file.isFile() && file.exists()){ //判断文件是否存在 InputStreamReader read = new InputStreamReader( new FileInputStream(file),encoding);//考虑到编码格式 BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; while((lineTxt = bufferedReader.readLine()) != null){ buffer.append(lineTxt); } read.close(); }else{ System.out.println("找不到指定的文件"); } } catch (Exception e) { System.out.println("读取文件内容出错"); e.printStackTrace(); } String reg = "(<(\\s*?)a{1}[^>]*>.*?<(\\s*?)/(\\s*?)a(\\s*?)>)|(\\[(\\s*?)url{1}[^\\]]*\\].*?\\[(\\s*?)/(\\s*?)url(\\s*?)\\])"; Pattern pattern = Pattern.compile(reg); Matcher matcher = pattern.matcher(buffer); while(matcher.find()){ String result = matcher.group(); System.out.println(result); } } }
目前还只是检索<a> 和<url>
相关文章推荐
- java 从jar中读取文件 三种方法<Enumeration>
- <Java>使用String读取文件名称获取得定位置的字符串
- 读取xml文件转成List<T>对象的两种方法
- java 从jar中读取文件 三种方法<Enumeration>
- <转>BufferedReader和FileReader读取txt文件乱码
- 读取xml文件转成List<T>对象的两种方法
- 读取xml文件转成List<T>对象的两种方法(附源码)
- java中addAll(Collection<? extends E> col)方法实例
- 使用<jsp:include>,不想写死URL,动态生成URL的解决的方法
- 用java读取一个文件往hbase里插入数据(List<PUT>)
- java读取TXT文件的方法
- Java学习笔记(八):Map<stirng,string>的遍历的四种方法
- java读取TXT文件的方法
- Java方法中 访问url 下载链接,下载文件
- 设置<a>链接颜色样式的几种方法
- java读取TXT文件的方法
- SpringMVC中的web.xml文件 <url-pattern>配置说明 3种方法
- Java读取UTF-8格式txt文件第一行出现乱码——问号“?”及解决;Java读带有BOM的UTF-8文件乱码原因及解决方法
- java读取txt文件内容,存放到list的方法
- 方法重载(overload)的例子分析(摘自<深入理解Java虚拟机>)