0

私は ApacheIOUtil サンプルを少し異なる方法でWeatherTelnet サンプルと連携するように調整しました。WeatherTelnet状態のコメントは次のとおりです。

単独で使用される TelnetClient クラスは、インタラクティブな使用ではなく、Telnet リソースへのアクセスの自動化を主な目的としています。

Apache を使用して出力を分割したいのですTeeOutputStreamが、APIは分岐しますがOutputStreamTelnetClient「出力」は の形式であるInputStreamため、もちろん読み取ることができます。便利なことに、ApachecopyStreamユーティリティ メソッドは InputStream を OutputStream にコピーします。

1.) をinまたはにコピーするInputStreamにはどうすればよいですか? 2.)をファイルに書き込む、またはティーにするにはどうすればよいですか?OutputStreamprintKindaWorksprintToFileOutputStream

秘訣は、ユーザーが気象サーバーと対話している間に、これらの操作をライブで行うことです。

頑張ってソースコードを見copyStreamて参考にしたのですが、どうもうまくいきません。ティーの Apache 実装の内部構造はまだ見ていません。

ユーティリティ クラス:

package apache;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.net.io.Util;

public final class IOUtil {

    private static final Logger log = Logger.getLogger(IOUtil.class.getName());

    private static void readFromConsole(final OutputStream outputStream) {
        Thread read = new Thread() {

            @Override
            public void run() {
                int ch;

                try {
                    while ((ch = System.in.read()) != -1) {
                        outputStream.write(ch);
                        outputStream.flush();
                    }
                } catch (IOException ioe) {
                    log.warning(ioe.toString());
                }
            }
        };
        read.start();
    }

    private static void writeToConsole(final InputStream inputStream) {
        Thread write = new Thread() {

            @Override
            public void run() {
                try {
                    Util.copyStream(inputStream, System.out);
                } catch (IOException ioe) {
                    log.warning(ioe.toString());
                }
            }
        };
        write.start();
    }

    private static void printKindaWorks(final InputStream inputStream) {
        Thread write = new Thread() {

            @Override
            public void run() {
                PrintStream printStream = null;
                try {
                    File file = new File("weather.log");
                    FileOutputStream fos = new FileOutputStream(file, true);
                    printStream = new PrintStream(fos);
                    Util.copyStream(inputStream, printStream);
                } catch (IOException ioe) {
                    log.warning(ioe.toString());
                }
            }
        };
        write.start();
    }

//                TeeOutputStream tee = new TeeOutputStream(inputStream, bis);   
    private static void writeToFile(final InputStream inputStream) throws FileNotFoundException, IOException {
        final String fname = "whether.log";
        File f = new File(fname);
        f.createNewFile();
        Thread fileWriter = new Thread() {

            @Override
            public void run() {
                char c = 0;
                int r = 0;
                try {
                    while ((r = inputStream.read()) != -1) {
                        c = (char) r;
                        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fname, true)));
                        out.print(c);
                        out.close();
                    }
                } catch (IOException ex) {
                    Logger.getLogger(IOUtil.class.getName()).log(Level.SEVERE, null, ex);
                }


            }
        };
        fileWriter.start();
    }

    public static void readWriteLog(final InputStream inputStream, final OutputStream outputStream) throws FileNotFoundException, IOException {
        readFromConsole(outputStream);
        writeToConsole(inputStream);
        writeToFile(inputStream);  //doesn't write much
  //      printKindaWorks(inputStream);       //blocks writeToConsole ?
    }
}

そしてドライバー:

package weather;

import apache.IOUtil;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.apache.commons.net.telnet.TelnetClient;

public class Weather {

    public Weather() {
    }

    public static void main(String[] args) throws UnknownHostException, IOException {
        int port = 3000;
        InetAddress host = InetAddress.getByName("rainmaker.wunderground.com");
        TelnetClient telnetClient = new TelnetClient();
        telnetClient.connect(host, port);
        IOUtil.readWriteLog(telnetClient.getInputStream(), telnetClient.getOutputStream());
    }
}

ASL の下のコードを検討してください。

私は「ログを記録」してInputStreamいますが、ログの問題に取り組んでいるわけではありません。ファイルへの書き込みは、説明のためだけのものです。InputStreamユーザーが天気予報サーバーとやり取りしている間に分割したいだけです。

4

2 に答える 2

1

System.out とログ ファイルをラップする TeeOutputStream をインスタンス化する必要があります。次に、telnet InputStream を TeeOutputStream にコピーするスレッドを作成します。(telnet InputStream を消費するスレッドは 1 つしか持てません)

于 2013-09-14T12:58:41.377 に答える
0

私が本当に望んでいるものではなく、むしろApache teeを使用したいのですが、これは(不器用に)仕事をします:

package apache;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

public final class IOUtil {

    private static final Logger log = Logger.getLogger(IOUtil.class.getName());

    private static void readFromConsole(final OutputStream outputStream) {
        Thread read = new Thread() {

            @Override
            public void run() {
                int ch;

                try {
                    while ((ch = System.in.read()) != -1) {
                        outputStream.write(ch);
                        outputStream.flush();
                    }
                } catch (IOException ioe) {
                    log.warning(ioe.toString());
                }
            }
        };
        read.start();
    }

//                TeeOutputStream tee = new TeeOutputStream(inputStream, bis);   
    private static void readInput(final InputStream inputStream) throws FileNotFoundException, IOException {
        Thread readInput = new Thread() {

            @Override
            public void run() {
                char c = 0;
                int r = 0;
                try {
                    while ((r = inputStream.read()) != -1) {
                        c = (char) r;
                        printToConsole(c);
                        logToFile(c);
                    }
                } catch (IOException ex) {
                    Logger.getLogger(IOUtil.class.getName()).log(Level.SEVERE, null, ex);
                }


            }

            private void logToFile(char c) throws IOException {
                String fname = "whetherOrNot.log";
                File f = new File(fname);
                f.createNewFile();
                PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fname, true)));
                out.print(c);
                out.flush();
                out.close();
            }

            private void printToConsole(char c) {
                System.out.print(c);
            }
        };
        readInput.start();
    }

    public static void readWriteLog(final InputStream inputStream, final OutputStream outputStream) throws FileNotFoundException, IOException {
        readFromConsole(outputStream);
        readInput(inputStream);
    }
}
于 2013-09-14T12:59:35.523 に答える