【问题标题】:getInputStream + BufferedReader + FileReader in JavaJava 中的 getInputStream + BufferedReader + FileReader
【发布时间】:2014-06-20 14:34:47
【问题描述】:

我正在尝试使用 ProcessBuilder 和 .getInputStream() 在 Java 中运行 C 应用程序。我认为哪个会得到这个应用程序的输出?那么我想我需要使用一个包裹在 BufferedReader 中的 FileReader 吗?我不确定如何连接 Process 和 FileReader?

我正在翻译一些 C(在 cmets 中):

void run(){ 
        //FILE *POINTS;
        //I think the above points to the file? so I'm using a InputStream object. 

        private InputStream POINTS;

            //generate octree for this case
            //=============================
            //sprintf(befehl,"%soconv -f %s %s %s \"%s\" > %s\n","",material_file, BlindRadianceFiles_Combined, geometry_file, sun_rad, octree);
            //note I've replaced "befehl" with "cmd".

            String.format(cmd, "%soconv -f %s %s %s \"%s\" > %s\n","",material_file, BlindRadianceFiles_Combined, geometry_file, sun_rad, octree);

            //POINTS = popen(befehl,"r");
            //Info on POPEN in C. For instance, if you wanted to read the output of your program, you'd use popen("program", "r"). On the other hand, if you want to write to its input, you would use popen("program", "w").

            Process process = new ProcessBuilder(cmd).start();
            POINTS = process.getInputStream();

            //while( fscanf( POINTS, "%s", buf ) != EOF )
            //  printf("%s \n",buf);
            //pclose(POINTS);

            BufferedReader in = new BufferedReader(new FileReader("<filename>"));

            while ((in = fileReader.readLine()) != null){
                System.out.printf("%s \n", buf);
            }
            POINTS.close();

更新:如下:

我现在有了以下内容,这似乎有意义吗?有 2 个不同的 C 程序基于命令生成文件并执行一些特定的计算。

void run_oconv_and_rtrace() throws IOException{
    String line;
    String cmd = null;

    //generate octree for this case
    //=============================
    //TODO review how the command is generated and what information is required for this analysis
    cmd = String.format(cmd,"%soconv -f %s %s %s \"%s\" > %s\n","", material_file, BlindRadianceFiles_Combined, geometry_file, sun_rad, octree);

    /* Use the processbuilder to run an external process (ie. Radiance C program).
     * process.getOutputStream(): return the standard input of the external program. (ie. Java writes to Process).
     * process.getInputStream(): return the standard output of the external program. (ie. Jave reads from Process).
     */

    ProcessBuilder builder = new ProcessBuilder(cmd);
    builder.redirectErrorStream(true);                  //adds error stream to inputstream
    Process process = builder.start();

    OutputStream POINTS_FROM_JAVA_TO_C = process.getOutputStream();
    InputStream POINTS_FROM_C_TO_JAVA = process.getInputStream();

    //BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(POINTS_FROM_JAVA_TO_C));
    BufferedReader reader = new BufferedReader(new InputStreamReader(POINTS_FROM_C_TO_JAVA));

    //Read output of Radiance:
    while ((line = reader.readLine()) != null){
        System.out.printf("%s \n", buf);
    }

    //////////////////////////


    // run rtrace and calcualte the shading status for this case
    //==========================================================
    //TODO review command
    cmd = null;
    cmd = String.format(cmd, "rtrace_dc -ab 0 -h -lr 6 -dt 0 \"%s\" < %s > %s\n",octree,long_sensor_file[BlindGroupIndex],dir_tmp_filename[NumberOfBlindGroupCombinations]);

    builder = new ProcessBuilder(cmd);
    builder.redirectErrorStream(true);                  //adds error stream to inputstream
    process = builder.start();

    POINTS_FROM_C_TO_JAVA = process.getInputStream();
    reader = new BufferedReader(new InputStreamReader(POINTS_FROM_C_TO_JAVA));

    //Read output of Radiance:
    while ((line = reader.readLine()) != null){
        System.out.printf("%s \n", buf);
    }

    //delete files
    //HERE WE NEED TO DELETE THE OCTREE FILES CREATED!

    BlindGroupNumberForThisCombination[NumberOfBlindGroupCombinations]=BlindGroupIndex;
    NumberOfBlindGroupCombinations++;
}

【问题讨论】:

    标签: java c bufferedreader processbuilder


    【解决方案1】:

    改变这一行

    BufferedReader in = new BufferedReader(new FileReader("<filename>"));
    

    到这里:

    BufferedReader in = new BufferedReader(new InputStreamReader(POINTS));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-27
      • 2014-05-09
      • 1970-01-01
      相关资源
      最近更新 更多