Java 如何将InputStream复制到需要OutputStream的Apache TeeOutputStream?
我对ApacheJava 如何将InputStream复制到需要OutputStream的Apache TeeOutputStream?,java,file-io,io,text-files,apache-commons,Java,File Io,Io,Text Files,Apache Commons,我对ApacheIOUtil进行了调整,使其与WeatherTelnet的工作方式略有不同。而WeatherTelnet的评论指出: 自身使用的TelnetClient类主要用于 自动访问telnet资源,而不是交互式使用 我想使用ApacheTeeOutputStream分割输出,但是分支OutputStream,而TelnetClient“输出”的形式是InputStream,因此当然可以读取。ApachecopyStream实用程序方法可以方便地将InputStream复制到OutputS
IOUtil
进行了调整,使其与WeatherTelnet
的工作方式略有不同。而WeatherTelnet
的评论指出:
自身使用的TelnetClient类主要用于
自动访问telnet资源,而不是交互式使用
我想使用ApacheTeeOutputStream
分割输出,但是分支OutputStream
,而TelnetClient
“输出”的形式是InputStream
,因此当然可以读取。ApachecopyStream
实用程序方法可以方便地将InputStream复制到OutputStream
1.)如何将InputStream
复制到printKindaWorks
或printToFile
中的OutputStream
?
2.)如何将OutputStream
写入一个文件,或者如何将其写入
诀窍是在用户与天气服务器交互时,实时执行这些操作
我尽力查看了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.< /P>下的代码
当我“记录”InputStream时,我没有处理记录问题,写入文件只是为了说明。我只想在用户与天气服务器交互时分割
输入流。您需要实例化一个TeeOutputStream,它包装System.out和日志文件。然后创建一个线程,将telnet InputStream复制到TeeOutputStream。(您只能有一个线程使用telnet InputStream)这不是我想要的,我更愿意使用ApacheTee,但这可以完成任务(笨拙):
好的,听起来很有趣!我认为System.out是一个输出流,也许是一个打印器?对于分支?@Thufir-只需使用FileOutputStream而不是FileWriter。对不起,我不知道如何将InputStream复制到TeeOutputstream。您认为只有一个线程可以使用telnet InputStream这一点非常重要。@Thufir-您已经有了将InputStream复制到(Tee)OutputStream的代码。我不理解这种混乱。
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);
}
}