Buff Reading
by in CodeSOD on 2025-05-21Frank inherited some code that reads URLs from a file, and puts them into a collection. This is a delightfully simple task. What could go wrong?
static String[] readFile(String filename) {
String record = null;
Vector vURLs = new Vector();
int recCnt = 0;
try {
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
record = new String();
while ((record = br.readLine()) != null) {
vURLs.add(new String(record));
//System.out.println(recCnt + ": " + vURLs.get(recCnt));
recCnt++;
}
} catch (IOException e) {
// catch possible io errors from readLine()
System.out.println("IOException error reading " + filename + " in readURLs()!\n");
e.printStackTrace();
}
System.out.println("Reading URLs ...\n");
int arrCnt = 0;
String[] sURLs = new String[vURLs.size()];
Enumeration eURLs = vURLs.elements();
for (Enumeration e = vURLs.elements() ; e.hasMoreElements() ;) {
sURLs[arrCnt] = (String)e.nextElement();
System.out.println(arrCnt + ": " + sURLs[arrCnt]);
arrCnt++;
}
if (recCnt != arrCnt++) {
System.out.println("WARNING: The number of URLs in the input file does not match the number of URLs in the array!\n\n");
}
return sURLs;
} // end of readFile()