I have implemented a program to sort a file consisting of numbers. The program generates a different output file with all numbers sorted in it. I have implemented this program using BitSet
and want to understand BitSet
.
public class Application {
public static void main(String[] args) throws Exception {
String inputFile = "c:\\temp\\numbers.txt";
NumberFileSorter.sort(inputFile, 100);
}
}
class NumberFileSorter {
private NumberFileSorter() {
}
public static void sort(String inputFile, int maxValue) throws Exception {
final BitSet bitSet = createBitSet(inputFile, maxValue);
final String outputFile = deriveOutputFile(inputFile);
writeBitSet(bitSet, outputFile);
}
private static BitSet createBitSet(String inputFile, int maxValue)
throws Exception {
final FileInputStream stream = new FileInputStream(inputFile);
final InputStreamReader streamReader = new InputStreamReader(stream);
final BufferedReader buffReader = new BufferedReader(streamReader);
String line = null;
int totalBits = maxValue + 1;
BitSet bitSet = new BitSet(totalBits);
try {
while ((line = buffReader.readLine()) != null) {
int number = Integer.parseInt(line);
bitSet.set(number, true);
}
} finally {
buffReader.close();
streamReader.close();
stream.close();
}
return bitSet;
}
private static String deriveOutputFile(String inputFile) {
String outputFileName = Paths.get(inputFile).getParent().toString()
+ "/output.txt";
return outputFileName;
}
private static void writeBitSet(BitSet bitSet, String outputFile)
throws Exception {
final File file = new File(outputFile);
file.createNewFile();
final FileOutputStream outputStream = new FileOutputStream(file);
final OutputStreamWriter writer = new OutputStreamWriter(outputStream);
final BufferedWriter buffWriter = new BufferedWriter(writer);
try {
for (int bitIndex = 0; bitIndex < bitSet.length(); bitIndex++) {
if (bitSet.get(bitIndex)) {
buffWriter.write(Integer.toString(bitIndex));
buffWriter.newLine();
}
}
} finally {
buffWriter.close();
writer.close();
outputStream.close();
}
}
}
I request reviewers to provide feedback on the following:
- Is existing code readable? What are changes required to make it more readable?
- Is it advisable to unit test sorting logic by decoupling it from the file-system by introducing appropriate interfaces and using dependency injection?
- Can this program be made more reliable and robust?
- Can this code be structured differently for the better?
- Would you integration test it or unit test it? Could you explain the reason?