1. import java.io.*;
    2. import java.util.Arrays;
    3. public class Sort {
    4. public void TestSort() throws IOException {
    5. BufferedReader br = new BufferedReader(new FileReader("F:\\Java\\IO Study 03\\test.txt"));
    6. int c = 0;
    7. String len;
    8. String[] str = new String[1];
    9. while ((len = br.readLine()) != null) {
    10. str[c] = len;
    11. str = Arrays.copyOf(str, str.length + 1);
    12. c++;
    13. }
    14. if (c != 0) {
    15. int[] ic = new int[c];
    16. c--;
    17. for (int m = 0; m <= c; m++) {
    18. ic[m] = Integer.parseInt(str[m]);
    19. }
    20. for (int i = 1; i < ic.length; i++) {
    21. int x = ic[i];
    22. int j = i - 1;
    23. while (j >= 0 && ic[j] < x) {
    24. ic[j + 1] = ic[j];
    25. j--;
    26. }
    27. ic[j + 1] = x;
    28. }
    29. for (int m = 0; m < ic.length; m++) {
    30. str[m] = String.valueOf(ic[m]);
    31. }
    32. br.close();
    33. BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\Java\\IO Study 03\\test.txt"));
    34. for (int m = 0; m < ic.length; m++) {
    35. bw.write(str[m]+"\n");
    36. bw.flush();
    37. }
    38. bw.close();
    39. }else{
    40. System.out.println(" ");
    41. System.exit(-1);
    42. }
    43. }
    44. }

    运行前文件中的内容CIA8F2]D0XT(RQI8X9SFWHT.png运行后文件中的内容UN0J{XUWG~EMJR3`(7~DUVL.png