第一个问题:邮箱的正则式是什么,网上有很多邮箱正则式,但是都不适用。要读文件的内容是很多字符串,比如所<fjien>¥……fjiefne mail:joe at cs.stanford.edu(fenix#&(&(^&%%fjien fneifb...
第二个问题就是我将结果输入到email文件中去的时候不能输入完整的结果,往往只能打印最后一个字符串结果import java.io.*;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class exercise6 {
@SuppressWarnings("resource")
public static void main(String[] args) throws Exception {
  
  String path="C:\\Users\\Administrator\\Desktop\\新建文件夹";
 
  File file1=new File("email.txt");//存储结果的文件email
  new PrintWriter(file1);//没有email文件的话建立一个
  
  File fi=new File(path);
  //path是要更改的文件所在的文件夹
  File[] file=fi.listFiles();
  System.out.println(file.length);//显示文件夹下的文件数
  
  for (int i = 0; i < file.length; i++) {
String filepath=file[i].getAbsolutePath();

    String cont = read(filepath);
    //读取一个文件的内容存放在cont中
 
    cont = cont.replaceAll(" at ", "@");
    //更新源文件
    
    
    write(findEmail(cont),file1);
    System.out.print(findEmail(cont));//控制台显示筛选的完整的结果
    //找出邮箱字符串并且将结果存放在file1中
    
  }
  
}


public exercise6() {

}


public static String read(String path) {
File file = new File(path);
StringBuffer res = new StringBuffer();
  String line = null;
  try {
   BufferedReader reader = new BufferedReader(new FileReader(file));
   while ((line = reader.readLine()) != null) {
    res.append(line + "\n");//读出来的文本内容存在res里
   }
   reader.close();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return res.toString();
 }
 public static void write(String cont, File dist) {
 try {
 PrintWriter output=new PrintWriter(dist);
 output.print(cont);
 output.close();
 } 
 catch (IOException e) {
   e.printStackTrace();
 }
 }  public static String findEmail(String cont){//以下邮箱正则式有待搞清楚
 Pattern pattern = Pattern.compile("@.*");
 Matcher matcher = pattern.matcher(cont);
 StringBuffer record=new StringBuffer();
 
 while(matcher.find()){
 record.append(matcher.group()+" ");
 }
 
 return record.toString();
 }}
Java文件ioEmail正则表达式

解决方案 »

  1.   

    忘记在哪个帖子里看到的了。。
        public static void main(String[] args)
        {
            // TODO Auto-generated method stub
            String[] mails = {"[email protected]", "ssw@126", "[email protected]"};
            String mailRegEx = "\\w{1,20}@\\w+\\.(com|cn|org|net|gov)";
            Pattern mailPattern = Pattern.compile(mailRegEx);
            Matcher matcher = null;
            for (String mail : mails)
            {
                matcher = mailPattern.matcher(mail);
                if (matcher.matches())
                {
                    System.out.println(mail + "是个有效的邮件地址!");
                }
                else
                {
                    System.out.println(mail + "不是个有效的邮件地址!");
                }
            }
        }
      

  2.   

    package likeafool;import java.io.*;
    import java.util.Scanner;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class exercise6 {
    @SuppressWarnings("resource")
    public static void main(String[] args) throws Exception {
      //Scanner input=new Scanner(System.in);
      //String path=input.next();//从控制台获得需要处理的文件所在的文件夹路径
      String path="C:\\Users\\Administrator\\Desktop\\新建文件夹";
     
      File file1=new File("email.txt");//存储结果的文件email
      new PrintWriter(file1);//没有email文件的话建立一个
      
      File fi=new File(path);
      //path是要更改的文件所在的文件夹
      File[] file=fi.listFiles();
      System.out.println(file.length);//显示文件夹下的文件数
      
      for (int i = 0; i < file.length; i++) {
    String filepath=file[i].getAbsolutePath();

        String cont = read(filepath);
        //读取一个文件的内容存放在cont中
     
        cont = cont.replaceAll(" at ", "@");
        //更新源文件
        
        
        write(findEmail(cont),file1);
        System.out.print(findEmail(cont));
        //找出邮箱字符串并且将结果存放在file1中
        
      }
      
    }


    public exercise6() {

    }


    public static String read(String path) {
    File file = new File(path);
    StringBuffer res = new StringBuffer();
      String line = null;
      try {
       BufferedReader reader = new BufferedReader(new FileReader(file));
       while ((line = reader.readLine()) != null) {
        res.append(line + "\n");//读出来的文本内容存在res里
       }
       reader.close();
      } catch (FileNotFoundException e) {
       e.printStackTrace();
      } catch (IOException e) {
       e.printStackTrace();
      }
      return res.toString();
     }
     public static void write(String cont, File dist) {
     try {
     PrintWriter output=new PrintWriter(dist);
     output.print(cont);
     output.close();
     } 
     catch (IOException e) {
       e.printStackTrace();
     }
     }  public static String findEmail(String cont){//邮箱正则式有待改
     Pattern pattern = Pattern.compile("\\w{1,20}@\\w+\\.(com|cn|org|net|gov|edu)");
     Matcher matcher = pattern.matcher(cont);
     StringBuffer record=new StringBuffer();
     
     while(matcher.find()){
     record.append(matcher.group()+" ");
     }
     
     return record.toString();
     }}
      

  3.   

    虽然正则式还是有些问题 但是我的第二个问题出在哪儿呢?文件io问题,源代码在楼上
    稍微修改:
              //这里
              StringBuffer all = new StringBuffer();
              for (int i = 0; i < file.length; i++) {
                String filepath=file[i].getAbsolutePath();
             
                String cont = read(filepath);
                //读取一个文件的内容存放在cont中
              
                cont = cont.replaceAll(" at ", "@");
                //更新源文件
                 
                all.append(findEmail(cont)+"   ");
                
                //找出邮箱字符串并且将结果存放在file1中
                 
              }
              write(all.toString(),file1);
              System.out.print(all.toString());
      

  4.   

    或者write方法修改下:
    PrintWriter output=new PrintWriter(new FileOutputStream(dist,true));
      

  5.   

    好的 O(∩_∩)O谢谢~第二个问题解决了但是为什么把write方法改成这样就可以了,原来的错在哪里,感觉逻辑上没什么错误啊?一整串的字符串cont为什么写到文件的只有一部分?
      

  6.   

    请给出你所要求的邮箱的格式规范说明,这样就有了构建正则表达式的依据。文件中出现的邮箱,应该有一定的范围吧,
    比如,(只是假设)只有163.com和qq.com因为@后面出现的域名一般固定,所以最好请说明下,你所说的文件中包含的邮箱格式如何。
    例如,邮箱@后包含那些域名(如qq.com),邮箱的名字有什么限制(注册邮箱时,名字一般是有限制的,比如长度,只能出现字符、数字及下划线等)
      

  7.   

    好的 O(∩_∩)O谢谢~第二个问题解决了但是为什么把write方法改成这样就可以了,原来的错在哪里,感觉逻辑上没什么错误啊?一整串的字符串cont为什么写到文件的只有一部分? 
      

  8.   

    LZ可以仔细看看API文档:如果文件存在,会将它的长度设置为0.意味着每次打开,都会从头写文件。
      

  9.   

    就是一堆文件里面 都是连续的字符串 字符串里面夹杂着邮箱 比如说(650) 725-6949<BR>
     Email:  <A
    HREF="mailto:[email protected]">[email protected]</A><BR>
    <BR>Address: 
    <DL>
    <DD>Department of Electrical Engineering
      

  10.   

    那就稍微改下方式:
     
                matcher = mailPattern.matcher(mail);
                while(matcher.find())
                {
                    System.out.println(matcher.group() + "是个有效的邮件地址!");
                }LZ也好好研究下API,这样印象更深些~