这是源码:
import java.io.*;
public class ArraySort {
public static void main(String args[]) {
DataInputStream in = null;
DataOutputStream out = null;
int count = 0, a[]= new int[200];
try {
in = new DataInputStream(new FileInputStream("myfile.txt"));
System.out.print("read from myfile.txt:");
while (true) {
try {
int a1 = in.readInt();
a[count] = a1;
count++;
} catch (Exception e) {
break;
}
}
int temp, n = count;
for (int i = 0; i < n; i++)
for (int j = 0; j < n - 1 - i; j++)
if (a[j] < a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
in.close();
} catch (FileNotFoundException e) {
System.out.print("File not found");
} catch (IOException e) {
}
try {
out = new DataOutputStream(new FileOutputStream("myfile1.txt"));
while (true) {
try {
out.writeInt(a[count]);
count--;
} catch (Exception e) {
break;
}
}
out.close();
} catch (IOException e) {
}
}
}
请问问题出在那?该整么改,谢谢!

解决方案 »

  1.   

    先提个建议,发贴时有代码,请用以下格式: [code=Java] 这里插入你的代码[/code]。
      

  2.   

    你试图catch到异常后用break结束那个while(true)循环,然后再让程序继续往入执行,这是做不到的。
    一旦catch到异常,在catch后的{}中做一些处理后,程序就会中止。
      

  3.   

    这里你说错了,catch后会继续的执行下去
      

  4.   

    楼主的源码:
    import java.io.*;public class ArraySort {
    public static void main(String args[]) {
    DataInputStream in = null;
    DataOutputStream out = null;
    int count = 0, a[] = new int[200];
    try {
    in = new DataInputStream(new FileInputStream("myfile.txt"));
    System.out.print("read from myfile.txt:");
    while (true) {
    try {
    int a1 = in.readInt();
    a[count] = a1;
    count++;
    } catch (Exception e) {
    break;
    }
    }
    int temp, n = count;
    for (int i = 0; i < n; i++)
    for (int j = 0; j < n - 1 - i; j++)
    if (a[j] < a[j + 1]) {
    temp = a[j];
    a[j] = a[j + 1];
    a[j + 1] = temp;
    }
    in.close();
    } catch (FileNotFoundException e) {
    System.out.print("File not found");
    } catch (IOException e) {
    }
    try {
    out = new DataOutputStream(new FileOutputStream("myfile1.txt"));
    while (true) {
    try {
    out.writeInt(a[count]);
    count--;
    } catch (Exception e) {
    break;
    }
    }
    out.close();
    } catch (IOException e) {
    }
    }
    }
      

  5.   

    readInt()读取了四个字节
    你直接这样读,是不能读到你期望的数的
    我是这样想的:
    需要把myfile.txt文件内数据存放的结构定义好
    比如一行放一个数,或者每个数用特定的符号分隔
    然后再读取的时候按你自己的定义分析处理,再放入数组
      

  6.   

    import java.io.*; 
    import java.util.*;public class ArraySort1 {
    public static void main(String args[]) { 
     // 用这个输入可以一个数一数的读入,
     //可以用BufferdReader(new InputStreamReader(new FileInputStream("myfile.txt"))),
     //这时你要用readline()读取一行给一个字符串,再用split()方法把整个串分成字符数组,再把数组元素一个一个的转为int。 
     //你原题中的方法不可以。DataInputStream类只适合于用DataOutputStream产生的文件(也就是二进制文件)。
    Scanner in = null;  
    //PrintStream 可以以字符形式输出,还能以你想要的格式输出
    PrintStream out = null; 
    int count = 0, a[]= new int[200];  //也可以考虑用一个List,它的容量是可以变化的,程序更有灵活性。 
    try { 
    in = new Scanner(new File("myfile.txt"));  //文本文件中的数据可以用空格隔开,也可以用回车,还可以混用。
    System.out.print("read from myfile.txt:"); 
    while (in.hasNextInt()) {      //只要还有下一个整数。
     a[count] = in.nextInt();   
     System.out.println(a[count]);
     count++;

    int temp, n = count; 
    for (int i = 0; i < n; i++) 
    for (int j = 0; j < n - 1 - i; j++) {
    if (a[j] < a[j + 1]) { 
    temp = a[j];
    a[j] = a[j + 1]; 
    a[j + 1] = temp; 
    }
    }
    in.close(); 
    out = new PrintStream(new FileOutputStream("myfile1.txt"));  //输出到文本文件,数据之间用空格隔开
    for(int i=0;i<count;i++) {
    out.print(" "+a[i]); 
    }
    out.close(); 
    } catch (IOException e) { 
    e.printStackTrace();
    }
    }
      

  7.   

    我一直以为发生异常后程序不可能再继续执行下去了,楼主原来的程序也证明了这一点。try语句还有一个finally子句,java为什么要设计这个子句呢?不是为在中断程序之前做点什么吗?如果异常之后程序还能继续,这个子句就一点用处也没有了。