对于我的任务,我必须从25个数字的文件中读取数据,然后按顺序对其进行排序,然后将其写入另一个文件。我想不出在我的代码中传递数组的方式(以数组的字符串形式)按顺序写入文件,并将数字写入不同的文件。
这可能是一个简单的问题,但我只是有点麻烦试图通过一切。 预先感谢您。读取文件,对其进行排序,写入另一个文件
public static void main(String[] args) throws IOException{
int[] number;
number = processFile ("Destination not specified");
swapIndex(number);
writeToFile ("Destination not specified");
}
public static int[] processFile (String filename) throws IOException, FileNotFoundException{
BufferedReader inputReader = new BufferedReader (new InputStreamReader(new FileInputStream(filename)));
String line;
int i = 0;
int[] value = new int [25];
while ((line = inputReader.readLine()) != null){
int num = Integer.parseInt (line); // Convert string to integer.
value[i] = num;
i++;
System.out.println (num); // Test
}
inputReader.close();
return value;
// Read the 25 numbers and return it
}
public static void swapIndex (int[] num){ // BUBBLE sort
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;
}
}
}
} // Method swapIndex
public static void writeToFile (String filename) throws IOException {
BufferedWriter outputWriter = new BufferedWriter(new FileWriter(filename));
outputWriter.write (String.valueOf()); // Need to take the string value of the array
outputWriter.flush();
outputWriter.newLine();
}
你在哪里卡住了?它是否给出了任何异常/错误? – gurvinder372 2013-04-28 03:19:24
我只是想找出一个办法来通过一切。特别是要传递writeToFile方法中字符串值的内容。 – Bao 2013-04-28 04:08:31