Results 1 to 10 of 10

Thread: Newlines

  1. #1
    Join Date
    Aug 2014
    Location
    South Africa
    Posts
    19
    Rep Power
    0

    Default Newlines

    I have the following problem:

    I need to read a text file using BufferedReader. Apply some method on the read data and finally write the results to another with a line separator between each line.

    The problem is that I need to determine the line separator ( i.e.. "\n" , "\r\n ,"\r") from the input file. The file is often not native to the OS, and this renders System.gePropertry("Line Separator") useless.

    I also need the checking of the line seperator to have no affect on the BufferedReader reads the file.


    I have no idea on what the correct approach is. I welcome any and all suggestions

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Newlines

    1) open the file
    2) read characters until you read either \r or \n
    3) if it is \r, read one more character
    4) based on last two characters read, determine the newline

    That does not help for files which mix and mash newlines. But I consider those files to be garbage in.

    I also need the checking of the line seperator to have no affect on the BufferedReader reads the file.
    It doesn't.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Newlines

    First 'sample' the file, i.e. start reading it using an InputStream; if it can read a \n it is probly a Unix file, if it reads a \r first it has to read the next byte; if it is a \n the file is probably a MS Windows file, otherwise it is probably a Mac file; at the end close the InputStream and do what you have to do with the file ... A BufferedReader is 'immune' to different EOL formats, so you can read it as always.

    kind regards,

    Jos

    edit: this slow old sod shouldn't reply on Monday mornings, but stick to his espresso and tobacco instead ...
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Newlines

    a) A least we agree on something
    b) it never hurts to have a second opinion
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Newlines

    Quote Originally Posted by gimbal2 View Post
    a) A least we agree on something
    b) it never hurts to have a second opinion
    Shh, I'm waiting for my old espresso machine (it makes noises as if it were a pneumatic hammer but it's slow as molasses; in the mean time I'm rolling my cigarette).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Newlines

    *tip-toes away*
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    Join Date
    Aug 2014
    Location
    South Africa
    Posts
    19
    Rep Power
    0

    Default Re: Newlines

    Java Code:
        private static String getLineSeparator(String fileName) throws IOException {
           BufferedReader bufferedReader  = new BufferedReader(new FileReader(fileName));
            int b;
            char c = 0;
            while ((b =bufferedReader.read()) !=-1){
                c = (char) b;
                System.out.println(c);
                if((c == CR )|| (c == LF))  break;
            }
             if(( c == LF )){
                return  Character.toString(LF);
    
             }
             else  if (c ==  CR){
                    c  = (char)bufferedReader.read();
                    if ( c == LF){
                       return  ""+CR+LF;
    
                    }
                 else  {
                        return Character.toString(CR);
                    }
    
             }
            return System.getProperty(" line.separator ");
        }
    
    
    }
    This my current attempt, however this does not seem to work.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Newlines

    For one thing, your property string should not contain white space. But it also appears the only time
    your return that is when an EOF is encountered.

    Regards,
    Jim
    Last edited by jim829; 09-15-2014 at 06:55 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Newlines

    Quote Originally Posted by Matthew John Dunk View Post
    This my current attempt, however this does not seem to work.
    What does it not do?
    Have you checked the values returned for various inputs and checked they are what you expect?
    Have you debugged to check that the correct route is being taken through the method for different inputs?

    Quote Originally Posted by jim829 View Post
    For one thing, your property string should not contain white space. But it also appears the only time
    your return that is when an EOF is encountered.

    Regards,
    Jim
    Isn't that correct (not the spaces part, but the returning the system separator)?
    I would guess that if you hit the EOF without any form of newline then you may as well default to the system. Not as if it's going to be used in any case.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Newlines

    You're probably right. I didn't give it enough thought.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 3
    Last Post: 08-20-2009, 03:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •