无论数据输入如何,在精确的传递次数后使用 IOException 进行排序算法
我正在尝试使用冒泡排序(我知道它的效率非常低)来对一些数据进行排序,但是我的代码表现得很奇怪,在外部 while 循环经过 926 次之后,抛出了 IOException,这与输入的数据无关,我检查过,它似乎与可用内存量无关,代码和异常如下:
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;
}
}
抛出的异常如下
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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个潜在的问题是您没有关闭在外循环中打开的流。
One potential problem is that you are not closing the streams opened in outer loop.
您正在写入正在读取的同一个文件。因此,一旦您将第一个整数写入写入器流,文件的内容就会被您写入的内容替换。由于您使用的是 BufferedOutputStream,因此实际上它可能会晚于您第一次写入时发生。
您应该读取内存中的所有整数,关闭输入流,对内存中的整数进行排序,然后将所有整数写入文件。
我不明白为什么您使用随机访问文件只是为了获取文件描述符和文件上的打开流。您要么想随机访问它,要么想使用流访问它。但你不能两者兼得。
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.