IntellijIdea:vim TOhtml的FileNotFoundException

时间:2016-07-03 08:13:41

标签: java vim

我试图用https://developer.rackspace.com/blog/displaying-prepared-code-with-syntax-highlighting-on-android/在html中显示一些代码,所以我在Java中编写了一个自定义格式化程序,其中包括以下程序:

String code2html( String s ) throws Exception {
    if ( s == null || s.length() == 0 ) return "";
    String t;
    PrintWriter w = new PrintWriter(Konst.FPATH+"tmp.txt","UTF-8");
    w.println(s);
    w.close();
    Runtime rt = Runtime.getRuntime();
    Process pr = rt.exec("vim -c \"let html_use_css=0\" -c \"TOhtml\" -c \"w\" "+Konst.FPATH+"tmp.txt.html"+" -c \"wq\" -c \"q\" "+Konst.FPATH+"tmp.txt");
    StringBuilder sb = new StringBuilder();

    BufferedReader b = new BufferedReader(new FileReader(Konst.FPATH+"tmp.txt.html"));
    while ( (t=b.readLine())!=null && !(t.length()>=5&&t.substring(0,5).equals("<body")) );
    do {sb.append(t+"\n");} while ( (t=b.readLine())!=null && !(t.length()>=6&&t.substring(0,6).equals("</body")) );
    sb.append(t);
    return sb.toString();
}

但是,我为tmp.txt.html文件获取了FileNotFoundException,尽管tmp.txt是创建好的。从命令行运行上面的vim命令会产生所需的结果。可以做些什么?

编辑:我添加了int retVal = pr.exitValue()并获得了Exception in thread "main" java.lang.IllegalThreadStateException: process hasn't exited

编辑:哈,我读过这篇精彩的文件:http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html?page=2,实现了处理这个问题的方法,我得到的是

ERROR>Vim: Warning: Output is not to a terminal
ERROR>Vim: Warning: Input is not from a terminal

1 个答案:

答案 0 :(得分:0)

Bram Moolenaar在http://vim.1045645.n5.nabble.com/Vim-Warning-Output-is-not-to-a-terminal-td4648615.html上回答了类似的问题 他说:“如果输入和输出都被重定向,那么你真的被困住了”。 我通过使用gvim而不是终端vim找到了一种解决方法,并将上面的代码重写为

String code2html( String s ) throws Exception {
        if ( s == null || s.length() == 0 ) return "";
        String t;
        PrintWriter w = new PrintWriter(Konst.FPATH+"tmp.txt","UTF-8");
        w.println(s);
        w.close();
        Runtime rt = Runtime.getRuntime();
        String T;
        try {
            Process pr = rt.exec(T = "gvim -c \"let html_use_css=0\" -c \"TOhtml\" -c \"wqx\" -c \"q\" -c \"q\" " + Konst.FPATH + "tmp.txt");
            FileOutputStream fos;

            StreamGobbler errorGobbler = new StreamGobbler(pr.getErrorStream(),"ERROR");
            StreamGobbler outputGobbler = new StreamGobbler(pr.getInputStream(),"OUTPUT",fos=new FileOutputStream(Konst.FPATH+"garbage.out"));

            errorGobbler.start();
            outputGobbler.start();

            int exitVal = pr.waitFor();
            System.out.println("ExitValue: "+exitVal);
            fos.flush();
            fos.close();

            StringBuilder sb = new StringBuilder();

            BufferedReader b = new BufferedReader(new FileReader(Konst.FPATH + "tmp.txt.html"));
            while ((t = b.readLine()) != null && !(t.length() >= 5 && t.substring(0, 5).equals("<body"))) ;
            do {
                sb.append(t + "\n");
            } while ((t = b.readLine()) != null && !(t.length() >= 6 && t.substring(0, 6).equals("</body")));
            sb.append(t);
            return sb.toString();
        }
        catch ( Throwable e ) {
            e.printStackTrace();
        }
        return "";
    }`