Page 1 of 2 12 LastLast
Results 1 to 20 of 29
  1. #1
    Hemanth Kumar is offline Member
    Join Date
    Feb 2012
    Location
    Bangalore
    Posts
    32
    Rep Power
    0

    Question how to delete a line from a file in java

    I have a file with.xcvs extension.. CVS extension is meant for canvas drawing. i want to delete the second line in this file.. can anyone help me out in this..

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: how to delete a line from a file in java

    Two ways:
    Copy the old file to a new file and skip over the line you don't want in the new file. Delete and rename the files.
    Use the RandomAccessFile class to find the start of the second line and then the start of the third line. Copy the rest of the file from the third line over the top of the second line.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Hemanth Kumar is offline Member
    Join Date
    Feb 2012
    Location
    Bangalore
    Posts
    32
    Rep Power
    0

    Default Re: how to delete a line from a file in java

    Ok does it make any difference if i have a file in .xcvs extension... Or the file should be saved in .txt only for using RandomAccessFile class.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: how to delete a line from a file in java

    The extension should not make a difference. A problem could be whether the java program can determine where lines begin and end. The internal format of the file could make it hard to remove part of the file. You would need to understand the internal format of the file to be sure it can be changed by a java program.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Hemanth Kumar is offline Member
    Join Date
    Feb 2012
    Location
    Bangalore
    Posts
    32
    Rep Power
    0

    Default Re: how to delete a line from a file in java

    <?xml version= "1.0"?>
    <?xml-stylesheet type="text/xsl" href="../Styles/xcvs.xsl"?>
    <canvas producerVersion="7.1.0.0">
    <properties>
    <culture_Info>
    <supported_Cultures>
    <language>en-US</language>
    </supported_Cultures>
    <defaultCulture><![CDATA[en-US]]></defaultCulture>
    <authoredCulture><![CDATA[en-US]]></authoredCulture>
    </culture_Info>
    <font>
    <style>
    <italic>false</italic>
    <underline>false</underline>
    <bold>false</bold>
    </style>
    <point_size>9</point_size>
    <name><![CDATA[Times New Roman]]></name>
    </font>
    <colors>
    <background_color>
    <gradient_angle>-1</gradient_angle>
    <end_color>#ffffff</end_color>
    <start_color>#ffffff</start_color>
    </background_color>
    <foreground_color>#000000</foreground_color>
    <text_color>#000000</text_color>
    </colors>
    <complexity>
    <complexityscore>0.0</complexityscore>
    <calculationscore>0.0</calculationscore>
    <datascore>0.0</datascore>
    </complexity>
    <notifications>
    <play_sound>0</play_sound>
    </notifications>
    <canvasResolutionY>864</canvasResolutionY>
    <canvasResolutionX>1152</canvasResolutionX>
    <canvas_os><![CDATA[999,631]]></canvas_os>
    <force_table_join>1</force_table_join>
    <canvas_size>999,631</canvas_size>
    <version>5.100000</version>
    <canvas_title><![CDATA[6f8432f9-845c-4ca3-874d-66f191f76448]]></canvas_title>
    <default_data_source_name><![CDATA[cc]]></default_data_source_name>
    </properties>
    <items>
    <line id="1">
    <colors>
    <border_color>#000000</border_color>
    <foreground_color>#000000</foreground_color>
    </colors>
    <location>
    <topisleft>true</topisleft>
    <height>0</height>
    <width>202</width>
    <top>209</top>
    <left>230</left>
    </location>
    <border_width>1</border_width>
    </line>
    </items>
    <data_sources>
    <data_source>
    <type_pd><![CDATA[]]></type_pd>
    <type><![CDATA[]]></type>
    <calc_pd><![CDATA[]]></calc_pd>
    <calc><![CDATA[]]></calc>
    <field><![CDATA[]]></field>
    <name><![CDATA[]]></name>
    <Schemaname><![CDATA[CallCenterSchema]]></Schemaname>
    <Schema><![CDATA[1.9]]></Schema>
    <backup_port_number>0</backup_port_number>
    <backup_ip_address><![CDATA[0]]></backup_ip_address>
    <primary_port_number>8000</primary_port_number>
    <primary_ip_address><![CDATA[192.168.228.33]]></primary_ip_address>
    <data_source_name><![CDATA[cc]]></data_source_name>
    </data_source>
    </data_sources>
    <resources>
    <culture id="en-US">
    <culturelabel>
    <value>Canvas1</value>
    <id>6f8432f9-845c-4ca3-874d-66f191f76448</id>
    </culturelabel>
    </culture>
    </resources>
    <calculations />
    </canvas>


    This is the content of the file which i have now.. i want to delete the second line of this file and then change this file to .xml extension from which i can traverse to all the child nodes of it and get the height tags details which is to be captured.. If i am trying to read the file line by line and print on the console its giving junk characters.. so facing problem in this...

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: how to delete a line from a file in java

    Is this the second line in the file:
    <?xml-stylesheet type="text/xsl" href="../Styles/xcvs.xsl"?>

    If not please explain What defines the beginning and the ending of a line?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Hemanth Kumar is offline Member
    Join Date
    Feb 2012
    Location
    Bangalore
    Posts
    32
    Rep Power
    0

    Default Re: how to delete a line from a file in java

    yes
    <?xml-stylesheet type="text/xsl" href="../Styles/xcvs.xsl"?>
    This is the second line of the file

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: how to delete a line from a file in java

    The contents of the file looks like text (ie no special binary characters or control characters in it) so you should not have any problems using either of the methods I suggested.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Hemanth Kumar is offline Member
    Join Date
    Feb 2012
    Location
    Bangalore
    Posts
    32
    Rep Power
    0

    Default Re: how to delete a line from a file in java

    can u just give me the code for it. if my file name is saved as dline.txt then how to traverse to the second line of the file using RandomAccessFile class. Previously the file extension was.xcvs which is nothing but canvas drawing. i have converted to .txt.. SO while reading this file line by line i had problems where it was displaying junk characters. Can u help me on this. Thanks in advance

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: how to delete a line from a file in java

    If you use the first scheme, it will be easier to write. It will read the file line by line and write all the lines to the output file except for the line you want to skip. Close the files when done copying them. Delete the input file and rename the output file.

    If you want someone to write the code for you, hire a programmer.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    Hemanth Kumar is offline Member
    Join Date
    Feb 2012
    Location
    Bangalore
    Posts
    32
    Rep Power
    0

    Default Re: how to delete a line from a file in java

    So then RAndomAccessFIle class is not required for this problem.? Is that what u mean? Thanks for ur instant Reply....

  12. #12
    Hemanth Kumar is offline Member
    Join Date
    Feb 2012
    Location
    Bangalore
    Posts
    32
    Rep Power
    0

    Default Re: how to delete a line from a file in java

    I will write a code not a problem for me.. Is this can be done without using a RandomAccessFIle class. ? Please clarify this to me...?

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: how to delete a line from a file in java

    I gave two different ways to do it. The first did not use the RandomAccessFile class.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    Hemanth Kumar is offline Member
    Join Date
    Feb 2012
    Location
    Bangalore
    Posts
    32
    Rep Power
    0

    Default Re: how to delete a line from a file in java

    try{
    LineNumberReader lnr = new LineNumberReader(new FileReader(new File("D:\\Canvas\\dline.txt")));
    lnr.skip(Long.MAX_VALUE);
    System.out.println("no of lines in file: " +lnr.getLineNumber());
    try{
    FileInputStream fstream = new FileInputStream("D:\\Canvas\\dline.xcvs");
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    while ((strLine = br.readLine().trim()) != null)
    {
    System.out.println ("strLine"+strLine.trim());
    }
    in.close();
    }
    catch (Exception e)
    {
    System.err.println("Error: " + e.getMessage());
    }

    }
    catch(Exception e){
    e.printStackTrace();
    }




    The above is the code written for the reading the file line by line.. BUt it displays junk characters in between the lines in the console window..

  15. #15
    Hemanth Kumar is offline Member
    Join Date
    Feb 2012
    Location
    Bangalore
    Posts
    32
    Rep Power
    0

    Default Re: how to delete a line from a file in java

    i am not able to understand anything from your reply..
    Last edited by DarrylBurke; 04-10-2012 at 12:27 PM. Reason: Corrected subject line

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: how to delete a line from a file in java

    Please post what the program prints out when it is executed. It looks like it reads the lines of the file and prints them on the screen.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    Hemanth Kumar is offline Member
    Join Date
    Feb 2012
    Location
    Bangalore
    Posts
    32
    Rep Power
    0

    Default Re: how to delete a line from a file in java

    There is a junk character displayed after each letter printed from the file... i am not able to copy that result as well. if i run the same thing outside eclipse the result will be like
    < ? x m l v e r s i o n = " 1 . 0 " ? > and so on for each line
    But in eclipse there is a junk character after each letter printed on the console

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: how to delete a line from a file in java

    What is the internal format of the file? Is it a unicode file? That means every character is using 2 bytes. For ASCII text one of the bytes will be 0 which will look like a junk character.
    Look at the file with a hex editor and see what the contents is.

    You will have to tell the classes that are reading the file what the format of the data is so it can convert the 2 bytes to a single character.
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    Hemanth Kumar is offline Member
    Join Date
    Feb 2012
    Location
    Bangalore
    Posts
    32
    Rep Power
    0

    Default Re: how to delete a line from a file in java

    Yes it is a unicode file..

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: how to delete a line from a file in java

    Then you need to tell the InputStreamReader class (in its constructor) about it so it can correctly create characters when it reads the file.
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. [SOLVED] Delete Current line from file
    By Azndaddy in forum New To Java
    Replies: 2
    Last Post: 04-06-2012, 08:00 AM
  2. Java- Writing a file and reading a file line by line
    By Nazneen Ali in forum New To Java
    Replies: 7
    Last Post: 07-20-2011, 07:56 AM
  3. Trigger cmd to delete a file in java program
    By hari.kr in forum Advanced Java
    Replies: 4
    Last Post: 04-28-2010, 07:37 PM
  4. delete a line from a text file in Java
    By ddatta8 in forum New To Java
    Replies: 2
    Last Post: 01-24-2009, 05:08 AM
  5. Replies: 1
    Last Post: 11-10-2008, 09:49 AM

Posting Permissions

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