Java写入文件排序数组

时间:2017-04-26 00:41:23

标签: java arrays sorting

我试图从记事本文件(名为array.txt)中读取25个整数,然后对它们进行排序并将它们输出到另一个文件中。当我运行它时,输出是按照我在记事本上的顺序排列的25个整数(未排序)以及说明如下的异常:

我在我的代码中评论过第32行和第16行是发生异常的地方。

以下是我得到的例外情况。

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 25 at writetofile.WriteToFile.processFile(WriteToFile.java:32)
at writetofile.WriteToFile.main(WriteToFile.java:16)

这是我的代码:

package writetofile;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class WriteToFile {

public static void main(String[] args) throws IOException {

int[] array=new int [25];
array = processFile ("src/array.txt");   //LINE 16
sortArray(array);
writeToFile ("src/array.txt",array);

}


public static int[] processFile (String filename) throws IOException, FileNotFoundException{

int[] value;
try (BufferedReader inputReader = new BufferedReader (new InputStreamReader(new FileInputStream(filename)))) {
    String line;
    int i = 0;
    value = new int [25];
    while ( (line = inputReader.readLine()) != null){
        int num = Integer.parseInt (line);      // Convert string to 
integer.
        value[i] = num;     //LINE 32 
        i++;
        System.out.println (num); // Test 
    }
}
return value;
// Read the 25 numbers and return it
}





public static void sortArray (int[] num){     //bubble sort method
boolean order = true;
int temp;

while (order){
    order = false; 
    for (int i = 0; i <num.length-1; i++){
        if (num[i]> num[i+1]){
            temp = num[i]; //set index to temp
            num[i] = num [i+1]; // swap
            num[i+1]= temp; //set index to the higher number before it
            order = true;
        }
    }
}          
} 




public static void writeToFile (String filename, int[] array) throws 
IOException{

BufferedWriter outputWriter= new BufferedWriter(new FileWriter(filename));
double number;
for (int counter=0; counter<25; counter ++){
    number= array[counter];
    String numberString= Double.toString(number);
    outputWriter.write(numberString);
    outputWriter.newLine();

}

    outputWriter.flush();
    outputWriter.close();
}

}

2 个答案:

答案 0 :(得分:2)

您正在尝试阅读超过25行 - 也许检查文件?

无论如何,您可以通过

来阻止这种情况
 while (i < 25 && (line = inputReader.readLine()) != null){
      // check for blank lines
     if (line.length () < 1) continue;
     int num = Integer.parseInt (line);
     ...
 }

答案 1 :(得分:0)

使用文件array.txt

1
2
32
34
0
34
43
54
43
532
5
43
52
2
43
43
45
5
43
2
54
3
54
324

使用文件WriteToFile.java

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class WriteToFile {

public static void main(String[] args) throws IOException {

  int[] array= new int [25];
  array = processFile ("array.txt");   //LINE 16
  sortArray(array);
  writeToFile ("newFile.txt",array);

  }


  public static int[] processFile (String filename) throws IOException, FileNotFoundException{
  System.out.println("Processing File");
  int[] value = new int [25];;
  try {
    BufferedReader inputReader = new BufferedReader (new InputStreamReader(new FileInputStream(filename)));
    String line;
    int i = 0;
    while ( (line = inputReader.readLine()) != null){
      int num = Integer.parseInt (line);      // Convert string to integer.
      value[i] = num;
      i++;
      System.out.println (num); // Test
    }
  } catch(Exception e) {
    System.out.println(e);
  }
  return value;
  // Read the 25 numbers and return it
  }





  public static void sortArray (int[] num){     //bubble sort method
    boolean order = true;
    int temp;

    while (order){
      order = false;
      for (int i = 0; i <num.length-1; i++){
          if (num[i]> num[i+1]){
          temp = num[i]; //set index to temp
          num[i] = num [i+1]; // swap
          num[i+1]= temp; //set index to the higher number before it
          order = true;
        }
      }
    }
  }




  public static void writeToFile (String filename, int[] array) throws IOException {
    System.out.println("\nWriting to File");
    BufferedWriter outputWriter= new BufferedWriter(new FileWriter(filename));
    double number;
    for (int counter=0; counter<25; counter ++){
      number= array[counter];
      System.out.println(number);
      String numberString= Double.toString(number);
      outputWriter.write(numberString);
      outputWriter.newLine();
    }

    outputWriter.flush();
    outputWriter.close();
  }

}