#Create a file from Resource

14 messages · Page 1 of 1 (latest)

forest hill
#

I want to create a .yml from a config.yml file in the resource folder and read it using SnakeYaml, i tried some methods but none of it worked

pearl socketBOT
#

This post has been reserved for your question.

Hey @forest hill! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

exotic jay
#

what happens when trying?

forest hill
#

Nothing, it just don't work

exotic jay
#

and what did you try?

forest hill
#

I found a way and it loaded the file but in the wrong way and snakeyaml didn'd return any data

exotic jay
#

?

forest hill
#

I tried this

        if (resourcePath != null && !resourcePath.equals("")) {
            resourcePath = resourcePath.replace('\\', '/');
            InputStream in = this.getResource(resourcePath);

            if (in == null) {
                throw new IllegalArgumentException("The embedded resource '" + resourcePath + "' cannot be found in " + configFile.getName());
            }

            File outFile = new File(configFile, resourcePath);
            int lastIndex = resourcePath.lastIndexOf(47);
            File outDir = new File(configFile, resourcePath.substring(0, Math.max(lastIndex, 0)));
            if (!outDir.exists()) {
                outDir.mkdirs();
            }

            try {
                if (outFile.exists() && !replace) {
                    Logger.getLogger("FadedMusicBot").log(Level.WARNING, "Could not save " + outFile.getName() + " to " + outFile + " because " + outFile.getName() + " already exists.");
                } else {
                    OutputStream out = new FileOutputStream(outFile);
                    byte[] buf = new byte[1024];

                    int len;
                    while ((len = in.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }

                    out.close();
                    in.close();
                }
            } catch (IOException var10) {
                Logger.getLogger("FadedMusicBot").log(Level.SEVERE, "Could not save " + outFile.getName() + " to " + outFile, var10);
            }


        } else {
            throw new IllegalArgumentException("ResourcePath cannot be null or empty");
        }
    }```
#
        configFile = new File(getProgramPath() + File.separator + directory.replace("/", File.separator));

        if (!configFile.exists()) {
            configFile.getParentFile().mkdirs();
            saveResource(directory, replace);
        }

        yaml.load(directory);
    }```
#

and it created a folder called config.yml containing the actual .yml file and snakeyaml doesn't return any data from it

exotic jay
#

what is the yaml variable?

forest hill
#

private final Map<Object, Object> configuration = new HashMap<>();

#

But the major problem is that it is creating it in the wrong way, i don't want it to create the folder called config.yml and i don't know how to fix it