對於我的任務,我必須從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