Page 2 of 2 FirstFirst 12
Results 21 to 29 of 29
  1. #21
    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
    {
    final StringBuilder stringBuilder = new StringBuilder();
    InputStream inStream = new FileInputStream("D:\\Canvas\\dline.txt");
    final InputStreamReader streamReader = new InputStreamReader(inStream, "UTF-8");
    final BufferedReader bufferedReader = new BufferedReader(streamReader);
    String line = "";
    while ((line = bufferedReader.readLine()) != null) {
    System.out.println(line);
    stringBuilder.append(line); }

    }catch(Exception e){
    System.out.println(e);
    }

    BUt still its displaying with junk characters

  2. #22
    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

    if i use UTF-16 its working fine

  3. #23
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

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

    I used "Unicode"
    If you don't understand my response, don't ignore it, ask a question.

  4. #24
    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

    Now from this unicode file i want to delete the second line and save the file with.xml extension. Should i use RandomAccessFile class for this?

  5. #25
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

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

    You could. See post#2
    If you don't understand my response, don't ignore it, ask a question.

  6. #26
    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 Thank u. I am able to do this...

  7. #27
    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
    {
    PrintWriter out = null ;
    out = new PrintWriter (new FileWriter ("D:\\Canvas\\dline.xml"));
    String line = null;
    int lineNum = 0;
    try {
    FileInputStream in = new FileInputStream("D:\\Canvas\\dline.xcvs");
    BufferedReader br1 = new BufferedReader(new InputStreamReader(in,"UTF-16"));
    while( (line =br1.readLine()) != null) {
    lineNum +=1;
    if(lineNum == 2){
    continue;
    }
    out.println(line);
    }
    System.out.println("Line deleted and converted to .xml file");
    }
    catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    out.flush();
    out.close();
    try {
    FileInputStream in = new FileInputStream("D:\\Canvas\\dline.xcvs");
    BufferedReader br1 = new BufferedReader(new InputStreamReader(in,"UTF-16"));
    br1.close();
    }
    catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    catch(Exception e){
    System.out.println(e);
    }

    This is the working code for the above problem..

  8. #28
    Bushman is offline Member
    Join Date
    Apr 2012
    Location
    Spainish TOwn
    Posts
    25
    Rep Power
    0

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

    Quote Originally Posted by Hemanth Kumar View Post
    try
    {
    PrintWriter out = null ;
    out = new PrintWriter (new FileWriter ("D:\\Canvas\\dline.xml"));
    String line = null;
    int lineNum = 0;
    try {
    FileInputStream in = new FileInputStream("D:\\Canvas\\dline.xcvs");
    BufferedReader br1 = new BufferedReader(new InputStreamReader(in,"UTF-16"));
    while( (line =br1.readLine()) != null) {
    lineNum +=1;
    if(lineNum == 2){
    continue;
    }
    out.println(line);
    }
    System.out.println("Line deleted and converted to .xml file");
    }
    catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    out.flush();
    out.close();
    try {
    FileInputStream in = new FileInputStream("D:\\Canvas\\dline.xcvs");
    BufferedReader br1 = new BufferedReader(new InputStreamReader(in,"UTF-16"));
    br1.close();
    }
    catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    catch(Exception e){
    System.out.println(e);
    }

    This is the working code for the above problem..

    Lol an army of complexed replies. These are unescessary.
    Solution.
    Load the contents of the file into an array list.
    Manipulate the array list itself.
    Overwrite the file whenever you wish.

  9. #29
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

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

    What if the file is HUGE?
    If you don't understand my response, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

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
  •