How to resolve java.nio.charset.UnmappableCharacterException in Scala 2.8.0?

I'm using Scala 2.8.0 and trying to read pipe delimited file like in code snipped below:

object Main {
  def main(args: Array[String]) :Unit = {
    if (args.length > 0) {
      val lines = scala.io.Source.fromPath("QUICK!LRU-2009-11-15.psv")
     for (line <-lines)
       print(line)
    }
  }
}

Here's the error:

Exception in thread "main" java.nio.charset.UnmappableCharacterException: Input length = 1 at java.nio.charset.CoderResult.throwException(CoderResult.java:261) at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:319) at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158) at java.io.InputStreamReader.read(InputStreamReader.java:167) at java.io.BufferedReader.fill(BufferedReader.java:136) at java.io.BufferedReader.read(BufferedReader.java:157) at scala.io.BufferedSource$$anonfun$1$$anonfun$apply$1.apply(BufferedSource.scala:29) at scala.io.BufferedSource$$anonfun$1$$anonfun$apply$1.apply(BufferedSource.scala:29) at scala.io.Codec.wrap(Codec.scala:65) at scala.io.BufferedSource$$anonfun$1.apply(BufferedSource.scala:29) at scala.io.BufferedSource$$anonfun$1.apply(BufferedSource.scala:29) at scala.collection.Iterator$$anon$14.next(Iterator.scala:149) at scala.collection.Iterator$$anon$2.next(Iterator.scala:745) at scala.collection.Iterator$$anon$2.head(Iterator.scala:732) at scala.collection.Iterator$$anon$24.hasNext(Iterator.scala:405) at scala.collection.Iterator$$anon$20.hasNext(Iterator.scala:320) at scala.io.Source.hasNext(Source.scala:209) at scala.collection.Iterator$class.foreach(Iterator.scala:534) at scala.io.Source.foreach(Source.scala:143) ... at infillreports.Main$.main(Main.scala:8) at infillreports.Main.main(Main.scala) Java Result: 1

object Main {
  def main(args: Array[String]) :Unit = {
    if (args.length > 0) {
      val lines = scala.io.Source.fromPath("QUICK!LRU-2009-11-15.psv")("UTF-8")
      for (line <-lines)
        print(line)
    }
  }
}

java.nio.charset.MalformedInputException: Input length = 1 · Issue , Play Version (2.7.0) API (Java) Operating System (Windows 10) JDK in RoutesCompiler.scala and in RoutesFileParser.scala below here: MalformedInputException: Input length = 1 at java.nio.charset. The problem is resolved. dwijnand added this to the Play 2.8.0-M1 milestone on May 24, 2019. How to resolve java.nio.charset.UnmappableCharacterException in Scala 2.8.0? (3) I'm using Scala 2.8.0 and trying to read pipe delimited file like in code snipped below:

I was struggling with this same issue and this answer helped me. I wanted to extend on the comment of seh regarding the 'why this works'. The answer should lie on the method signature:

def fromFile(file: JFile)(implicit codec: Codec): BufferedSource

It takes an implict codec parameter. Yet, on the example, a string is provided, not a codec. A second translation is taking place behind the scenes: The companion object of the class Codec defines an apply method from String:

def apply(encoding: String): Codec

so the compiler has done some work for us: val lines = Source.fromFile(someFile)(Codec("UTF-8"))

Given that Codec is implicit, if you are calling this method several times, you can also create a Codec object in the scope of your call:

implicit val codec = Codec("UTF-8")
val lines = Source.fromFile(someFile)
val moreLines = Source.fromFile(someOtherFile)

I hope I got that right (I'm still a Scala n00b, getting my grips on it - feel free to correct where needed)

Scala 2.8.0でjava.nio.charset.UnmappableCharacterExceptionを , How to resolve java.nio.charset.UnmappableCharacterException in Scala 2.8.0? I'm using Scala 2.8.0 and trying to read pipe delimited file like in code snipped  I am able to reproduce it by explicitly setting wrong encoding in CodeGrid.scala line 63: private def source ( mfile : MeasuredFile ) : String = Source .fromFile(mfile.source, " windows-1252 " ).mkString

To add to Daniel C. Sobral's answer, you can also try something like this:

val products = Source.fromFile("products.txt")("UTF-8").getLines().toList;

for(p <- products){
        println("product :" + p);
}

Error with Python 3 in RPi - python-3.4 - csharp, java.io.IOException · java.nio.charset. UnmappableCharacterException(int inputLength). Constructs an UnmappableCharacterException with the given length. java.nio.charset.UnmappableCharacterException on 1.7.0-SNAPSHOT using Scala 2.11.0-M8 and Java 8 on OS X #279 Open boxsterman opened this issue Feb 12, 2014 · 1 comment

This maybe a more generic solution:

implicit val codec = Codec("UTF-8")
codec.onMalformedInput(CodingErrorAction.REPLACE)
codec.onUnmappableCharacter(CodingErrorAction.REPLACE)

with the two settings, you can avoid the malformed data in file.

UnmappableCharacterException (Java Platform SE 8 ), How to resolve java.nio.charset.UnmappableCharacterException in Scala 2.8.0?​Error during sbt execution: java.lang.NoClassDefFoundError:  Stack Overflow Public questions A quick fix would be to wrap the the call to How to resolve java.nio.charset.UnmappableCharacterException in Scala 2.8.0? 6.

java.lang.NoClassDefFoundError: sbt/State$NextImport a , object Main { def main(args: Array[String]) :Unit = { if (args.length > 0) { val lines = scala.io.Source.fromPath("QUICK!LRU-2009-11-15.psv")("UTF-8") for (line  I'm a newbie in Scala, and I wanted to write some sourcecodes from myself for me to get better. I've written a simple object (with a main entry) in order to simulate a "grep" call on all files of the

It is mostly a UTF-8 file but some of the field values are in different encodings, so there is no encoding in which the whole file is valid, but I need to parse it anyway. Apart from using java libraries like Weka, I am mainly working in Scala. I am not even able to read the file usin scala.io.Source: For example. Source. fromFile(filename

NullPointerException in Scala Spark, appears to be caused be collection type? How to resolve java.nio.charset.UnmappableCharacterException in Scala 2.8.0? 9.

Comments
  • I hope you'll take the time to understand why adding "UTF-8" here was necessary, and how it changed the behavior of the reading of characters from the input stream. If you don't get it, please elaborate your question here or ask a new question concerning bytes, characters, and character encodings.
  • In scala 2.12.3 you shoud write Source.fromFile(fileName)(Codec("utf-8"))
  • import scala.io.{Source, Codec}
  • Actually, since Codec.apply is not an implicit conversion, there is no reason for the compiler to silently call it. In fact its is Codec.string2codec which is called here (as it is an implicit conversion from String to Codec)
  • Hi, I wanted to try your solution because the accepted answer did not solve my problem. But it doesn't compile, as the compiler does not recognize the value CodingErrorAction.REPLACE. I have imported scala.io._, which namespace am I missing?
  • You are missing java.nio.charset.CodingErrorAction