-1

I have a file, e.g., like this:

// file_1.txt
10 2 3
20 5 6
30 8 9

I need to write a letter with a space before each line that meets a criterion regarding the first value / number in the line, e.g., if I give the value 20 then the file should look like this:

// file_1.txt
10 2 3
c 20 5 6
30 8 9

How I can achieve this in Scala?

This is what I am trying, till now:

import java.io._
import scala.io.Source

object Example_01_IO {

  val s = Source.fromFile("example_01_txt")

  val source = s.getLines()
  val destination = new PrintWriter(new File("des_example_01.txt"))
  val toComment = Array(-10, 20, -30)

  def main(args: Array[String]): Unit = {


    for (line <- source) {
      //if(line_begins_with_any_value_from_toComments_then_write_a_"c"_infront_of_that_line){
        println(line)
        destination.write("c" + line)
        destination.write("\n")
      //}

    }

    s.close()
    destination.close()

  }
}

I can write into another file, let's say, but I need to write in the same file, and only when a line meets a such condition.

I would appreciate any help.

Avah
  • 227
  • 3
  • 13
  • What problems did you run into when you implemented it yourself? – Dima Jan 27 '17 at 12:05
  • This is off-topic for SO. You are likely to have more luck on sites like http://freelancer.com – Dima Jan 27 '17 at 12:06
  • I made an edit (see up) on what I am trying. I wanted to keep the question simple for anyone that can help (and not to get offended. @Dima). – Avah Jan 27 '17 at 12:27

1 Answers1

0

Starting from what you have, all you really need to add is a way to check whether the current line starts with a number that is in your Array.

One way to do that is split the line on every space so you get a list of all the numbers on that line. Then take only the first of that list and convert it to an Int. Then you can simply check whether that Int is present in your Array of allowed numbers.

import java.io._
import scala.io.Source

object Example_01_IO {

  val s = Source.fromFile("example_01_txt")

  val source = s.getLines()
  val destination = new PrintWriter(new File("des_example_01.txt"))
  val toComment = Array(-10, 20, -30)

  def main(args: Array[String]): Unit = {
    def startsWithOneOf(line: String, list: Array[Int]) = 
      list.contains(line.split(" ").head.toInt)

    for (line <- source) {
      val lineOut = if (startsWithOneOf(line, toComment)) s"c $line" else line
      destination.write(lineOut)
      destination.write("\n")
    }

    s.close()
    destination.close()

  }
}
Jasper-M
  • 14,966
  • 2
  • 26
  • 37
  • In my example I was using 2 different files (to read from: `example_01_txt`, and to write to: `des_example_01_txt`). I tried your solution, by trying to write in the same file, _but_ when I use class `PrintWriter` the content of the file is replaced (meaning that, lines which are not satisfying the condition are removed), and when I am using `FileWriter(file, true)` the lines that fulfill the condition are appended. Is there a way to deal with this? Thanks a lot, btw. – Avah Jan 27 '17 at 14:46
  • I see. I wanted to avoid having extra files and maintaining them. Thank you, a lot! – Avah Jan 27 '17 at 15:03
  • @user4712458 I don't think it's possible to only replace certain lines. I think writing to files comes in 2 modes: a) append to file or b) write new file (possibly overwriting an existing file). So if you want to change a line in a file you'll have to write the entire file, including the unchanged lines. [There are ways to modify arbitrary bytes in files](https://docs.oracle.com/javase/7/docs/api/java/io/RandomAccessFile.html), but that still doesn't allow you to insert new bytes in the middle without overwriting the existing bytes. – Jasper-M Jan 27 '17 at 15:03
  • Ah, thanks for telling me about this opportunity! I didn't know about that, despite that, I think I am going to write in new files. – Avah Jan 27 '17 at 15:07