Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Removing Line feeds or carriage returns

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Removing Line feeds or carriage returns

    Hi All,

    I have a requirement where I need to read a file with so many carriage return line feeds.
    Attached is the file I am reading.

    I have written a sample application to read the file and and remove these new line feeds but when I execute it, I see the line feed.
    import java.io.*;
     
    public class Main {
     
        public static void main(String[] args) throws IOException{
     
     
            BufferedReader br = null;
            PrintWriter sb = null;
     
            try{
                br = new BufferedReader(new FileReader("C:/Users/rejalu/Downloads/ATG/SubmittedOrders/2010/testfile.txt"));
                sb = new PrintWriter(new FileWriter("C:/Users/rejalu/Downloads/ATG/SubmittedOrders/2010/testfile3.txt"));
     
                String I;
     
                while((I = br.readLine())!= null){
                    sb.println(I.replaceAll("[\\r|\\n]+", " "));
                }
            }
            finally
            {
                   if (br!=null)
                   {
                       br.close();
                   }
                if (sb != null){
                    sb.close();
                }
             }
     
     
            }
        }

    Is there away to achieve this in attached output file?

    This is just one record, but I have several records like these.

    Thanks for your help.

    Ron
    Attached Files Attached Files


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Removing Line feeds or carriage returns

    What do you see when you print the variable: I? (??? Why the name I vs line or record?)
    The readLine() method does not return the endline character.
    The println() method adds an endline character.

    What are you trying to achieve?
    Try using the print() method to write the file.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Removing Line feeds or carriage returns

    I am trying to remove new line feeds from the file.
    The first record in the file are the column headers,

    The next line is the data record separated by commas.

    I am supposed to load these files in a postgress database but I cannot load them when they have carriage return line feeds.

    Also you recommended me to use print() but it does not do the job since it just appends the data record at the end of the column headers.
    Last edited by Ron256; June 11th, 2014 at 10:32 PM.

  4. #4
    Junior Member
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Removing Line feeds or carriage returns

    This is what I see when I print the variable I

    Screenshotresult.jpg

    It is basically the same input file record I see being printed.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Removing Line feeds or carriage returns

    Create a simpler data file and work on that, something like:

    Text file:
    this is
    is the
    the file
    file
    to
    work
    on

    Read each line and combine the next with the previous without the linefeeds so that the result should be:

    this is is the the file file to work on

    Show us that code and let us know what problems you're having with it. The code that successfully does this simpler example SHOULD work on the complex problem, though there may be complexities in the data file you haven't made clear.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Removing Line feeds or carriage returns

    print() but it does not do the job since it just appends the data record at the end of the column headers.
    That is what the lines of the file will look like if the endline characters are removed.
    Either there are endline chars to separate lines
    or all the data will be on one line when the endline chars are removed.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Read RSS feeds and add them to JFrame
    By nickthegreek in forum Java Theory & Questions
    Replies: 1
    Last Post: May 12th, 2013, 03:59 PM
  2. Curious about newline and carriage return
    By IHeartProgramming in forum Java Theory & Questions
    Replies: 4
    Last Post: November 25th, 2012, 08:13 PM
  3. Replies: 10
    Last Post: September 16th, 2011, 07:49 PM
  4. live feeds and jtables
    By petem86 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 19th, 2010, 08:44 PM
  5. Implementing RSS Feeds
    By madhu_sushmi in forum Java Networking
    Replies: 0
    Last Post: April 1st, 2010, 07:57 PM