如何解决 Scala 2.8.0 中的 java.nio.charset.UnmappableCharacterException?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1757272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to resolve java.nio.charset.UnmappableCharacterException in Scala 2.8.0?
提问by Roman Kagan
I'm using Scala 2.8.0 and trying to read pipe delimited file like in code snipped below:
我正在使用 Scala 2.8.0 并尝试读取管道分隔的文件,如下面的代码所示:
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
线程“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) 在 sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158) 在 java.io.InputStreamReader.read(InputStreamReader.java:167) 在 java.io.BufferedReader.fill(BufferedReader.java:136) 在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) 在 scala.collection.Iterator$$anon$2.head(Iterator.scala:732) 在 scala.collection.Iterator$$anon$24.hasNext(Iterator.scala:405) 在 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 结果:1next(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 结果:1next(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 结果:1Iterator$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 结果:1Iterator$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 结果:1
回答by Daniel C. Sobral
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)
}
}
}
回答by maasg
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:
我在同样的问题上苦苦挣扎,这个答案帮助了我。我想扩展 seh 关于“为什么这行得通”的评论。答案应该在于方法签名:
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:
它采用隐式编解码器参数。然而,在示例中,提供的是字符串,而不是编解码器。第二个转换发生在幕后:Codec 类的伴随对象定义了一个来自 String 的 apply 方法:
def apply(encoding: String): Codec
so the compiler has done some work for us: val lines = Source.fromFile(someFile)(Codec("UTF-8"))
所以编译器为我们做了一些工作: 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:
鉴于 Codec 是隐式的,如果您多次调用此方法,您还可以在您的调用范围内创建一个 Codec 对象:
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 n00b,正在掌握它 - 随时在需要时进行更正)
回答by Bren1818
To add to Daniel C. Sobral's answer, you can also try something like this:
要添加到 Daniel C. Sobral 的回答中,您还可以尝试这样的操作:
val products = Source.fromFile("products.txt")("UTF-8").getLines().toList;
for(p <- products){
println("product :" + p);
}
回答by Evans Y.
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.
通过这两个设置,您可以避免文件中的格式错误的数据。

