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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:28:00+00:00 2026-05-28T02:28:00+00:00

I am reading a file using the scanner class.I want my EOL delimiter to

  • 0

I am reading a file using the scanner class.I want my EOL delimiter to be CR LF but there are some records in the file which have only LF hence my scanner is reading the LF and going to the next line. I want the scanner to go to the next line only when it encounters CR LF both.
Here is my snippet: (using double slashes in the regex also gives same result)

java.util.Scanner sMain = new java.util.Scanner(inputStream,encoding)
                              .useDelimiter(Pattern.compile("[\r\n]"));
while (sMain.hasNextLine()) {  
// sysout line  
}
  • 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-05-28T02:28:01+00:00Added an answer on May 28, 2026 at 2:28 am

    So you want a delimiter which is

    "\r\n"
    

    "[\r\n]" means one of \r or \n.

    The delimiter determines the separator between “words”. To break up the line you can use split(“~”) as follows.

    ByteArrayInputStream inputStream = new ByteArrayInputStream((
            "a line~with a \r and~a \n in it \r\n" +
            "a line with \n\r in~it\r\n").getBytes());
    
    Scanner sMain = new Scanner(inputStream,"UTF-8").useDelimiter("\r\n");
    Pattern wordPattern = Pattern.compile("~");
    while (sMain.hasNext()) {
        String line = sMain.next();
        line = line.replaceAll("\r", "\\\\r").replaceAll("\n", "\\\\n");
        String[] words = wordPattern.split(line);
        System.out.println(Arrays.toString(words));
    }
    

    prints

    [a line, with a \r and, a \n in it ]
    [a line with \n\r in, it]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have .zip file which contain csv data. I am reading .zip file using
I've tried reading a JPG file using the StreamReader class' ReadToEnd() method which returns
I'm reading a file using bufferedreader, so lets say i have line = br.readLine();
i have implement a small code of reading from a file with scanner and
Hi i am reading csv file and getting date in 01-01-2011 but i want
I am reading a file line by line using Scanner. So my question is
I was trying to open a file for reading. When using: Scanner input =
I'm reading a binary file using perl. In the file's headers, there's 4 bytes
I have xml file with TAG like this: <Question>dzia&amp;#322;owa</Question> I'm reading this file using
I'm reading a local file using a BufferedReader wrapped around a FileReader: BufferedReader reader

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.