Download files from JAR to Scala

I have the following code structure:

Projects/ classes/ performance/AcPerformance.class resources/ Aircraft/ allAircraft.txt 

I have the contents of the class folder in the JAR, and my Acperformance scala code is trying to read the contents of the text files of the Aircraft folder. My code is:

 val AircraftPerf = getClass.getResource("resources/Aircraft").getFile val dataDir = new File(AircraftPerf) val acFile = new File(dataDir, "allAircraft.txt") for (line <- linesFromResource(acFile)) { // read in lines } 

When I try to run the code, I get the following error:

Called: java.io.FileNotFoundException: C: \ Projects \ file: \ C: \ Projects \ libraries \ aircraft.jar! \ Aircraft \ allAircraft.txt (Invalid file name, directory or volume name)

Is it right to read the contents of the JAR? Thanks!

+6
source share
1 answer

No, the getFile URL will not do what you want here - the path it gives you is not the path to the file system that you can use in the File constructor. It is best to use getResourceAsStream and the full path to the resource:

 val in = getClass.getResourceAsStream("/resources/Aircraft/allAircraft.txt") 

Note that you also need to specify the path with / to make it absolute - in the current version you are looking for the resources directory in the performance section.

+8
source

Source: https://habr.com/ru/post/913679/


All Articles