Results 1 to 14 of 14
  1. #1
    alon2580 is offline Member
    Join Date
    Aug 2008
    Posts
    7
    Rep Power
    0

    Default remove a portion from a file

    Hi,
    I have a text file and I looking for a way to delete all the end of the file, meaning delete a portion from a token I can provide until the end of the file.
    For Example, if my file is:

    Today is Sunday
    Tomorrow is Monday
    delete from here
    the hour is XXXX
    goodbye.

    and I want to delete all the lines from:delete from here including delete from here(which I always know that exist)

    Does anyone knows on a good way to do it?
    Thanks

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

    Default

    I think there may be a truncate method that you can use to trim off the end of a file. Look at the API doc's Index under t

  3. #3
    alon2580 is offline Member
    Join Date
    Aug 2008
    Posts
    7
    Rep Power
    0

    Default

    Thanks for the reply but I can't find anything like that.
    Any other ideas?

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

    Default

    What version of java do you have? I see truncate in the 1.4 version.

  5. #5
    alon2580 is offline Member
    Join Date
    Aug 2008
    Posts
    7
    Rep Power
    0

    Default

    I'm using java 1.5
    can you please give more details please,I probably looked in the wrong place
    Last edited by alon2580; 08-24-2008 at 03:17 PM. Reason: fixed typo

  6. #6
    alon2580 is offline Member
    Join Date
    Aug 2008
    Posts
    7
    Rep Power
    0

    Default

    o.k I found what you referred to and it's not what I was looking for.
    Any other suggestions maybe?

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

    Default

    not what I was looking for
    What are you looking for?
    Another solution is to copy the parts of the file you want, delete the original, rename the copy.

  8. #8
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Read the file into a StringBuilder until you get to the "delete from here" line. Write the string in the StringBuilder back to the file.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Here is another way to do that.

    Java Code:
        public static void main(String[] args) {
            BufferedReader inputFile = null;
            PrintWriter outputFile = null;
            
            try {
                inputFile = new BufferedReader(new FileReader("files/inputFile.txt"));
                outputFile = new PrintWriter("files/outputFile.txt");
    
                String lineread;
    
                // Loop through and read each line
                while (!(lineread = inputFile.readLine()).equals("delete from here")) {
                    outputFile.println(lineread);
                }
            }
            catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } 
            catch (IOException ex) {
                ex.printStackTrace();
            }
            finally {
                try {
                    inputFile.close();
                    outputFile.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }

  10. #10
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    I'm boggled at the OP's need to ask for code. Being able to read and write files, and to change the contents of a file is a fundamental skill of any programmer. Its typically the third or so program assignment in any class, not much more difficult than Hello World.

    Is this just a trivial homework assignment, the kind we should *not* be giving code for.

    How can you learn without doing. As Yoda says, there is no try, only do or not do.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes, that's true pal. As you said I never give a code to anyone without any attempt. But I give this just to help him, because I thought to post a code after a long time. So I choose this thread. Because I want to keep-alive with a code as well. If you can, but I don't think so, check my few past replays. I never give a code without any help.

    I hope you have an idea what I'm talking about.

  12. #12
    alon2580 is offline Member
    Join Date
    Aug 2008
    Posts
    7
    Rep Power
    0

    Default

    Thanks Eranga,
    I agree with both of you. but this is also funny cause yesterday after I didn't find a specific function that does what I want(that what I was looking for, Norm)I decided to go on the old fashion way exactly as the way you posted Eranga.
    So of course I know to write a code that trivial but thanks a lot for all of your help everybody.

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    It's ok, I've help you to solve the question. But what fishtoprecords is says 100% correct. Pay attention on that in grate depth.

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

    Default

    ANother method to change the length of a file is setLength().
    Did you look at that?

Similar Threads

  1. [SOLVED] Remove All Line from File
    By Mir in forum New To Java
    Replies: 41
    Last Post: 07-17-2008, 09:44 AM
  2. Remove duplicate lines from a text file
    By Dirt.Diver in forum New To Java
    Replies: 15
    Last Post: 06-25-2008, 02:08 PM
  3. Replies: 0
    Last Post: 01-20-2008, 06:07 AM
  4. How to remove Control Characters from an input file?
    By renjan in forum Advanced Java
    Replies: 0
    Last Post: 08-01-2007, 03:33 PM
  5. how to remove an old version of JDK
    By tommy in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 08:59 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
  •