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 2 of 2

Thread: JAVA code for deleting spaces from Text file

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JAVA code for deleting spaces from Text file

    Hi,
    I am trying to write a JAVAcode for deleting spaces from Text file.I have a following scenario.
    I have a text file like

    STARTBUR001 20120416
    20120416MES201667 20120320000000000201203210000000002012032200000000 02012032300000000020120324000000000201203260000000 00201203270000000002012032800000000020120329000000 00020120330000000000


    20120416MES202566 20120305000000000201203060000000002012030700000000 02012030800000000020120309000000000201203100000000 00201203110000000002012031200000000020120313000000 0002012031400000000020


    20120416MES275921 20120305000000000201203060000000002012030700000000 02012030800000000020120309000000000201203100000000 00201203110000000002012031200000000020120313000000 00020120314000000000

    END 0000000202

    Here all lines are single lines.
    But what i want is like

    STARTBUR001 20120416
    20120416MES201667 20120320000000000201203210000000002012032200000000 02012032300000000020120324000000000201203260000000 00201203270000000002012032800000000020120329000000 00020120330000000000
    20120416MES202566 20120305000000000201203060000000002012030700000000 02012030800000000020120309000000000201203100000000 00201203110000000002012031200000000020120313000000 0002012031400000000020
    20120416MES275921 20120305000000000201203060000000002012030700000000 02012030800000000020120309000000000201203100000000 00201203110000000002012031200000000020120313000000 00020120314000000000
    END 0000000202

    So in all i want to start checking from second line till i encounter END and delete all spaces at the end of each line.

    Can someone guide me for writing this code??


  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: JAVA code for deleting spaces from Text file

    Copy and edit the input file to a new file as follows:
    begin loop
    Read a line into a String and use the String class's method to replace the spaces in the String with an empty String: ""
    Write the changed line out to the new file.
    end loop
    Delete the input, rename the new file.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Splitting a txt file with multiple spaces
    By macko in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: November 1st, 2011, 01:03 PM
  2. Object and text file stream code problmes
    By Kakashi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 3rd, 2011, 04:34 PM
  3. renaming/deleting file
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 15
    Last Post: January 15th, 2011, 12:18 AM
  4. [SOLVED] Can someone verify if this code for deleting a BST works?
    By scottb80 in forum Java Theory & Questions
    Replies: 2
    Last Post: November 2nd, 2010, 10:19 AM
  5. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM