scala 将Scala生成的数据写入文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28969738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 06:57:08  来源:igfitidea点击:

Writing data generated in scala to a text file

scalaoutput

提问by ALs

I was hoping somebody could help, I'm new to scala and I'm having some issues writing my output to a text file.

我希望有人能提供帮助,我是 Scala 的新手,我在将输出写入文本文件时遇到了一些问题。

I have a data table and I've written some code to read it in one line at a time, do what I want it to do, and now I need it to write that line to a text file.

我有一个数据表,我已经编写了一些代码来一次一行读取它,做我想要它做的事情,现在我需要它将该行写入文本文件。

So for example, I have the following table of data type

例如,我有以下数据类型表

Name, Date, goX, goY, stopX, stopY

姓名、日期、goX、goY、stopX、stopY

1, 12/01/01, 1166, 2299, 3300, 4477

1、12/01/01、1166、2299、3300、4477

My code, takes the first characters of goX and goY and creates a new number, in this instance 1.2 and does the same for stopX and stopY so in this case you get 3.4

我的代码采用 goX 和 goY 的第一个字符并创建一个新数字,在本例中为 1.2 并对 stopX 和 stopY 执行相同的操作,因此在这种情况下您得到 3.4

What I want to get in the text file is essentially the following:

我想在文本文件中得到的内容基本上如下:

go, stop

走,停下

1.2, 3.4

1.2、3.4

and I want it to go through hundreds of lines doing this until I have a long list of on and off in the text file.

我希望它通过数百行来执行此操作,直到我在文本文件中有一长串打开和关闭列表。

My current code is as follows, this is almost certainly not the most elegant solution but it is my first ever scala/java code:

我当前的代码如下,这几乎肯定不是最优雅的解决方案,但它是我的第一个 scala/java 代码:

import scala.io.Source

object FT2 extends App {
for(line<-Source.fromFile("C://Users//Data.csv").getLines){

var array = line.split(",")
val gox = (array(2));
val xStringGo = gox.toString
val goX =xStringGo.dropRight(1|2)
val goy = (array(3));
val yStringGo = goy.toString
val goY = yStringGo.dropRight(1|2)
val goXY = goX+"."+goY

val stopx = (array(4));
val xStringStop = stopx.toString
val stopX =xStringStop.dropRight(1|2)
val stopy = (array(3));
val yStringStop = stopy.toString
val stopY = yStringStop.dropRight(1|2)
val stopXY = stopX+"."+stopY

val GoStop = List(goXY,stopXY)
//This is where I want to print GoStop to a text file
}

Any help is much appreciated!

任何帮助深表感谢!

回答by dhg

This should do it:

这应该这样做:

import java.io._

val data = List("everything", "you", "want", "to", "write", "to", "the", "file")

val file = "whatever.txt"
val writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)))
for (x <- data) {
  writer.write(x + "\n")  // however you want to format it
}
writer.close()

But you can make it a little nicer by creating a method that will automatically close stuff for you:

但是你可以通过创建一个自动为你关闭东西的方法来让它更好一点:

def using[T <: Closeable, R](resource: T)(block: T => R): R = {
  try { block(resource) }
  finally { resource.close() }
}

using(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)))) { 
  writer =>
    for (x <- data) {
      writer.write(x + "\n")  // however you want to format it
    }
}

So:

所以:

using(new BufferedWriter(new OutputStreamWriter(new FileOutputStream("output.txt")))) { 
  writer =>
    for(line <- io.Source.fromFile("input.txt").getLines) {
      writer.write(line + "\n")  // however you want to format it
    }
}