0

Java project build by Gradle.

I want to create file in \build\resources\main\db\sqlite\insert-data.sql

I try this:

  protected File getDestinationFile(String baseDir) {
    try {
        URL testURL = ClassLoader.getSystemClassLoader().getResource("db/sqlite");
        String pathName = testURL + "/" + DEFAULT_FILE_NAME_INSERT_DATA;
        logger.debug("pathName = " + pathName);
        File newFile = new File(pathName);
        newFile.createNewFile();
        return newFile;
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
        return null;
    }
}

But I get error on runtime:

[17.06.2022 00:19:38.223] MyClass.getDestinationFile(SQLiteStrategy.java:170) ERROR:
   java.io.IOException: 
    at java.io.WinNTFileSystem.createFileExclusively(Native Method)
    at java.io.File.createNewFile(File.java:1012)
5
  • What does testURL look like? Maybe it doesn't point to where you expect it. Commented Jun 16, 2022 at 21:58
  • A resource is not a file, it is data packaged in a JAR. Commented Jun 17, 2022 at 11:41
  • Alternatively, look at How to include SQLite database in executable Jar? if you want to open a read-only sqlite database. Commented Jun 17, 2022 at 11:46
  • @akarnokd pathName = file:/C:/dev/temp/m2cm/app/build/resources/main/db/sqlite/insert-data.sql Commented Jun 17, 2022 at 17:20
  • 1
    That's not a valid file name. There is URL to URI to File chain to get a valid File object stackoverflow.com/a/54123394/61158 Commented Jun 17, 2022 at 17:41

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.