簡體   English   中英

在 JAVA 中打印到控制台

[英]Print to console in JAVA

我有一個簡單的問題。 我的程序要求用戶鍵入名稱、范圍和長度以生成隨機數。 結果將從控制台打印到文件中。 我想知道是否可以在控制台打印文件完成后打印出來。

目前這是我打印到文件的設置:

        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
     ```

不要調用System.setOut 當你這樣做時,你不能再打印到控制台。 而不是System.out.println寫入文件,只是... stream.println寫入文件。 然后你可以使用System.out打印到控制台。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM