错误:方法值重载,并带有替代方法:

时间:2018-07-11 09:02:20

标签: scala

我正在尝试读取固定宽度的文件并将其写为文本文件,下面是代码。字段的输入文件布局长度(4,10,3,5,3,1,1,5,7)

094482018-07-10 022 14012 000 0 30000A 002290 {

059412018-07-10 022 14013 000 0 30000A 002290 {

015172018-07-10 046 17502 000 0 30000A 001699I

scala> val inputdata = scala.io.Source.fromFile("/C:/Users/rrrrrr/Desktop/datas
tage/inputdata.txt")
inputdata: scala.io.BufferedSource = non-empty iterator

scala> val lines = inputdata.getLines
lines: Iterator[String] = non-empty iterator

scala> val matched=lines.map(l=>(l.substring(0,5).trim(),l.substring(5,5).trim()
))
matched: Iterator[(String, String)] = non-empty iterator

scala> val outputdata= new File("/C:/Users/rkumar0/Desktop/folder/output_N4.txt"
)
outputdata: java.io.File = C:\Users\rrrrrrr\Desktop\folder\output_N4.txt

scala> val writer= new BufferedWriter(new FileWriter(outputdata))
writer: java.io.BufferedWriter = java.io.BufferedWriter@759c0c14

scala> matched.foreach{line =>
     | writer.write(line)
     | writer.newLine()
     | }
<console>:19: error: overloaded method value write with alternatives:
  (x$1: Int)Unit <and>
  (x$1: String)Unit <and>
  (x$1: Array[Char])Unit
 cannot be applied to ((String, String))
       writer.write(line)
              ^

scala>

我的输出文件创建为空。

2 个答案:

答案 0 :(得分:2)

错误消息告诉您write接受IntStringArray[Char],但是您将其传递给{ 1}}。您需要将其传递为(String, String)IntString,而不是Array[Char]的2元组。

答案 1 :(得分:1)

您的行的类型为private func animateSentData(data: WhiteboardData) { var path = UIBezierPath() for line in data.lines { let subPath = UIBezierPath() for (n, point) in line.pts.enumerated() { /// Construct your subPath … } path.append(subPath) } let shapeLayer = CAShapeLayer() shapeLayer.path = path.cgPath … } ,但是要编写它们,您应该使用UIBezierPath方法将它们转换为(String, String),并且不要忘记调用StringtoString()写完后:

flush()

更新的解决方案(基于输入数据)

close()

对于基于您输入的数据(带有一些错误数据)的样本输入:

matched.foreach(line => {
  writer.write(line.toString)
  writer.newLine()
})
writer.flush()
writer.close()

输出将是:

import java.io.{BufferedWriter, File, FileWriter}
import java.time.LocalDate

import scala.util.{Failure, Success, Try}

val inputData = scala.io.Source.fromFile("/C:/Users/rrrrrr/Desktop/datastage/inputdata.txt")
val lines = inputData.getLines

val boundDate = LocalDate.parse("2018-01-01")

val matched = lines.filter(line =>
  Try(LocalDate.parse(line.substring(5, 15))) match {
    case Success(date) => date.isAfter(boundDate)
    case Failure(_) => false
  })

val outputData = new File("/C:/Users/rkumar0/Desktop/folder/output_N4.txt")

val writer= new BufferedWriter(new FileWriter(outputData))

matched.foreach(line => {
  writer.write(line)
  writer.newLine()
})
writer.flush()
writer.close()

更新的解决方案2 (用于比较前5个字节):

094482017-06- 022 14012 000 0 30000A 002290{

094482017-326asd- 022 14012 000 0 30000A 002290{

094482017-06-10 022 14012 000 0 30000A 002290{

094482017-12-01 022 14012 000 0 30000A 002290{

094482018-07-10 022 14012 000 0 30000A 002290{

059412018-07-10 022 14013 000 0 30000A 002290{

015172018-07-10 046 17502 000 0 30000A 001699I

015172018-10-25 046 17502 000 0 30000A 001699I