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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:56:28+00:00 2026-06-06T11:56:28+00:00

My Question: How do I force an input stream to process line separators as

  • 0

My Question: How do I force an input stream to process line separators as the system standard line separator?

I read a file to a string and the newlines get converted to \n but my System.getProperty("line.separator"); is \r\n. I want this to be portable, so I want my file reader to read the newlines as the system standard newline character (whatever that may be). How can I force it? Here are my methods from the Java Helper Library to read the file in as a string.

/**
* Takes the file and returns it in a string. Uses UTF-8 encoding
*
* @param fileLocation
* @return the file in String form
* @throws IOException when trying to read from the file
*/
public static String fileToString(String fileLocation) throws IOException {
  InputStreamReader streamReader = new InputStreamReader(new FileInputStream(fileLocation), "UTF-8");
  return readerToString(streamReader);
}

/**
* Returns all the lines in the Reader's stream as a String
*
* @param reader
* @return
* @throws IOException when trying to read from the file
*/
public static String readerToString(Reader reader) throws IOException {
  StringWriter stringWriter = new StringWriter();
  char[] buffer = new char[1024];
  int length;
  while ((length = reader.read(buffer)) > 0) {
    stringWriter.write(buffer, 0, length);
  }
  reader.close();
  stringWriter.close();
  return stringWriter.toString();
}
  • 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-06T11:56:29+00:00Added an answer on June 6, 2026 at 11:56 am

    Your readerToString method doesn’t do anything to line endings. It simply copies character data – that’s all. It’s entirely unclear how you’re diagnosing the problem, but that code really doesn’t change \n to \r\n. It must be \r\n in the file – which you should look at in a hex editor. What created the file in the first place? You should look there for how any line breaks are represented.

    If you want to read lines, use BufferedReader.readLine() which will cope with \r, \n or \r\n.

    Note that Guava has a lot of helpful methods for reading all the data from readers, as well as splitting a reader into lines etc.

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

Sidebar

Related Questions

After answering a question about how to force-free objects in Java (the guy was
I have a question about fiscal date literals in the Force.com API ( http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_select_dateformats.htm
this may be a very simple question, but is it possible to force a
I am basically trying to get a user to input a hexadecimal input via
Is there a way to force Git to store versions of a file as
By default, the input type="date" shows date as YYYY-MM-DD . The question is, is
How do you force evaluation of the input of a function before benchmarking the
Long story short, my question is: How can I force GnuPG which private/public key
Question: I'm exporting a System.Data.DataTable to XML. So far it works fine. But I
There are lots of questions about how to force the browser to cache or

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.