为什么 Scala 在读取我的 CSV 时崩溃?
该文件位于此处
http://dl.dropbox.com/u/12337149/history.csv
我尝试按如下方式读取数据,
for (line <- Source.fromFile(new File(file)).getLines) {
println(line)
}
但出现以下错误
Exception in thread "main" java.nio.charset.MalformedInputException: Input length = 1
at java.nio.charset.CoderResult.throwException(CoderResult.java:260)
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.readLine(BufferedReader.java:299)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at scala.io.BufferedSource$BufferedLineIterator.<init>(BufferedSource.scala:32)
at scala.io.BufferedSource.getLines(BufferedSource.scala:43)
at com.alluvia.reports.RunIGConverter$$anonfun$main$1.apply(RunIGConverter.scala:17)
at com.alluvia.reports.RunIGConverter$$anonfun$main$1.apply(RunIGConverter.scala:15)
at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:34)
at scala.collection.mutable.ArrayOps.foreach(ArrayOps.scala:38)
at com.alluvia.reports.RunIGConverter$.main(RunIGConverter.scala:15)
at com.alluvia.reports.RunIGConverter.main(RunIGConverter.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
该文件在 Excel 中打开得很好。我认为这是某种类型的编码问题,但我不知道解决方法
The file is here
http://dl.dropbox.com/u/12337149/history.csv
I try to read the data as follows
for (line <- Source.fromFile(new File(file)).getLines) {
println(line)
}
I get the following error
Exception in thread "main" java.nio.charset.MalformedInputException: Input length = 1
at java.nio.charset.CoderResult.throwException(CoderResult.java:260)
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.readLine(BufferedReader.java:299)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at scala.io.BufferedSource$BufferedLineIterator.<init>(BufferedSource.scala:32)
at scala.io.BufferedSource.getLines(BufferedSource.scala:43)
at com.alluvia.reports.RunIGConverter$anonfun$main$1.apply(RunIGConverter.scala:17)
at com.alluvia.reports.RunIGConverter$anonfun$main$1.apply(RunIGConverter.scala:15)
at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:34)
at scala.collection.mutable.ArrayOps.foreach(ArrayOps.scala:38)
at com.alluvia.reports.RunIGConverter$.main(RunIGConverter.scala:15)
at com.alluvia.reports.RunIGConverter.main(RunIGConverter.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
The file opens just fine in excel. I think it is some type of encoding issue but I do not know the work around
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会尝试 ISO8859_1 编码,或者 Cp1252(如果不起作用),如下所示:
您可以查看 Sun Java 支持哪些编码 此处。我忘记你应该使用 nio 还是 io 版本。 (正如你从我的回答中看到的,它同时使用了两者。)
I'd try the ISO8859_1 encoding, or Cp1252 if that doesn't work, as so:
You can see which encodings Sun Java supports here. I forget whether you're supposed to use the nio or io versions. (As you can see from my answer, which has used both.)