Java加载文件以传递给其他类

Java加载文件以传递给其他类,java,file-io,Java,File Io,将文件加载到java应用程序中并传递给另一个类的最佳方法是什么 目前我使用JFileChooser来选择一个源文件(C,C++,java),然后将其传递给一个可执行的SRC2SRCML。我的代码运行src2srcml工具,该工具获取源文件并将其转换为XML,然后存储在我的工作区(eclipse)中。然后我想把这个XML文件传递给另一个类进行分析。正如您在下面看到的,我目前正在尝试getResources方法。它可以很好地找到文件,但我实际上不知道如何将其传递给类UnitXMLReader。Get

将文件加载到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);
JButton btnRunSourceCode=newjbutton(“运行源代码”);
btnRunSourceCode.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件arg0){
//------检查加载的文件----//
if(filePath==null){
TestTra.StTeScript(“请加载源文件(C,C++,java));
}
否则{
试一试{
INTC;
setText(“将源代码转换为XML”);
字符串workspace=System.getProperty(“user.dir”);
字符串classPath=System.getProperty(“java.class.path”);
String[]commands={/bin/bash'、“-c”、“cd”+workspace+”&&//src2srcml--position“+selectedFile.getName()+”-o“+classPath+”/xmlParseGUI/targetFile.xml”};
进程src2XML=Runtime.getRuntime().exec(命令);
InputStream in1=src2XML.getErrorStream();
InputStream in2=src2XML.getInputStream();
而((c=in1.read())!=-1 | |(c=in2.read())!=-1){
系统输出打印((字符)c);
}
src2XML.waitFor();
}
catch(异常exc){/*src2srcml Fail*/}
}
ParallelXMlGUI c=新的ParallelXMlGUI();
类cls=c.getClass();
//查找相对于类位置的资源
URL=cls.getResource(“targetFile.xml”);
//UnitXMLReader.ChosenFile=文件路径;
//UnitXMLReader.main(空);
System.out.println(“Value=“+url”);
Java有一个用于处理文件的文件类。请尝试创建指向该文件的新文件对象,然后传递新文件对象

或者您可以尝试相对路径:

File file = new File("/smlParseGUI/targetFile.xml");
两种文件方法:

file.getPath();
file.getString();
文件文件=新文件(文件路径);
file.getPath();
file.getString();