Search code examples
scalascalatest

%3d instead of = in file path, then i try to open file from resources


I write some tests and to get absolute path from relative path i use this function

    private def getAbsolutePath(filePath: String): String = {
        getClass.getResource(filePath).getFile
    } 

and then i do:

println(getAbsolutePath("/parquetIncrementalProcessor/withPartitioning/"))
println(getAbsolutePath("/parquetIncrementalProcessor/withPartitioning/own_loading_id=1/partition_column=test/"))

i get:

/Users/19658296/csp-fp-snaphot/library/target/scala-2.11/test-classes/parquetIncrementalProcessor/withPartitioning/
/Users/19658296/csp-fp-snaphot/library/target/scala-2.11/test-classes/parquetIncrementalProcessor/withPartitioning/own_loading_id%3d1/partition_column%3dtest/

As you can see, instead of =, I get some strange symbol. At the same time, when I try to read these files with a park, he can read the path without %3d, and with %3d he gets the error "Path does not exist".

How can I fix this?


Solution

  • Seems like its URL encoded, maybe because using stuff from files and resources are designed to work with Universal Resource Locators. You can URLDecode it like so:

    import java.net.URLDecoder
    def getAbsolutePath(filePath: String): String = {
        val path = getClass.getResource(filePath).getFile
        URLDecoder.decode(path, "UTF-8")
    }