我有一個簡單的問題。我的程式要求用戶輸入名稱、范圍和長度以生成亂數。結果將從控制臺列印到檔案中。我想知道是否可以在完成后列印檔案已列印到控制臺。
目前這是我列印到檔案的設定:
Scanner s = new Scanner(System.in);
System.out.println("-----------------------------------------------");
System.out.println("Choose a name to your file: ");
String fn = s.nextLine();
System.out.println("-----------------------------------------------");
System.out.println("Choose your range: ");
String rn = s.nextLine();
System.out.println("-----------------------------------------------");
System.out.println("Choose your length of array: ");
String ln = s.nextLine();
System.out.println("-----------------------------------------------");
int rangeToInt = Integer.parseInt(rn);
int lengthToInt = Integer.parseInt(ln);
File file = new File(fn ".txt");
PrintStream stream = new PrintStream(file);
//System.out.println("File Has been Printed");
System.setOut(stream);
int[] result = getRandomNumbersWithNoDuplicates(rangeToInt, lengthToInt);
for(int i = 0; i < result.length; i ) {
Arrays.sort(result);
System.out.println(result[i] " ");
}
System.out.println("Current Time in Millieseconds = " System.currentTimeMillis());
System.out.println("\nNumber of element in the array are: " result.length "\n");
}// end of main
```
uj5u.com熱心網友回復:
不要打電話System.setOut
。當您這樣做時,您將無法再列印到控制臺。而不是System.out.println
寫入檔案,只是......stream.println
寫入檔案。然后您可以使用System.out
列印到控制臺。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/465546.html