gpt4 book ai didi

java - 在构建的应用程序中获取资源

转载 作者:行者123 更新时间:2023-11-30 02:54:45 24 4
gpt4 key购买 nike

我想在我的应用程序运行时获取文件夹内的文件,所以我知道我需要将其作为资源获取,如果我将其作为文件获取,它将无法工作,所以这就是我所做的。

jaxbContext = JAXBContext.newInstance(Catalogo.class);
jaxbUnmarshaller = jaxbContext.createUnmarshaller();
InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream("catalogos/");
BufferedReader br = new BufferedReader(new InputStreamReader(resourceAsStream));
String line;
try {
while((line = br.readLine()) != null){
InputStream resourceAsStream1 = getClass().getClassLoader().getResourceAsStream("catalogos/"+line);
tempCat = (Catalogo) jaxbUnmarshaller.unmarshal(resourceAsStream1);
if(tempCat != null){
codigoCurso = String.valueOf(tempCat.getCourse().getId());
nomeDoCurso = dados.get(codigoCurso);
anoCatalogo = String.valueOf(tempCat.getAno());
if(nomeDoCurso == null){
dados.put(codigoCurso, tempCat.getCourse().getNome());
}
anos.add(anoCatalogo);
}
}

我想要做的是,获取文件夹(/catalogos/)内的所有文件,然后循环并将每个文件解码到一个对象,以便我能够访问我需要的属性。因此,当我使用 netbeans 运行此程序时,效果很好,但是当我构建并运行 jar 时,我没有得到与使用 netbeans 相同的结果,我的意思是,数据不是我预期的。

最佳答案

下面的示例演示如何从当前可运行的jar文件中的目录中获取文件并读取这些文件的内容。

假设您有一个名为“FolderTestApp”的 NetBeans 项目。执行以下步骤:

  1. 在项目根文件夹 FolderTestApp\ 中创建文件夹 myFiles
  2. catalogos 文件夹复制到 FolderTestApp\myFiles\

files in the project structure

生成项目 jar 时,需要

myFiles 文件夹在 jar 文件结构中保留 catalogos 文件夹。 myFiles 文件夹将从 jar 文件中消失,但 catalogos 文件夹将保留。

catalogos folder in generated jar file

如果您不执行这些步骤,并将 catalogos 直接放置到项目文件夹中(而不是作为 myFiles 的子文件夹),那么 Catalogos 文件夹中的文件将被放置到你的 jar 文件。

  • 在 netbeans 项目属性中添加 myFiles 文件夹作为源文件夹。
  • adding folder to the source folders list

    假设您的属性文件包含以下内容:

    文件1.属性:

    key11=value11
    key12=value12
    key13=value13

    文件2.属性:

    key21=value21
    key22=value22
    key23=value23

    请注意,下面的代码尚未优化。这是简单的概念证明,展示如何解决您的任务。

    将以下类添加到您的项目中:

    package folderapp;

    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.URISyntaxException;
    import java.util.Properties;
    import java.util.Set;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;


    public class FolderTestApp {

    public static void main(String[] args) throws URISyntaxException, IOException {
    new FolderTestApp();
    }

    public FolderTestApp() throws URISyntaxException, IOException {

    // determining the running jar file location
    String jarFilePath = getClass().getProtectionDomain().
    getCodeSource().getLocation().toURI().getPath();

    // note, that the starting / is removed
    // because zip entries won't start with this symbol
    String zipEntryFolder = "catalogos/";

    try (ZipInputStream zipInputStream
    = new ZipInputStream(new FileInputStream(jarFilePath))) {

    ZipEntry zipEntry = zipInputStream.getNextEntry();
    while (zipEntry != null) {

    System.out.println("processing: " + zipEntry.getName());

    if (zipEntry.getName().startsWith(zipEntryFolder)) {
    // directory "catalogos" will appear as a zip-entry
    // and we're checking this condition
    if (!zipEntry.isDirectory()) {
    // adding symbol / because it is required for getResourceAsStream() call
    printProperties("/" + zipEntry.getName());
    }

    }

    zipEntry = zipInputStream.getNextEntry();
    }

    }

    }

    public void printProperties(String path) throws IOException {

    try (InputStream is = getClass().getResourceAsStream(path)) {
    InputStreamReader fr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(fr);

    Properties properties = new Properties();
    properties.load(br);

    System.out.println("contents from: " + path + "\n");

    Set<Object> keySet = properties.keySet();
    for (Object key : keySet) {
    System.out.println(key + " = " + properties.get(key));
    }

    System.out.println("---------------------------------------");
    }
    }

    }

    在项目设置(Run 部分)中将此类设置为主类。

    main class setup

    并通过菜单构建您的项目:运行 - 构建

    项目构建完成后,打开生成的 jar 所在的 FolderTestApp/dist 文件夹并运行以下 jar 文件:

    running the generated jar file

    就是这样:)

    关于java - 在构建的应用程序中获取资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37599149/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com