java - 在文本文件中查找电子邮件并对电子邮件信息进行排序

标签 java file iostream

我有一个文本文件,应该看起来像这样

I am Hakan. My email address is hakan@cs.uh.edu, and what is your name? Hi my name is Tarikul and my favorite email address is tarikul2000@uh.edu

我应该创建一个程序,该程序将在存储用户名、域名、子域和扩展名的文件中查找电子邮件,然后将它们重写到另一个文本文件中。

import java.io.* ;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.FileOutputStream;

public class Try {
    public static void main(String[] args) {
        Email [] storage;// email is a class that was made to store the data
        try {
            Scanner input= new Scanner("inputemails.txt");
            PrintWriter output= new PrintWriter("outputemails.txt");

            }

         catch (FileNotFoundException e) {
            System.out.print("File not found");
            System.exit(0);}
        int i=0;
        while(input.hasNextLine()){
            if(input.contains("@"));
            {
                storage[i]=
            }
        }


    }

}

这是我到目前为止所拥有的,它不多,但我如何让它在文本文件中找到电子邮件?

我还认为,如果我将实际说明添加到我的作业中会更好,我不会要求任何人完成整个程序,只是我如何分离我需要查找的数据

  1. Connect to the external input file. The name of the input file will always be inputemails.txt, and therefore please don’t ask the file name in your program.
  2. Detect the email addresses in the file.
  3. If an email does not have a sub-domain, please create Email object for that email.
  4. If an email has a sub-domain, please create UniversityEmail object for that email.
  5. Please store all emails in the same (one single) array list.
  6. After you copy all emails from the file to the array list, please ask the user what type of emails to be included in the output file. If the user enters 0, please write the emails that do not have sub-domain in the array list. Please note that the output file must start with a number indicating the number of emails in the file. If the user enters a number between 1-7, please write all emails from the specific department in the output file. Please note that the output file must start with a number indicating the number of emails in the file. The user can enter only one integer number from 0 to 8

它所讨论的子域是

1 art 2 chee 3 chem 4 coe 5 cs 6 egr 7 polsci

最佳答案

public static void fillEmailsHashSet(String line,HashSet<String> container){

        Pattern p = Pattern.compile("([\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Za-z]{2,4})");
        Matcher m = p.matcher(line);

        while(m.find()) {
            container.add(m.group(1));
        }

    }

您可以在此处找到读取文件的示例:https://stackoverflow.com/a/22074145/3315914

编辑:

输入/输出示例:

public static void emails() {
        HashSet<String> hs = new HashSet<>();
        FileReader file = null;
        try {
            file = new FileReader(new File("emails.txt"));
        } catch (FileNotFoundException e1) {
            System.err.println("File emails.txt not found!");
            e1.printStackTrace();
        }
        BufferedReader br = new BufferedReader(file);
        String line;
        try {
            while ((line = br.readLine()) != null) {
                fillEmailsHashSet(line, hs);
            }

        } catch (IOException e) {
            System.err.println("Error when reading");
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    System.err.println("Unexpected error");
                    e.printStackTrace();
                }
            }
        }
        for (String string : hs) {
            System.out.println(string);
        }
    }

编辑(2):更紧凑的方法是使用 try-with-resources

public static void emails() throws IOException {
    HashSet<String> hs = new HashSet<>();
    FileReader file = null;
    try (BufferedReader br = new BufferedReader(new FileReader(new File("emails.txt")))) {
        String line;
        while ((line = br.readLine()) != null) {
            fillEmailsHashSet(line, hs);
        }
        for (String string : hs) {
            System.out.println(string);
        }
    }
}

输入文件内容:

dolor ac egestas purus TheBumpy@whatever.com Vestibulum justo. magna vulputate Morbi TheBlue@whatever.com
TheJudicious@whatever.com Nulla nec dui. adipiscing in TheOpen@whatever.com TheFascinated@whatever.com
sapien urna mi TheHarmonious@whatever.com vitae aliquam In eget Pellentesque ThePhysical@whatever.com tellus.
non sollicitudin faucibus et mi justo, sit nibh dapibus potenti. venenatis venenatis, molestie sed Proin fermentum elementum.
Sed ut velit venenatis TheDidactic@whatever.com dignissim
consequat condimentum, porttitor Lorem nibh felis,
ullamcorper eros. ut diam vel ipsum nisi luctus. ultrices sem amet non Aliquam aliquet lobortis mauris Vestibulum est purus dignissim
Etiam Cras in eget, Sed pellentesque, ThePhobic@whatever.com eu amet,
Mauris magna euismod, odio semper lorem, potenti. dui tellus.
TheDetailed@whatever.com Ut ipsum mi non Suspendisse tempus cursus ac nec TheMiniature@whatever.com semper. ac, quis suscipit posuere,
posuere Suspendisse Donec tristique a sagittis  vel vitae urna Aliquam vehicula sit condimentum. mi risus mollis rutrum varius. nec elit.
nulla Fusce TheKaput@whatever.com sagittis dictum nunc, eget in TheAmusedGamer@gmail.com venenatis consectetur ultricies. interdum fermentum.
ante TheJolly@whatever.com eros quam. nec TheElectric@whatever.com blandit. massa. molestie ac, TheHistorical@whatever.com purus, ligula fringilla
imperdiet lorem neque. et blandit tortor. enim sed, magna.

输出:

ThePhysical@whatever.com
TheHistorical@whatever.com
TheAmusedGamer@gmail.com
TheBlue@whatever.com
TheKaput@whatever.com
TheMiniature@whatever.com
TheFascinated@whatever.com
ThePhobic@whatever.com
TheBumpy@whatever.com
TheDetailed@whatever.com
TheHarmonious@whatever.com
TheJudicious@whatever.com
TheElectric@whatever.com
TheJolly@whatever.com
TheOpen@whatever.com
TheDidactic@whatever.com

编辑:

如果你想要数组的形式:

String[] array=hs.toArray(new String[hs.size()]);

关于java - 在文本文件中查找电子邮件并对电子邮件信息进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22305773/

相关文章:

java - JSP 条件语句

java - 将 "Map<UUID, String>"接口(interface)替换为更简单的 "IMap"

c# - SharpSVN 读取所有文件名

file - salt 栈 : Transfer multiple files (using wildcard? )

Java读取文本文件并将每一行保存为新的字符串数组

c++ - C++ 表达式中尾随流操纵符的含义

c++ - 如何判断 `std::getline()`提取了多少个字符?

java - 如何在 Java 中确定数组中的颜色

c++ - 可以轮询标准 C++ iostream 操纵器的状态吗?

java - Matcher.appendEvaluated() 上出现 NullPointerException?