简体   繁体   中英

Source.fromResource() works locally, but throws java.nio.charset.MalformedInputException on server

I'm using this code to read a resource:

  val source = Source.fromResource(pathWithoutSlash)
  val lines:Seq[String] = (for (l <- source.getLines() if ! l.trim.isEmpty) yield l.trim).toList

This code works fine when I run it locally - but on the server, it fails with:

Exception in thread "main" java.nio.charset.MalformedInputException: Input length = 1
    at java.base/java.nio.charset.CoderResult.throwException(CoderResult.java:274)
    at java.base/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:339)
    at java.base/sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
    at java.base/java.io.InputStreamReader.read(InputStreamReader.java:185)
    at java.base/java.io.BufferedReader.fill(BufferedReader.java:161)
    at java.base/java.io.BufferedReader.readLine(BufferedReader.java:326)
    at java.base/java.io.BufferedReader.readLine(BufferedReader.java:392)
    at scala.io.BufferedSource$BufferedLineIterator.hasNext(BufferedSource.scala:70)

I'm guessing its because the file does contain some accented characters, like: éclair's , and probably the default charset being used on the server is different from what I have locally.

My question is, how can I change the charset on the server so it matches whatever I have locally (and how can I check what I have locally)?

Thanks.

I'd assume that the implicit Codec value that you can see with

println(implicitly[scala.io.Codec])

is different on your server. If I understand it correctly, it should evaluate to scala.io.Codec.fallbackSystemCodec . Just pass the appropriate Codec explicitly (the fromResource method takes an implicit Codec in the second parameter list), eg:

val source = Source.fromResource(pathWithoutSlash)(Codec.UTF8)

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