Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package examples;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.PrintStream;
- 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 remoteOutput) {
- Thread read = new Thread() {
- @Override
- public void run() {
- int ch;
- try {
- while ((ch = System.in.read()) != -1) {
- remoteOutput.write(ch);
- remoteOutput.flush();
- }
- } catch (IOException ioe) {
- log.warning(ioe.toString());
- }
- }
- };
- read.start();
- }
- public static void writeToConsole(final InputStream remoteInput) {
- Thread write = new Thread() {
- @Override
- public void run() {
- try {
- Util.copyStream(remoteInput, System.out);
- } catch (IOException ioe) {
- log.warning(ioe.toString());
- }
- }
- };
- write.start();
- }
- public static void writeToFile(final InputStream remoteInput) {
- 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(remoteInput, printStream);
- } catch (IOException ioe) {
- log.warning(ioe.toString());
- }
- }
- };
- write.start();
- }
- public static void readWrite(final InputStream remoteInput, final OutputStream remoteOutput, final InputStream localInput, final OutputStream localOutput) throws FileNotFoundException {
- readFromConsole(remoteOutput);
- writeToConsole(remoteInput);
- writeToFile(remoteInput);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment