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), e.g.:
val source = Source.fromResource(pathWithoutSlash)(Codec.UTF8)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With