如何从Clojure程序调用Java程序?

时间:2016-04-21 05:18:49

标签: java clojure functional-programming

package javaCodes;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class interm3parse {
public static boolean checkGroup(String str)throws IOException
{
    FileInputStream fis = new FileInputStream(path);
    BufferedReader br = new BufferedReader(new InputStreamReader(fis));
    String line = null;
    while((line=br.readLine())!=null)
    {
        if(line.contains(str))
        {
            return true;
        }
    }
    br.close();
    return false;

}
public static boolean checkPresence(String str) throws IOException
{
    FileInputStream fis = new FileInputStream(filepath);
    BufferedReader br = new BufferedReader(new InputStreamReader(fis));
    String line = null;
    while((line=br.readLine())!=null)
    {
        if(line.startsWith(str))
        {
            return true;
        }
    }
    br.close();
    return false;

}

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    PrintWriter pw = new PrintWriter(new       FileWriter("filepath"));
    FileInputStream fis = new FileInputStream("filepath");
    BufferedReader br = new BufferedReader(new InputStreamReader(fis));
    String line = null;
    StringBuilder sb = new StringBuilder();
    String s=new String();
    while ((line = br.readLine()) != null)
    {
        s="";
        if(!line.startsWith("defmeasure")||(line==null))
         {
         String[] splitArray = line.split("\\s");
         s=s+splitArray[0];
         //System.out.println(splitArray[0]);
         for(int i=1;i<splitArray.length;i++)
          {
            if(splitArray[i].contains("/"))
            {
                String[] alias = splitArray[i].split("/");
                if(checkPresence(alias[1])||checkGroup(alias[1]))
                {
                    s=s+" "+alias[1];
                }
            }
            else
            {
                s=s+" "+splitArray[i];
            }
          }
         pw.write(s);
         pw.println();
         System.out.println(s);

         }
        else
        {
            //fw.write(line);
            //fw.write("\n");
            System.out.println(line);
            pw.write(line);
            pw.println();   
        }
        }
    pw.flush();
    pw.close();

}//main

}//end of class

我有一个用户定义的Java程序(interm3parse.java);我想从clojure程序调用该程序(函数)....两个代码都可以单独工作..Java代码在javaCodes包中... ..................... clojure代码在anothernamespace ........................ .........

Clojure code

(ns javaCodes.generalizedmeasuretocomp )

(defn output-Measure-Criterias-Final []
;;;Java program to replace all group components and remove aliases 

)

1 个答案:

答案 0 :(得分:0)

你可以call Java code from Clojure(假设Java代码类在Clojure应用程序的类路径上):

当您的Java类定义静态方法时,您可以像这样调用它们:

(ns javaCodes.generalizedmeasuretocomp
  (:import javaCodes.interm3parse))

(defn output-Measure-Criterias-Final []
  (javaCodes.interm3parse/checkGroup "some string"))

顺便提一下,我注意到你的Java代码可能无法正常工作(但我想这只是为了说明目的)。它使用一些未定义的名称(例如filepath)并且可能泄漏资源(当它返回true时它不会关闭输入流)。

我认为您应该将资源包装到try-with-resources语句中,或者使用with-open在Clojure中编写该代码:

(defn file-line-starts-with-str? [filename s]
  (with-open [reader (clojure.java.io/reader filename)]
    (->> reader
      (line-seq)
      (some #(clojure.string/starts-with? % s)))))