// TextFileHandle2.java import java.io.*; import java.util.*;; public class TextFileHandle2{//<1 //**************************************** static String keyExtract(String line){//<2 int idx = line.lastIndexOf('/'); if(idx == -1){//<3 return null; }else{//3><3 return line.substring(idx + 1); }//3> }//2> //*********************************** static int putLinesToHashtable (String inFileName, Hashtable<String, LinkedList<String>> ht){//<2 int putCount = 0; try{//<3 BufferedReader br1 = new BufferedReader(new FileReader(inFileName)); while(true){//<4 String line = br1.readLine(); if(line == null){//<5 break; }else{//5><5 String key = keyExtract(line); if(key != null){//<6 LinkedList<String> orgVal = ht.get(key); if(orgVal != null){//<7 orgVal.add(line); ht.put(key, orgVal); putCount++; }else{//7><7 LinkedList<String> newVal = new LinkedList<String>(); newVal.add(line); ht.put(key, newVal); putCount++; }//7> }//6> }//5> }//4> br1.close(); }catch(IOException e){//3><3 System.out.println("Catching " + e); }//3> return putCount; }//2> //********************** public static void main(String args[]){//<2 if(!(args.length >= 1)){//<3 System.out.println("At least one command-line arg is required."); System.exit(1); }//3> Hashtable<String, LinkedList<String>> ht1 = new Hashtable<String, LinkedList<String>>(); int count = putLinesToHashtable(args[0], ht1); System.out.println("count = " + count); BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); while(true){//<3 try{//<4. Input the class name to be searched. System.out.print( "What class? (for quit: just type `return'):"); String className = rd.readLine(); if(className.length() == 0){//<5. For just return. break; }//5> String key = className + ".class";// Search the entry. LinkedList<String> entries = ht1.get(key);
if(entries != null){//<5. When found, print it. System.out.println("** Number of entries = " + entries.size()); System.out.println(entries); }else{//5><5. Otherwise, just say so. System.out.println( className +" is not found."); }//5> }catch(IOException e){//4><4 System.out.println( "Catching " + e); return; }//4> }//3> }//2> }//1>
public static void main(String args[]) {// <2
if (!(args.length >= 1)) {// <3
System.out.println("At least one command-line arg is required.");
System.exit(1);
}// 3>
Hashtable<String, LinkedList<String>> ht1 = new Hashtable<String, LinkedList<String>>();
int count = putLinesToHashtable(args[0], ht1);
System.out.println("count = " + count);
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
while (true) {// <3
try {// <4. Input the class name to be searched.
System.out.print("What class? (for quit: just type `return'):");
String className = rd.readLine();
if (className.length() == 0) {// <5. For just return.
break;
}// 5>
String key = className + ".class";// Search the entry.
LinkedList<String> entries = findEntry(ht1, key);
if (entries != null) {// <5. When found, print it.
System.out.println("** Number of entries = " + entries.size());
System.out.println(entries);
} else {// 5><5. Otherwise, just say so.
System.out.println(className + " is not found.");
}// 5>
} catch (IOException e) {// 4><4
System.out.println("Catching " + e);
return;
}// 4>
}// 3>
}// 2>
}// 1>