Last active
March 28, 2023 22:35
-
-
Save AlexBaitov/4dbd610eebca9d40e78e9da1e3af41f9 to your computer and use it in GitHub Desktop.
Different ways to get file from resource
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1 | |
| Source.fromResource("/public.pem").getLines() | |
| // 2 | |
| getClass.getResourceAsStream("/public.pem") match { | |
| case null => throw new FileNotFoundException("/public.pem") | |
| case stream => Source.fromInputStream(stream).getLines().mkString | |
| } | |
| // 3 | |
| Try(scala.io.Source.fromURL(getClass.getResource("/public.pem"))) | |
| .recover{case _ => throw new FileNotFoundException("/public.pem")} | |
| .get | |
| // 4 | |
| Try(scala.io.Source.fromURL(getClass.getResource("/public.pem"))) | |
| .getOrElse(throw new FileNotFoundException("/public.pem")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * image in /var/www/scala/nagual/it-nagual/src/test/resources/logo.png | |
| * test in /var/www/scala/nagual/it-nagual/src/test/scala/ru/dgis/myteam/nagual/MyTest.scala | |
| * image will appear in /var/www/scala/nagual/it-nagual/target/scala-2.12/test-classes/logo.png | |
| * test will appear in /var/www/scala/nagual/it-nagual/target/scala-2.12/test-classes/ru/dgis/myteam/nagual/MyTest.class | |
| */ | |
| package ru.dgis.myteam.nagual | |
| import java.io.{File, FileInputStream} | |
| import java.net.URL | |
| import java.nio.file.{Files, Paths} | |
| import org.apache.commons.compress.utils.IOUtils | |
| import org.scalatest.{FeatureSpec, Matchers} | |
| class MyTest extends FeatureSpec with Matchers { | |
| feature("Working with images") { | |
| scenario("First test") { | |
| val name = "/logo.png" | |
| // val name = "/tost.txt" | |
| val fl = new File(name) | |
| println(fl.getPath) // /logo.png | |
| println(fl.exists()) // false | |
| println(getClass) // class ru.dgis.myteam.nagual.MyTest | |
| val fl2: URL = getClass.getResource(name) | |
| println(fl2) // file:/var/www/scala/nagual/it-nagual/target/scala-2.12/test-classes/logo.png | |
| println(fl2.getFile) // /var/www/scala/nagual/it-nagual/target/scala-2.12/test-classes/logo.png | |
| println(fl2.getPath) // /var/www/scala/nagual/it-nagual/target/scala-2.12/test-classes/logo.png | |
| println(new File(fl2.getFile)) // /var/www/scala/nagual/it-nagual/target/scala-2.12/test-classes/logo.png | |
| println(new File(fl2.getFile).getName) // logo.png | |
| println(new File(fl2.getFile).exists()) // true | |
| val bytes: Array[Byte] = IOUtils.toByteArray(new FileInputStream(fl2.getFile)) | |
| println(bytes) // [B@6dbb137d | |
| val bytes2: Array[Byte] = Files.readAllBytes(Paths.get(fl2.getPath)) | |
| println(bytes2) // [B@3c9d0b9d | |
| println(bytes.map(_.toChar).mkString) | |
| // ノPNG | |
| // | |
| // IHDR  | |
| // ... | |
| println(bytes2.map(_.toChar).mkString) | |
| // ノPNG | |
| // | |
| // IHDR  | |
| // ... | |
| "1" shouldBe "1" | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment