2
將文件加載到Java應用程序以傳遞給另一個類的最佳方式是什麼?Java加載文件傳遞給其他類
目前我正在使用JFileChooser來選擇一個源文件(C,C++,Java),然後將其傳遞給名爲src2srcml的可執行文件。我的代碼運行src2srcml工具,該工具接收源文件並將其轉換爲XML,然後存儲在我的工作區(eclipse)中。然後我想要將該XML文件傳遞給另一個類進行分析。正如你可以看到下面我目前正在嘗試getResources方法。它可以找到文件,但我實際上並不知道如何將它傳遞給UnitXMLReader類。 GetResources返回該文件的URL,但其他類需要文件路徑。有沒有更好的方法來查找文件?
JButton btnRunSourceCode = new JButton("Run Source Code");
btnRunSourceCode.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//------Check for loaded file ----//
if(filePath == null){
textArea.setText("Please Load a source file (C, C++, Java)");
}
else{
try{
int c;
textArea.setText("Converting Source Code to XML");
String workspace = System.getProperty("user.dir");
String classPath = System.getProperty("java.class.path");
String[] commands = {"/bin/bash", "-c", "cd " + workspace + " && ./src2srcml --position " + selectedFile.getName() + " -o " + classPath + "/xmlParseGUI/targetFile.xml"};
Process src2XML = Runtime.getRuntime().exec(commands);
InputStream in1 = src2XML.getErrorStream();
InputStream in2 = src2XML.getInputStream();
while ((c = in1.read()) != -1 || (c = in2.read()) != -1) {
System.out.print((char)c);
}
src2XML.waitFor();
}
catch(Exception exc){/*src2srcml Fail*/}
}
ParallelXMlGUI c = new ParallelXMlGUI();
Class<? extends ParallelXMlGUI> cls = c.getClass();
// finds resource relative to the class location
URL url = cls.getResource("targetFile.xml");
//UnitXMLReader.ChosenFile = filePath;
//UnitXMLReader.main(null);
System.out.println("Value = " + url);
檔案文件=新的文件(文件路徑); – brso05 2014-09-23 17:28:12