【问题标题】:Getting xml file from src folder of the package从包的 src 文件夹中获取 xml 文件
【发布时间】:2013-01-17 10:35:51
【问题描述】:

我之前问过一个关于如何在 stackoverflow (Read here) 上从 xml 导入文件的问题。提供给我的解决方案是使用

documentbuilder.parse( xmlhandler.class.getResourceAsStream("shutdownscheduler.xml")); 

此解决方案正在从中选择 xml 文件 "C:\Users\Rohan Kandwal\Documents\NetBeansProjects\Shutdown Scheduler\build\classes\shutdown\scheduler\shutdownscheduler.xml"这对于只读目的来说很好。

但我需要 documentbuilder 从位置 "C:\Users\Rohan Kandwal\Documents\NetBeansProjects\Shutdown Scheduler\src\shutdown\scheduler 访问文件,以便我可以永久保存对 xml 所做的更新。 有没有办法从src 文件夹而不是build 文件夹访问文件?

【问题讨论】:

标签: xml swing


【解决方案1】:

通常当您的应用程序应该写入文件时,它必须在具有写入权限的地方执行此操作。您可以确定(在大多数情况下)您的应用程序将被允许具有此类访问权限的唯一地方是用户主目录。您可能已经注意到,其他基于 java 的应用程序通常会在您的主文件夹中创建一个名为 ".some-application" 的目录(在您的情况下为 "C:\Users\Rohan Kandwal")。他们这样做的原因(其中之一)是写访问权限。

您也应该这样做,并且忘记将Class.getResourceAsStream(...) 用于可写文件。您可以通过System.getProperty("user.home") 获取到您的主目录的路径。

请注意,示例中使用的Class.getResourceAsStream(...) 之所以从build 目录中获取文件,是因为您在IDE 中运行它。如果你在 IDE 之外运行它,这条路径将会改变。这是因为类文件 (xmlhandler.class) 的路径很可能会改变,Class.getResourceAsStream(...) 依赖它来查找资源。

另请注意,在部署应用程序时,您的src 目录很可能甚至不存在。即使这样做了,也无法知道您的用户是否会将其部署在具有写入权限的位置。因此,您尝试做的事情没有多大意义。

编辑01

这是一个如何做你想做的事的例子。这将为您的应用程序创建一个主目录,您可以在其中以任何您希望的方式编写文件。如果您要在应用程序运行之间保留的文件不存在,它将被创建并填充您的资源文件的内容。请注意,这只是一个示例。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class ConfigDir {

    private static final String APP_DIR = ".myappdir";
    private static final String FILE_NAME = "shutdownscheduler.xml";

    private File fillFileFromTemplate(InputStream template, File file) throws FileNotFoundException, IOException {
            OutputStream out = null;
            try {
                out = new FileOutputStream(file);
                int read = 0;
                byte[] bytes = new byte[1024];
                while ((read = template.read(bytes)) != -1) {
                    out.write(bytes, 0, read);
                }
                out.flush();
                return file;
            } catch (FileNotFoundException ex) {
                throw ex;
            } finally {
                if (out != null) {
                    out.close();
                }
            }
        }

    public static void main(String[] argv) throws IOException {        
        String userdir = System.getProperty("user.home"); // user home directory
        // OR if you know that your program will run from a directory that has write access
        // String userdir = System.getProperty("user.dir"); // working directory
        if (!userdir.endsWith(System.getProperty("file.separator"))) {
            userdir += System.getProperty("file.separator");
        }
        String myAppDir = userdir + APP_DIR;
        File myAppDirFile = new File(myAppDir);
        myAppDirFile.mkdirs(); // create all dirs if they do not exist
        String myXML = myAppDir + System.getProperty("file.separator") + FILE_NAME;
        File myXMLFile = new File(myXML); // this is just a descriptor        
        ConfigDir cd = new ConfigDir();        
        if (!myXMLFile.exists()) {
            // if the file does not exist, create one based on your template
            // replace "ConfigDir" with a class that has your xml next to it in src dir
            InputStream template = ConfigDir.class.getResourceAsStream(FILE_NAME);  
            cd.fillFileFromTemplate(template, myXMLFile);
        }

        // use myXMLFile java.io.File instance for read/write from now on
        /*
        DocumentBuilderFactory documentbuilderfactory=DocumentBuilderFactory.newInstance();
        DocumentBuilder documentbuilder=documentbuilderfactory.newDocumentBuilder();
        Document document=(Document)documentbuilder.parse(myXMLFile);
        document.getDocumentElement(); 
        ...
        */        
    }    
}

【讨论】:

  • 我知道部署应用程序时我的文件路径会发生变化。这就是我不提供文件的直接路径的原因,即 C:\Users\Rohan Kandwal\Documents\NetBeansProjects\Shutdown Scheduler\src\shutdown\scheduler 。在我之前的问题中,我还问了同样的问题,即即使已部署该文件,也应始终从我的包中导入该文件。正在工作的成员给了我一个解决方案,我认为它是正确的。您能否给我一些方法,以便即使在部署之后也可以访问该文件?
  • 这会创建一个新的空白 xml 文件还是将文件从我的包中复制到主目录?对不起,我是菜鸟。
  • @RohanKandwal,正如我在编辑中所述,它会创建一个新的空白文件并用您的模板文件中的内容填充它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-09
  • 2014-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多