简体   繁体   中英

Java doesn't find solution package in resource folder Intellij

This sample is supposed to locate the copied over.zip file from the build output directory but for some reason it doesn't find that file. I created a resource directory in the root directory and marked it as "Resource Root" and still didn't work. Any help is appreciated. Here is a snippet of the code:

if (install && customSolutionID == null) {
    File file = new File(customSolutionFilename);
    byte[] bytes = new byte[(int) file.length()];
    //Locate the solution zip file, which should have been copied over to the build
            //output directory.
    try {
    FileInputStream inputStream = new FileInputStream(file);
    inputStream.read(bytes);
    inputStream.close();
    }
    catch (FileNotFoundException ex) {
        System.out.println("File Not Found.");
        return;
    }
    catch (IOException ex) {
    System.out.println("Error Reading The File.");
    ex.printStackTrace();
    }

Here is a screenshot of the project structure: Project Structure

I suppose you mean you've created the file in the /main/resources folder of your project?

If that's so, then you can copy it as such:

public static void main(String[] args) throws IOException {
    var path = Main.class.getResourceAsStream("/myfile.txt");
    Files.copy(path, Path.of("file.txt"));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM