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 955787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:26:26+00:00 2026-05-16T00:26:26+00:00

The getLines() method of scala.io.Source strips \r and \n from the lines it returns.

  • 0

The getLines() method of scala.io.Source strips \r and \n from the lines it returns. I instead want to keep those characters in the returned Strings.

getLines’ comment says:

returns lines (NOT including newline character(s)) […] if you need more refined behavior you can subclass Source#LineIterator

I’ve tried to subclass LineIterator, but I’m having trouble (and possibly plain misunderstanding!).

I want something like:

class FullLineIterator() extends LineIterator {   
    // Don't strip \r?\n
    override def getc() = iter.hasNext && {
        val ch = iter.next
        if (ch == '\n') { 
            sb append ch
            false
        } else if (ch == '\r') {            
            sb append ch

            if (iter.hasNext && iter.head == '\n') {
                iter.next
                sb append iter.head
            }

            false
        } else {
            sb append ch
            true
        }
    }
}

But I hit a couple of major issues:
1) Type LineIterator is not found, even though I have import scala.io.Source._
2) I’m fairly sure that I can’t access sb, since it’s private[this] inside LineIterator.

Has anyone ever wanted to do this? It’s a fairly strange thing to want to do, but I do want to do it :).

I’ve peeked at scalax and have googled for a fair while, with no luck.

Any suggestions will be gratefully received. Thanks.

  • 1 1 Answer
  • 1 View
  • 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-05-16T00:26:27+00:00Added an answer on May 16, 2026 at 12:26 am

    Since you probably subclass scala.io.Source itself why not override getLines() method and return your class not overriding LineIterator, but providing exactly the functionality you need? The comment is misleding however, no doubt.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to read the source code from a website 100 lines at a
I really like the for (line <- Source fromFile inputPath getLines) {doSomething line} construction
I'm using std::getline() to read lines from an std::istream-derived class, how can I move
I'm using Scala Source.fromFile however I can't seem to find a nice way of
So I'm trying to figure out how to implement a method of selecting lines
I have converted this simple method from C# to C++. It reads a path
I am looking for some method in vim-script json.dumps(join(getlines(1,'$'),'\n')) just like python's json module
This one liner... Console.println(io.Source.fromFile(names.txt).getLines.mkString.split(,).map{x:String => x.slice(1, x.length -1)}.sortBy { x => x}.zipWithIndex.map{t =>{ (t._2
C# coder just wrote this simple C++ method to get text from a file:
I am getting data from a device that returns flow rate (double) and time

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.