2014-06-20 2 views
-1

ProcessBuilder 및 .getInputStream()을 사용하여 Java에서 C 응용 프로그램을 실행하려고합니다. 나는이 응용 프로그램의 출력을 얻는다고 생각합니까? 그렇다면 BufferedReader에 래핑 된 FileReader를 사용해야한다고 생각합니까? 프로세스와 FileReader를 연결하는 방법을 잘 모르겠습니다.getInputStream + Java의 BufferedReader + FileReader

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(); 

UPDATE : 아래 : 나는 지금, 이것은 의미가 납니까 다음 주 잖아

나는 (코멘트에) 몇 가지 C를 변환하는거야? 명령에 따라 파일을 생성하고 특정 계산을 수행하는 두 가지 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++; 
} 

답변

0

변경이 줄이에

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

:

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