java小程序读取文件获取文件有用信息

import java.util.regex.*;
import java.io.*;
import java.lang.*;
public class GainNumber{
        public static void main(String[]args)throws Exception{
        /*
       读取文件  获取文件中的电话号码
      */
      //读取文件
       String Path="111.txt";  //文件路径
      //打开输入流
       FileInputStream input=new FileInputStream(Path);
       byte[] b=new byte[1024];                   //每次读取1024字节放入字节数组b中        
       int len=-1;
       StringBuffer sb=new StringBuffer();  
       String str;
       while((len=input.read(b))!=-1){           
       str=new String(b,0,len);      
       sb.append(str);                    //每次读取的字符串放入字符串缓冲区        
  }
  input.close();                        //关闭流
  gain(sb.toString());                 //将缓冲区中的数据转换成字符串传给获取方法
 }
 //获取文件信息
      public static void gain(String str){
              String regex="1[34578]\\d{9}";    //电话号码格式正则表达式 
		Pattern p=Pattern.compile(regex); //获取正则表达式
		Matcher m=p.matcher(str);         //获取匹配引擎    
		while(m.find()){                  
			System.out.println(m.group()); //获取并打印
                }
	}
}                

猜你喜欢

转载自blog.csdn.net/weixin_42758003/article/details/81146909