perl

来源:互联网 发布:西语记单词软件 编辑:程序博客网 时间:2024/06/08 12:03

代码亲自跑一下,很好玩

Java 调用 perl 

Java代码  收藏代码
  1. Runtime.getRuntime().exec("perl C:\\Perl\\execise.pl");  


Java 调用 perl 通过IO的方式来得到返回值 
Java代码  收藏代码
  1. package fiveGame;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.InputStreamReader;  
  7.   
  8. public class JavaCallPerl {  
  9.     public static void main(String[] args) throws IOException {  
  10.   
  11.         StringBuffer resultStringBuffer = new StringBuffer();  
  12.   
  13.         String lineToRead = "";  
  14.         int exitValue = 0;  
  15.   
  16.         try {  
  17.   
  18.             Process proc = Runtime.getRuntime().exec("perl C:\\Perl\\execise.pl");  
  19.             InputStream inputStream = proc.getInputStream();  
  20.             BufferedReader bufferedRreader = new BufferedReader(new InputStreamReader(inputStream));  
  21.   
  22.             // save first line  
  23.             if ((lineToRead = bufferedRreader.readLine()) != null) {  
  24.                 resultStringBuffer.append(lineToRead);  
  25.             }  
  26.   
  27.             // save next lines  
  28.             while ((lineToRead = bufferedRreader.readLine()) != null) {  
  29.                 resultStringBuffer.append("\r\n");  
  30.                 resultStringBuffer.append(lineToRead);  
  31.             }  
  32.   
  33.             // Always reading STDOUT first, then STDERR, exitValue last  
  34.             proc.waitFor(); // wait for reading STDOUT and STDERR over  
  35.             exitValue = proc.exitValue();  
  36.         } catch (Exception ex) {  
  37.             resultStringBuffer = new StringBuffer("");  
  38.             exitValue = 2;  
  39.         }  
  40.   
  41.         System.out.println("exit:" + exitValue);  
  42.   
  43.         System.out.println(resultStringBuffer.toString());  
  44.   
  45.     }  
  46.   
  47. }  



perl 
Java代码  收藏代码
  1. #!/usr/bin/perl -w  
  2. $pi = 3.141592654;  
  3. $circ = 2 * $pi * 12.5;  
  4. print "The circumference of a circle of radius 12.5 is $circ.\n"
1 0
原创粉丝点击