2010-08-09 5 views
0

에서 자바 실행 파일을 호출 나는 다음과 같은 코드를 컴파일에서 자바 클래스 파일을 호출하고 싶습니다 그러나가 bash는

import java.io.*; 

public class hex_to_dec { 
    private BufferedReader bufferedReader; 
    private BufferedWriter bufferedWriter; 

    public hex_to_dec (String stringPath, String stringPath_dec) 
    { 
     try 
     { 
      bufferedReader = new BufferedReader(new FileReader(stringPath)); 
      bufferedWriter = new BufferedWriter(new FileWriter(stringPath_dec, false)); 
     } catch (Exception e) { 
      System.out.println("Error in opening file." + e); 
     } 
    } 
    public void Parse() 
    { 
     try { 
      String tempLine; 
      int temp;   
      while((tempLine = bufferedReader.readLine()) != null) { 
       String[] tempBytes = tempLine.split(" "); 
       temp = Integer.valueOf(tempBytes[0], 16); 
       tempBytes[0] = String.valueOf((temp)); 
       temp = Integer.valueOf(tempBytes[2], 16); 
       tempBytes[2] = String.valueOf(((byte) temp)); 
       temp = Integer.valueOf(tempBytes[3], 16); 
       tempBytes[3] = String.valueOf((temp)); 
       temp = Integer.valueOf(tempBytes[5], 16); 
       tempBytes[5] = String.valueOf(((byte) temp)); 
       temp = Integer.valueOf(tempBytes[6], 16); 
       tempBytes[6] = String.valueOf((temp)); 
       temp = Integer.valueOf(tempBytes[8], 16); 
       tempBytes[8] = String.valueOf(((byte) temp)); 
       temp = Integer.valueOf(tempBytes[9], 16); 
       tempBytes[9] = String.valueOf((temp)); 
       temp = Integer.valueOf(tempBytes[11], 16); 
       tempBytes[11] = String.valueOf(((byte) temp)); 
       temp = Integer.valueOf(tempBytes[12], 16); 
       tempBytes[12] = String.valueOf((temp)); 
       temp = Integer.valueOf(tempBytes[14], 16); 
       tempBytes[14] = String.valueOf(((byte) temp)); 
       temp = Integer.valueOf(tempBytes[15], 16); 
       tempBytes[15] = String.valueOf((temp)); 
       temp = Integer.valueOf(tempBytes[17], 16); 
       tempBytes[17] = String.valueOf(((byte) temp)); 
       temp = Integer.valueOf(tempBytes[18], 16); 
       tempBytes[18] = String.valueOf((temp)); 
       temp = Integer.valueOf(tempBytes[20], 16); 
       tempBytes[20] = String.valueOf(((byte) temp)); 
       temp = Integer.valueOf(tempBytes[21], 16); 
       tempBytes[21] = String.valueOf((temp)); 
       temp = Integer.valueOf(tempBytes[23], 16); 
       tempBytes[23] = String.valueOf(((byte) temp)); 

          for (int i = 0; i < tempBytes.length; i++) 
       { 
        bufferedWriter.append(tempBytes[i] + " "); 
       } 
       bufferedWriter.append("\n");     
      } 
      bufferedWriter.flush(); 
     } catch (Exception e) { 
      System.err.println("Error:" + e); 
     } 
    } 
    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     hex_to_dec data = new hex_to_dec(
       "C:\\Documents and Settings\\Admin\\My Documents\\MATLAB\\tests\\rssi_2\\trimmed\\s5_node12", 
       "C:\\Documents and Settings\\Admin\\My Documents\\MATLAB\\tests\\rssi_2\\trimmed_dec\\s5_node12"); 
     data.Parse(); 
    } 
} 

을, 그것은 인수가 필요합니다, 나는 호출에 인수를 전달하는 방법을 모른다 이 명령은 bash에서 정리합니다. 또한, 선택한 디렉터리의 하위 디렉터리 아래에있는 모든 텍스트 파일을 통해이 함수를 반복적으로 호출 할 수있는 디렉터리를 파싱 할 수 있기를 바랍니다. 이것을 달성하는 가장 쉬운 방법은 무엇입니까?

미리 감사드립니다. 희망이 너무 지나치게 요구하지 않습니다.

+0

많은 파일에서이 파일을 호출하려는 경우 finally 블록 내부의 스트림을 닫으려고합니다. –

답변

2

여기에 몇 가지 단계가 있다고 생각합니다. 먼저 args를 사용하도록 메인을 변경하는 것입니다.

: 당신은 예를 들어, 소스 파일이 지정되어 있는지 확인 args.length를 확인해야합니다 (경고 : 검증되지 않은 자바를 C 프로그래머에서)

public static void main(String[] args) 
{ 
    if (args.length == 1) 
    { 
    hex_to_dec data = new hex_to_dec(args[0], args[0] + ".dec"); 
    data.Parse(); 
    } 
} 

를 클래스가 인수를 승인하면, 당신이 원하는 것 그것을 컴파일하십시오.

javac hex_to_dec.java 

일단 컴파일되면 스크립트를 사용하여 재귀 적으로 디렉토리를 처리 할 수 ​​있습니다.

#!/bin/sh 
find . | xargs -L 1 java hex_to_dec 

목표가 16 진수의 파일을 10 진수로 변환하는 것이라면 java 및 bash를 사용하는 것이 과잉이라고 생각하십시오. 다음과 같은 단일 쉘 스크립트를 사용하여이를 수행 할 수 있습니다.

#!/bin/sh 
find . -type f | while read filename 
do 

    # skip the file if it is already decoded 
    if [ "${filename%.dec}" != "${filename}" -o -z "${filename}" ] 
    then 
    continue 
    fi 

    (
    # read the file, line by line 
    cat "${filename}" | while read line 
    do 
     line=$(
     echo "${line}"     | 
     sed -e "s/[[:space:]]\{1,\}/;/g" | # split the line by spaces 
     tr '[:lower:]' '[:upper:]')  # convert lower to uppercase 

     echo "ibase=16; ${line}"   | # format the line for bc 
     bc        | # convert hex to dec 
     tr "\n" " "      # rejoin the output to a line 

     echo ""        # add the new line 
    done 
) > "${filename}.dec" 
done 
+0

'args'의 첫번째 요소는'args [0]'이 아니라'args [1]'(C와 유사)입니다. – mob

+0

고마워, 나는 이것을 반영하기 위해 대답을 업데이트했다. 그러나 C에서 argv [0]은 제안한 첫 번째 매개 변수가 아니라 이진 이름이다. argv [1]에서 시작한다. 그러므로 내 실수 다. –

+0

서브 쉘을 만들지 않고'while' 루프의 출력을 리디렉션 할 수 있습니다. 그리고'cat'을 파이프하지 않고 while 루프로 파일을 읽을 수 있습니다. '동안 ...; ...; done < input > output' 또한 이것이 Bash (OP가 질문을 태그) 인 경우,'base = 16; hexnum = 'aa'; decnum = $ (($ base # $ hexnum))'십진수를 10 진수로 변환합니다. Bash와'sh '는 공백으로 나눌 수 있습니다. –

관련 문제