Cfp*_*rta 0 java iostream file
我有一个文本文件,应该看起来像这样
我是哈坎。我的电子邮件地址是 hakan@cs.uh.edu,你叫什么名字?嗨,我叫 Tarikul,我最喜欢的电子邮件地址是 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]=
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止所拥有的,它并不多但是我如何让它在文本文件中找到电子邮件?
我也认为如果我在我的作业中添加实际说明会更好,我不要求任何人完成整个程序我将如何分离我需要找到的数据
- 连接到外部输入文件。输入文件的名称将始终是 inputemails.txt,因此请不要在您的程序中询问文件名。
- 检测文件中的电子邮件地址。
- 如果电子邮件没有子域,请为该电子邮件创建电子邮件对象。
- 如果电子邮件有子域,请为该电子邮件创建 UniversityEmail 对象。
- 请将所有电子邮件存储在同一个(一个)数组列表中。
- 将文件中的所有电子邮件复制到阵列列表后,请询问用户要包含在输出文件中的电子邮件类型。如果用户输入0,请在数组列表中写入没有子域的电子邮件。请注意,输出文件必须以表示文件中电子邮件数量的数字开头。如果用户输入 1-7 之间的数字,请在输出文件中写下来自特定部门的所有电子邮件。请注意,输出文件必须以表示文件中电子邮件数量的数字开头。用户只能输入 0 到 8 之间的一个整数
它正在谈论的子域是
1 艺术 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));
}
}
Run Code Online (Sandbox Code Playgroud)
您可以在此处找到读取文件的示例: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);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑(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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
输入文件内容:
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.
Run Code Online (Sandbox Code Playgroud)
输出:
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
Run Code Online (Sandbox Code Playgroud)
编辑:
如果你想要数组的形式:
String[] array=hs.toArray(new String[hs.size()]);
Run Code Online (Sandbox Code Playgroud)