这是源码:
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) {
}
}
}
请问问题出在那?该整么改,谢谢!
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) {
}
}
}
请问问题出在那?该整么改,谢谢!
一旦catch到异常,在catch后的{}中做一些处理后,程序就会中止。
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) {
}
}
}
你直接这样读,是不能读到你期望的数的
我是这样想的:
需要把myfile.txt文件内数据存放的结构定义好
比如一行放一个数,或者每个数用特定的符号分隔
然后再读取的时候按你自己的定义分析处理,再放入数组
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();
}
}
}