错误:使用替代方法写入重载方法值: [英] error: overloaded method value write with alternatives:

查看:48
本文介绍了错误:使用替代方法写入重载方法值:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to read a fixed width file and writting it as text file ,below is the code . Input file layout length of the fields(4,10,3,5,3,1,1,5,7)

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

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

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

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

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

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>

我的输出文件创建为空.

and my output file created empty.

推荐答案

你的行有 (String, String) 类型,但要写它们,你应该把它们转换成 String 使用 toString() 方法,写完后不要忘记调用 flush()close() :

Your lines have type of (String, String), but to write them, you should convert them to String using toString() method, and don't forget to call flush() and close() after writing:

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()

对于基于您输入的数据的示例输入(有一些错误的数据):

For the sample input based on you input data (with some wrong data):

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

输出将是:

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

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

Updated solution 2 (for comparing first 5 bytes):

val matched = lines.filter(line =>
  Try(line.substring(0, 5).toInt) match {
    case Success(n) => n > 90000
    case Failure(_) => false
  }
)

这篇关于错误:使用替代方法写入重载方法值:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆