Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By Hibernate

Thread: Delete last character of a file

  1. #1
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

    Default Delete last character of a file

    Hi,
    I must delete the last character of a text file BUT if the last character is a newline character or a carriage return, I WANT TO DELETE THE NEWLINE CHARACTER.

    I am using readLine() and a BufferedReader but I believe readLine() doesn't read newlines?

    Let me know how I can do this.

    Thanks

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    You cannot just edit a file. What you need to do is read the original file in line by line and write them back out to another temp file. After each line you would write a new line/carriage return except for the last line. When you have finished rename the temp file to the original file.

  3. #3
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by PrinceSendai View Post
    Hi,
    I must delete the last character of a text file BUT if the last character is a newline character or a carriage return, I WANT TO DELETE THE NEWLINE CHARACTER.

    I am using readLine() and a BufferedReader but I believe readLine() doesn't read newlines?

    Let me know how I can do this.

    Thanks
    The readLine() method read a line of a file up to one of the following:
    "\n" - line feed
    "\r" - carriage return
    "\r\n" - carriage return followed by a line feed

    The BufferedReader does contain a read() method that reads a single character and returns an int.

    ACSII int for the following:
    13 - carriage return
    10 - line feed

    Hope this helps.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

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

    Default

    To make your code more readable you can cast the char to int. For example: (int)'\n' vs using 10
    Java Code:
          System.out.println("char \\n=" + (int)'\n' + " \\r=" + (int)'\r');  // char \n=10 \r=13

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Hope this helps
    How does that help? Advising someone to read a file character by character is bad advice (yet again). Considering that I had already provided a good reply, why come along only to post a bad reply?

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

    Default

    Could reading lines and writing lines change the contents of the file? There are different mixes of lineend carriage return. Would they be preserved?

    I must delete the last character of a text file BUT if the last character is a newline character or a carriage return, I WANT TO DELETE THE NEWLINE CHARACTER.
    Are these instructions ambiguous. First it says to always delete the last character. Then it says if the last char is a CR or a NL delete the NL. Does that mean to leave the CR if it is the last char?

  7. #7
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    I'm not sure what you mean with that you want to delete the newline character (and do you mean line feed character ['\n']?).
    Do you want to remove the character from the read data or from the file and change the file?

    For the last alternative:

    Use RandomAccessFile: RandomAccessFile (Java 2 Platform SE 5.0)
    Open the file in "rw" mode (enables both reading and writing).
    Get length and store it in a variable, e.g. ł.
    If the length (ł) ≤ 1: Truncate file the size 0: file.setLength(0)
    Else:
    seek to ł - x, read x bytes from file (file.read(byteArrayOfLengthX)), where X is how many characters you want need to read at the end of the file.
    And if you want to delete the last x characters truncate the file: file.setLength(ł - x).
    If you need to as something to the end the file (after truncation) use: file.write("your character(s)".getBytes("UTF-8")), do not use e.g. writeChar because it does not write normal characters (it is used for serialization).

    When done flush and close the file.
    Norm likes this.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  8. #8
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    You cannot just edit a file. What you need to do is read the original file in line by line and write them back out to another temp file. After each line you would write a new line/carriage return except for the last line. When you have finished rename the temp file to the original file.
    If this is want you want to do, use my instructions and replace the x with 2, and if byteArrayOfLengthX[0] is neither 10 or 13 (\n or \r) then the text with want to append to the file is that characters,
    which is easiest done using file.write(byteArrayOfLengthX[0]).

    Junky's method does the same, but is slower (which you should not notice exception for if the file is huge or if the device it is stored in is slow) but simpler. Use it if you do not what to use RandomAccessFile (due to complexity, or some other reason).
    Last edited by Hibernate; 08-09-2011 at 07:49 AM.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  9. #9
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    How does that help? Advising someone to read a file character by character is bad advice (yet again). Considering that I had already provided a good reply, why come along only to post a bad reply?
    For someone ignoring my replies to a thread you certainly seem to be reading them.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

Similar Threads

  1. Replies: 2
    Last Post: 05-26-2011, 05:38 AM
  2. Can't Delete A File
    By blazecode in forum Threads and Synchronization
    Replies: 4
    Last Post: 03-07-2011, 10:16 AM
  3. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 04:01 PM
  4. Delete From .txt file
    By Sarinam in forum New To Java
    Replies: 86
    Last Post: 06-28-2008, 10:17 AM
  5. How to delete a file
    By Alpha in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 08:11 AM

Tags for this Thread

Posting Permissions

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