Want more features on Pastebin? Sign Up, it's FREE!
Guest

ConsoleEncTest

By: a guest on Apr 3rd, 2012  |  syntax: Java  |  size: 1.26 KB  |  views: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.io.OutputStream;
  7. import java.util.Map;
  8.  
  9. public class ConsoleEncTest {
  10.         public static void main(String[] args) throws IOException, InterruptedException {
  11.                 String pyCode;
  12.                 pyCode = "import os;import sys;print '%s|%s' % (sys.getdefaultencoding(), sys.stdin.encoding);";
  13.                 ProcessBuilder pb = new ProcessBuilder("python", "-c", "\"" + pyCode + "\"");
  14.  
  15.                 Map<String, String> env = pb.environment();
  16.                 env.put("PYTHONPATH", ".");
  17.  
  18.                 Process p = pb.start();
  19.  
  20.                 OutputStream outStream = p.getOutputStream();
  21.                 InputStream inStream = p.getInputStream();
  22.                 InputStream errStream = p.getErrorStream();
  23.                
  24.                 BufferedReader inReader = new BufferedReader(new InputStreamReader(inStream));
  25.                 BufferedReader errReader = new BufferedReader(new InputStreamReader(errStream));
  26.                 outStream.close();
  27.                 p.waitFor();
  28.                
  29.                 System.out.println("STDOUT:");
  30.                 String line = inReader.readLine();
  31.                 while (line != null) {
  32.                         System.out.println(line);
  33.                         line = inReader.readLine();
  34.                 }
  35.                 System.out.println("STDERR:");
  36.                 line = errReader.readLine();
  37.                 while (line != null) {
  38.                         System.out.println(line);
  39.                         line = errReader.readLine();
  40.                 }
  41.         }
  42. }
clone this paste RAW Paste Data