Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8924103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:19:12+00:00 2026-06-15T07:19:12+00:00

I would like to read a file, find some strings and replace everything that

  • 0

I would like to read a file, find some strings and replace everything that is after the symbol “=” in this line.

Lets say I have a textfile like this:

name=whatever
age=150
id.from.system=10298092_42_42

path=D:\name\somewhere
whatever_A= WHATEVER

Lets say I want to change path. At first I have to find the string “path” and then replace everything after “=” somehow. Any ideas? I know I could easily read the file line by line something like this:

val source = io.Source.fromFile("C:/myfile.txt)
val lines = source.mkString
source.close()

But this is maybe not the best idea, because its not that performant to read the whole file (maybe the file got 10000000 lines, and the string is already at line 2, but my program would read the whole file. That would be unnecessary).

And there is maybe another problem: if Im searching for specific strings, like here for “name” but these strings are there several times. I want to make sure that its only valid is after the string there is an “=”. Maybe I could search always for something with an “=” at the end, that could solve the problem. But I have no idea how to write this in a nice scala code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:19:13+00:00Added an answer on June 15, 2026 at 7:19 am

    If your C:/myfile.txt contains the line path=D:\name\somewhere, you can replace D:\name\somewhere with the following code:

    val lines = fromString("path=D:\\name\\somewhere").getLines // use fromFile here 
    for { in <- lines
          out <- if (in startsWith("path=")) "path=D:\\my\\path" else in
    } yield out
    

    This example will return the string

    path=D:\my\path
    

    You would need to use fromFile to get the lines and write the lines out to a new file.

    Here’s another approach that accomplishes the same thing:

    val lines = fromString("path=D:\\name\\somewhere").getLines
    lines.map(in => if (in startsWith("path=")) "path=D:\\my\\path" else in)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In java i would like to read a file line by line and print
I would like to read a file in R as a table for a
I would like to read a text file and input its contents into an
I would like to read a DICOM file in C#. I don't want to
I would like to read the last 1 megabyte of a MP3 file and
I would like to know how is possible to read a file placed in
I have a .plist file that looks like this: <plist version=1.0> <array> <dict> <key>name</key>
I am trying to write some code that will open a file, read its
I am currently using a code construction like this: string line; using (System.IO.StreamReader file
I have a flat file that is pipe delimited and looks something like this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.