简体   繁体   中英

java.nio.charset.MalformedInputException when reading a csv file

I am trying to read values of a csv file in scala.

val bufferedSource = io.Source.fromFile(destFile);
for (line <- bufferedSource.getLines) {
      rows += line.split(",").map(_.trim)
}

However when I am doing io.Source.fromFile(destFile); I am getting an exception

java.nio.charset.MalformedInputException: Input length = 1
at java.nio.charset.CoderResult.throwException(CoderResult.java:281)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:339)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at scala.io.BufferedSource$BufferedLineIterator.hasNext(BufferedSource.scala:70)
at scala.collection.Iterator.foreach(Iterator.scala:929)
at scala.collection.Iterator.foreach$(Iterator.scala:929)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1417)
at TestFileProcessZip7z$.$anonfun$main$1(TestFileProcessZip7z.scala:79)
at TestFileProcessZip7z$.$anonfun$main$1$adapted(TestFileProcessZip7z.scala:34)
at scala.collection.immutable.List.foreach(List.scala:389)
at TestFileProcessZip7z$.main(TestFileProcessZip7z.scala:34)
at TestFileProcessZip7z.main(TestFileProcessZip7z.scala)

()

How do I resolve the above issue? Thanks in advance

尝试将字符集赋予ISO-8859-1 val bufferedSource = io.Source.fromFile(destFile,“ ISO-8859-1”);

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