簡體   English   中英

Java:將控制台輸出寫入文件,無法正常工作

[英]Java : Writing console output to file, Not working

我已經編寫了一個用於端口掃描的應用程序,我想將控制台輸出寫入文件,但是發生了一些小問題。 “ PrintStream”未將所有控制台輸出寫入該文件。
例如: try塊中的代碼顯示控制台中打開的端口,該代碼不會將任何內容寫入文件,但是catch塊中的死主機將被寫入。

我的代碼:

public class start {


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

        for (int i = 5935; i < 10000; i++){
        new test(i);
        }

        PrintStream printStream = new PrintStream(new FileOutputStream("E:\\ports.txt"));
        System.setOut(printStream);
        printStream.flush();


    }
}


class test implements Runnable{

    static String host = "localhost";
    int t;
    Thread y;

    public test(int t2){
        t = t2;
        y = new Thread(this);
        y.start();
    }

    public void run() {
        try {
            Socket socket = new Socket(host, t);
            System.out.println("Port is alive - " + t);
        } catch (IOException e){
            System.out.println("Port is dead... - " + t);
        }

    }
}

您有一些與手頭問題直接相關的問題。

  1. 在啟動線程之后設置輸出,因此輸出的位置幾乎是隨機的。
  2. 您無需等待線程完成,因此應用程序“隨機結束”。
  3. 您不刷新輸出。

更新的代碼:

class StartPortTester {
    public static void main(String[] args) throws IOException, InterruptedException {

        // Set up the stream BEFORE starting threads
        // This should be in a try-with-resources, or the close done in a finally block.
        PrintStream printStream = new PrintStream(new FileOutputStream("ports.txt"));
        System.setOut(printStream);

        // Start the threads!    
        List<PortTester> testers = new LinkedList<>();
        for (int i = 5935; i < 10000; i++){
            testers.add(new PortTester(i));
        }

        // Wait for the threads to end
        for(PortTester t : testers ) {
            t.y.join();
        }

        // Flush (write to disk) and close.
        printStream.flush();
        printStream.close();;
    }
}


class PortTester implements Runnable{

    static String host = "localhost";
    int t;
    Thread y;

    public PortTester(int t2){
        t = t2;
        y = new Thread(this);
        y.start();
    }

    public void run() {
        try {
            // You should close this either in the finally block or using a try-with-resource. 
            Socket socket = new Socket(host, t);
            System.out.println("Port is alive - " + t);
        } catch (IOException e){
            System.out.println("Port is dead... - " + t);
        }

    }
}

這不是完美的

  • 它創建了大量的線程,使用線程池會更好。
  • 它還會永遠等待線程完成,您可能希望它只等待x秒才放棄。
  • 異常將導致文件不被刷新和關閉。
  • 您將演示文稿與邏輯混淆。 我不會在PortTester編寫System.out,而是創建一個描述端口狀態的數據結構,然后將其輸出(將顯示與邏輯分開)。
  • 目前,輸出的順序是隨機的(基於線程完成的時間)。
  • 關閉插座
  • 使用執行器服務
  • 在寫入輸出之前設置輸出流
  • 等待所有作業准備就緒
  • 准備就緒時沖洗輸出
  • 關閉printStream

結果是:

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class start
{

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

    try (PrintStream printStream = new PrintStream(new FileOutputStream("E:\\ports.txt"))) {
        System.setOut(printStream);

        ExecutorService pool = Executors.newCachedThreadPool();
        for (int i = 5935; i < 10000; i++) {
            final int port = i;
            pool.execute(() -> {
                try (Socket socket = new Socket("localhost", port)) {
                    System.out.println("Port is alive - " + port);
                }
                catch (IOException e) {
                    System.out.println("Port is dead... - " + port);
                }
            });
        }
        pool.awaitTermination(100, TimeUnit.SECONDS);
        printStream.flush();
    }
  }
}

暫無
暫無

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

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