thufir

write to file should be similar to write to console

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