gpt4 book ai didi

scala - 为什么我会从此代码中收到 MalformedInputException?

转载 作者:行者123 更新时间:2023-12-02 01:22:54 26 4
gpt4 key购买 nike

我是 Scala 的新手,我想自己编写一些源代码以提高自己的水平。我编写了一个简单的对象(带有主条目),以模拟对当前目录的所有文件的“grep”调用。 (我从 Eclipse Indigo 和 Debian Squeeze 中启动该程序):

package com.gmail.bernabe.laurent.scala.tests

import java.io.File

import scala.io.Source

object DealWithFiles {

def main(args:Array[String]){
for (result <- grepFilesHere(".*aur.*"))
println(result)
}

private def grepFilesHere(pattern:String):Array[String] = {
val filesHere = new File(".").listFiles

def linesOfFile(file:File) =
Source.fromFile(file).getLines.toList

for (file <- filesHere;
if file.isFile
)
yield linesOfFile(file)(0)
}

}

但是我得到了 java.nio.charset.MalformedInputException,我无法解决:

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.hasNext(BufferedSource.scala:67)
at scala.collection.Iterator$class.foreach(Iterator.scala:772)
at scala.io.BufferedSource$BufferedLineIterator.foreach(BufferedSource.scala:43)
at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:48)
at scala.collection.mutable.ListBuffer.$plus$plus$eq(ListBuffer.scala:130)
at scala.collection.TraversableOnce$class.toList(TraversableOnce.scala:242)
at scala.io.BufferedSource$BufferedLineIterator.toList(BufferedSource.scala:43)
at com.gmail.bernabe.laurent.scala.tests.DealWithFiles$.linesOfFile$1(DealWithFiles.scala:18)
at com.gmail.bernabe.laurent.scala.tests.DealWithFiles$$anonfun$grepFilesHere$2.apply(DealWithFiles.scala:23)
at com.gmail.bernabe.laurent.scala.tests.DealWithFiles$$anonfun$grepFilesHere$2.apply(DealWithFiles.scala:20)
at scala.collection.TraversableLike$WithFilter$$anonfun$map$2.apply(TraversableLike.scala:697)
at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:34)
at scala.collection.mutable.ArrayOps.foreach(ArrayOps.scala:38)
at scala.collection.TraversableLike$WithFilter.map(TraversableLike.scala:696)
at com.gmail.bernabe.laurent.scala.tests.DealWithFiles$.grepFilesHere(DealWithFiles.scala:20)
at com.gmail.bernabe.laurent.scala.tests.DealWithFiles$.main(DealWithFiles.scala:10)
at com.gmail.bernabe.laurent.scala.tests.DealWithFiles.main(DealWithFiles.scala)

提前感谢您的帮助:)

最佳答案

来自 JavaDoc:

MalformedInputException thrown when an input byte sequence is not legal for given charset, or an input character sequence is not a legal sixteen-bit Unicode sequence.

将当前编码作为参数传递给 Source.fromFile 方法。

关于scala - 为什么我会从此代码中收到 MalformedInputException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10846848/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com