Need help with the following java program requirement. Any help is greatly appre
ID: 3659794 • Letter: N
Question
Need help with the following java program requirement. Any help is greatly appreciated!
Explanation / Answer
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;
public class ProcessFile {
public static void main(String args[]) throws Exception {
if(args != null && args.length >= 1) {
ProcessFile processor = new ProcessFile();
processor.process(args[0]);
} else
System.out.println("Please enter log file name");
}
public void process(String logFile) throws Exception {
//Read the file
FileInputStream fstream = new FileInputStream(logFile);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
HashMap sources = new HashMap ();
HashMap destinations = new HashMap ();
while ((strLine = br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(strLine," "); //space as delimter
if (st.countTokens() > 0) {
String src =st.hasMoreTokens() ? st.nextToken() : null;
String dest = st.hasMoreTokens() ? st.nextToken() : null;
//Map sources and destinations into a map
if(src != null) {
sources.put(src, sources.get(src)+","+dest);
//Add check for unique sources/destinations - search in the string of returned by get
destinations.put(dest, destinations.get(dest)+","+src);
}
}
}
//Close the input stream
in.close();
//Now we have sources and destinations print them in whatever way u need.
if(!sources.isEmpty()) {
for(String key: sources.keySet()) {
System.out.println("Source Name: "+key+" ");
System.out.println("Destinations separated by ',' "+sources.get(key));
//Note : Calculate count of destinations using string tokenizer
}
}
if(!destinations.isEmpty()) {
for(String key: destinations.keySet()) {
System.out.println("Destination Name: "+key+" ");
//Note : Calculate count of Sources using string tokenizer
System.out.println("Sources separated by ',' "+destinations.get(key));
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.