bobrivers2003
Technical User
Make a brew first!
I am creating some classes that calculate the checksum of all files within a ear archive and saves these results to a report.txt file. It works a treat and computes a checksum for all files even if there are within a directory, within a directry etc.
The problem I am having is printing the results to a txt file. I have two columns: col 1 = path and file name, col 2 = checksum value. Because the paths and filenames vary greatly in length the column of checksum values is a mess and not aligned. I wish to change this.
A friend reccommended putting the values (filename, checksum) into a hashmap/table, order the entries, work out the length of each entry and based on the length add certain number of \t's to the output to sort out alignment.
I have never touched hashmaps/tables and I am having a nightmare. I want the path/filename to be the key and the checksum result to be the entry.
writeReport(string result, File fileName) is method in ReportWriter class that writes to report.txt
The error I get is "ERROR java.lang.ClassCastException: java.util.Hashtable$Entry"
If there is any advice I would greatly appreciate it. Thank you for reading this thread.
My Code:
import java.io.File;
import java.util.Hashtable;
import java.util.zip.CRC32;
import java.util.HashMap;
import java.util.Vector;
import java.util.Iterator;
public class ListDirectory {
static MD5Checksum md5 = null;
static ComputeCRC32 crc = null;
static ReportWriter rw = null;
static File checkFile = null;
static Hashtable map;
public static void listDirectory(String tempDir, String reportFile) {
map = new Hashtable();
md5 = new MD5Checksum();
crc = new ComputeCRC32();
rw = new ReportWriter();
Vector zipFile = new Vector();
File dir = new File(tempDir);
File[] fileArray = dir.listFiles();
if (fileArray == null) {
// Either dir does not exist or is not a directory
} else {
for (int i = 0; i < fileArray.length; i++) {
String stringFile = fileArray.toString();
if (fileArray.isDirectory()) {
System.out.println("DIRECTORY " + fileArray);
rw.writeReport(fileArray + " IS A DIRECTORY ", reportFile);
String fileDir = fileArray.toString();
listDirectory(fileDir, reportFile);
} else {
try {
System.out.println("FILE " + fileArray);
//String result = md5.getMD5Checksum(stringFile);
String result = String.valueOf(crc.getChecksumValue(new CRC32(), stringFile));
System.out.println(fileArray + " CRC32 " + result);
map.put(fileArray, result);
formatReport(map, reportFile);
//rw.writeReport(fileArray + "" + '\t' + '\t' + '\t' + '\t'+ '\t' + '\t' + " CRC32 " + result, reportFile);
} catch (Exception e) {
System.out.println("ERROR " + e);
}
}
}
}
}
public static void formatReport(Hashtable map, String reportFile) {
Hashtable htMap = map;
Iterator iter = htMap.entrySet().iterator();
int maxSize = 0;
while (iter.hasNext()) {
String fileName = (String) iter.next();
String crc = (String)map.get(fileName);
System.out.println("ITER" + iter);
if (fileName.length() > maxSize) {
maxSize = fileName.length();
// Do some stuff to add tabs etc
// and write to file
}
}
}
}
I am creating some classes that calculate the checksum of all files within a ear archive and saves these results to a report.txt file. It works a treat and computes a checksum for all files even if there are within a directory, within a directry etc.
The problem I am having is printing the results to a txt file. I have two columns: col 1 = path and file name, col 2 = checksum value. Because the paths and filenames vary greatly in length the column of checksum values is a mess and not aligned. I wish to change this.
A friend reccommended putting the values (filename, checksum) into a hashmap/table, order the entries, work out the length of each entry and based on the length add certain number of \t's to the output to sort out alignment.
I have never touched hashmaps/tables and I am having a nightmare. I want the path/filename to be the key and the checksum result to be the entry.
writeReport(string result, File fileName) is method in ReportWriter class that writes to report.txt
The error I get is "ERROR java.lang.ClassCastException: java.util.Hashtable$Entry"
If there is any advice I would greatly appreciate it. Thank you for reading this thread.
My Code:
import java.io.File;
import java.util.Hashtable;
import java.util.zip.CRC32;
import java.util.HashMap;
import java.util.Vector;
import java.util.Iterator;
public class ListDirectory {
static MD5Checksum md5 = null;
static ComputeCRC32 crc = null;
static ReportWriter rw = null;
static File checkFile = null;
static Hashtable map;
public static void listDirectory(String tempDir, String reportFile) {
map = new Hashtable();
md5 = new MD5Checksum();
crc = new ComputeCRC32();
rw = new ReportWriter();
Vector zipFile = new Vector();
File dir = new File(tempDir);
File[] fileArray = dir.listFiles();
if (fileArray == null) {
// Either dir does not exist or is not a directory
} else {
for (int i = 0; i < fileArray.length; i++) {
String stringFile = fileArray.toString();
if (fileArray.isDirectory()) {
System.out.println("DIRECTORY " + fileArray);
rw.writeReport(fileArray + " IS A DIRECTORY ", reportFile);
String fileDir = fileArray.toString();
listDirectory(fileDir, reportFile);
} else {
try {
System.out.println("FILE " + fileArray);
//String result = md5.getMD5Checksum(stringFile);
String result = String.valueOf(crc.getChecksumValue(new CRC32(), stringFile));
System.out.println(fileArray + " CRC32 " + result);
map.put(fileArray, result);
formatReport(map, reportFile);
//rw.writeReport(fileArray + "" + '\t' + '\t' + '\t' + '\t'+ '\t' + '\t' + " CRC32 " + result, reportFile);
} catch (Exception e) {
System.out.println("ERROR " + e);
}
}
}
}
}
public static void formatReport(Hashtable map, String reportFile) {
Hashtable htMap = map;
Iterator iter = htMap.entrySet().iterator();
int maxSize = 0;
while (iter.hasNext()) {
String fileName = (String) iter.next();
String crc = (String)map.get(fileName);
System.out.println("ITER" + iter);
if (fileName.length() > maxSize) {
maxSize = fileName.length();
// Do some stuff to add tabs etc
// and write to file
}
}
}
}