Sorting Algorithm With IOException after a precise number of passes, regardless of data input
I am trying to use a bubble sort ( i know its very inefficient ) to sort some data, but my code is behaving quite strangely, after exactly 926 passes of the outer while loop, an IOException is thrown, this is independent of the data inputed, I've checked and it dont seem to be related to the amount of memory available, the code and exception are below:
public static void sort(String f1, String f2) throws FileNotFoundException, IOException {
RandomAccessFile reader = new RandomAccessFile(f1,"rw");
RandomAccessFile writer = new RandomAccessFile(f1,"rw");
// start the bubble sort
int limit = (int) reader.length()/4;
while (limit>1) {
DataOutputStream writerstream = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(writer.getFD())));
DataInputStream readerstream = new DataInputStream(
new BufferedInputStream(new FileInputStream(reader.getFD())));
// the first value, a is the first value in the file
int a = readerstream.readInt();
int myfilepointer = 4;
// this flag is used to stop passing through when correct order is detected
boolean setpass = false;
// pass through the file
while (myfilepointer<limit*4) {
// the second value, b is the next value in the file
//System.out.println(myfilepointer);
int b = readerstream.readInt();
myfilepointer += 4;
// test if a and b are in the correct way around
// if wrong way around then b is written and the next a value is the same as before
if (a>b) { writerstream.writeInt(b); setpass = false; }
// if the correct way about then a is written and the next a value is the same as the previous b value
else { writerstream.writeInt(a); a = b; }
}
// this last value is the a value of exiting the while loop
writerstream.writeInt(a);
// write the new data to the file
writerstream.flush();
writer.seek(0);
reader.seek(0);
// if there was no swaps then the order is correct so exit loop
if (setpass == true) break;
limit -= 1;
}
}
and the exception thrown is below
Exception in thread "main" java.io.IOException: Read error
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read(Unknown 开发者_如何学运维Source)
at java.io.DataInputStream.readInt(Unknown Source)
at uk.ac.cam.hh360.fjava.tick0.ExternalSort.sort(ExternalSort.java:48)
at uk.ac.cam.hh360.fjava.tick0.ExternalSort.main(ExternalSort.java:119)
One potential problem is that you are not closing the streams opened in outer loop.
You're writing to the same file you're reading. So as soon a you write your first integer to the writer stream, the contents of the file is replaced by what you've written. Since you're using a BufferedOutputStream, it might in fact happen later than the first time you're writing.
You should read all the integers in memory, close the input stream, sort the integers in memory, and then write all the integers to the file.
I don't understand why you're using a random access file just to get a file descriptor and the open streams on the file. Either you want to access it randomly, or you want to access it using streams. But you can't do both.
精彩评论