我想通过编译以下代码来调用Java类文件:
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 :(得分:2)
我认为你在这里有几个步骤,首先是改变主要使用args。您应该检查args.length以确保指定了源文件,例如:
(警告:来自C程序员的未经测试的java)
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
请注意,如果你的目标是将十六进制数的文件转换为十进制数,那么使用java和bash可能有点过分。您可以使用单个shell脚本完成此操作,如:
#!/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