In: Computer Science
Using the provided m1_mbox.txt, write a program that reads the data from the file, locates the lines containing sender information (those lines begin with 'From:'), and extracts just the email address of the sender of each message. It should then duplicate the set of email addresses for senders, and write the duplicated list to a file called senders.txt, so that each sender appears in the output file only once. In addition, your program should print to the screen the following stats: total number of email messages in the file, and number of unique senders. Your program should expect the input and output files to be in the same directory and you should hardcode this into your program (that is, do not use command line arguments).
File filename = new File("m1_mbox.txt"); //File
Descriptor
FileReader fileReader = new FileReader(fileName); //File
Object
String words[] = null;
BufferedReader buffer = new BufferedReader(fileReader);
String searchFor = "From:";
String fileLine;
int totalEmails = 0;
int uniqueSendersCount = 0;
String sendersList[] = null;
String uniqueSendersList[] = null;
while(fileLine=buffer.readLIne() != null) {
words = s.split(" ");
for(int i = 0 ; i < words.length ; i++){
if(words[i].equals(searchFor)){
++ totalEmails;
if( !Arrays.asList(senders).contains(words[i+1]) ){
++uniqueSendersCount;
uniqueSendersList.add(word[i+1]); //maintains a unique
senders list
}
sendersList.add(word[i+1]); //maintains all senders
list
};
};
System.out.println("Output:'\n'"+"--Total
Emails:"+totalEmails+'\n'+"--Unique
Count:"+uniqueSendersCount);
FileWriter fileWrite = new FileWriter("senders.txt");
fileWrite.write(uniqueSendersCount);