thufir

bad output

Sep 14th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  1. package examples;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import java.io.PrintStream;
  10. import java.util.logging.Logger;
  11. import org.apache.commons.net.io.Util;
  12.  
  13. public final class IOUtil {
  14.  
  15.     private static final Logger log = Logger.getLogger(IOUtil.class.getName());
  16.  
  17.     private static void readFromConsole(final OutputStream remoteOutput) {
  18.         Thread read = new Thread() {
  19.  
  20.             @Override
  21.             public void run() {
  22.                 int ch;
  23.  
  24.                 try {
  25.                     while ((ch = System.in.read()) != -1) {
  26.                         remoteOutput.write(ch);
  27.                         remoteOutput.flush();
  28.                     }
  29.                 } catch (IOException ioe) {
  30.                     log.warning(ioe.toString());
  31.                 }
  32.             }
  33.         };
  34.         read.start();
  35.     }
  36.  
  37.     public static void writeToConsole(final InputStream remoteInput) {
  38.         Thread write = new Thread() {
  39.  
  40.             @Override
  41.             public void run() {
  42.                 try {
  43.                     Util.copyStream(remoteInput, System.out);
  44.                 } catch (IOException ioe) {
  45.                     log.warning(ioe.toString());
  46.                 }
  47.             }
  48.         };
  49.         write.start();
  50.     }
  51.  
  52.     public static void writeToFile(final InputStream remoteInput) {
  53.         Thread write = new Thread() {
  54.  
  55.             @Override
  56.             public void run() {
  57.                 PrintStream printStream = null;
  58.                 try {
  59.                     File file = new File("weather.log");
  60.                     FileOutputStream fos = new FileOutputStream(file, true);
  61.                     printStream = new PrintStream(fos);
  62.                     Util.copyStream(remoteInput, printStream);
  63.                 } catch (IOException ioe) {
  64.                     log.warning(ioe.toString());
  65.                 }
  66.             }
  67.         };
  68.         write.start();
  69.     }
  70.  
  71.     public static void readWrite(final InputStream remoteInput, final OutputStream remoteOutput, final InputStream localInput, final OutputStream localOutput) throws FileNotFoundException {
  72.         readFromConsole(remoteOutput);
  73.         writeToConsole(remoteInput);
  74.         writeToFile(remoteInput);
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment