我已经对 ApacheIOUtil
示例进行了调整,以稍微不同地使用该WeatherTelnet
示例。虽然评论WeatherTelnet
指出:
其本身使用的 TelnetClient 类主要用于自动访问 telnet 资源,而不是交互式使用。
我想使用 Apache 拆分输出TeeOutputStream
,但是API分支OutputStream
,而TelnetClient
“输出”是 的形式InputStream
,因此当然可以读取它。方便的是,ApachecopyStream
实用程序方法会将 InputStream 复制到 OutputStream。
1.) 如何将 复制InputStream
到OutputStream
inprintKindaWorks
或printToFile
?2.) 我如何将其写入OutputStream
文件,或 tee ?
诀窍是在用户与天气服务器交互时实时进行这些操作。
我尽力查看源代码copyStream
并将其用作参考,但它根本不起作用。我还没有查看 tee 的 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
用户与天气服务器交互的时间。