Page 1 of 2 12 LastLast
Results 1 to 20 of 23
Like Tree1Likes

Thread: How to write to a file without erasing it?

  1. #1
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default How to write to a file without erasing it?

    Hello all,

    This is probably a pretty simple thing, but I've been having difficulty writing to an existing file without erasing it.

    Lately in Java I've noticed that any attempt write to an existing file seems to wipe it's contents and then Java will write to a file, the only way around this that I've found is by appending the file.



    Is there any way to get Java to write to a file without wiping it first?

    I know there is can be cone in both C and C++, but is this possible in Java too?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: How to write to a file without erasing it?

    Try to read/write the file with a RandomAccessFile object; (read its API documentation first).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How to write to a file without erasing it?

    What is wrong with appending? Basically, Either you add to the end or you replace the old with the new. Or use RandomAccessFile as Jos suggested.

    What does C do that is different?
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default Re: How to write to a file without erasing it?

    Quote Originally Posted by Norm View Post
    What is wrong with appending? Basically, Either you add to the end or you replace the old with the new. Or use RandomAccessFile as Jos suggested.

    What does C do that is different?

    With C and C++ you can write to an existing file without wiping the file clean. You can basically replace text with other text and you don't even have to append in order to not wipe the file.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How to write to a file without erasing it?

    Yes, you can also do that in Java. See the preceding posts.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default Re: How to write to a file without erasing it?

    Quote Originally Posted by Norm View Post
    Yes, you can also do that in Java. See the preceding posts.
    So I've been using this demo code

    Java Code:
     File file = new File("DemoRandomAccessFile.out");
                RandomAccessFile raf = new RandomAccessFile(file, "rw");
     // Append to the end of the file
                raf.write(0x0A);
                raf.writeBytes("This will complete the Demo");
                raf.close();
    so if (0x0A) indicates appending to the end of a file, what would I put to tell it to go to a specific part of the file?

    is 0x0A a hexadecimal number representing where the cursor is? or is it an instruction of some kind?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How to write to a file without erasing it?

    what would I put to tell it to go to a specific part of the file?
    You need to read the API doc for the class to see what methods it has and which ones might be useful for what you want to do.

    is 0x0A a hexadecimal number representing where the cursor is? or is it an instruction of some kind?
    Neither. In a file it is just one of the 256 different values a byte can have. The meaning of any byte(s) in a file depends on what program is reading the file.
    SurfMan likes this.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: How to write to a file without erasing it?

    In reply #2 I wrote this:
    read its API documentation first.
    and in reply #7 Norm wrote this:
    You need to read the API doc for the class to see what methods it has and which ones might be useful for what you want to do.
    And now you come up with something silly as if the value 0x0a were something special; what have you been smoking?

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: How to write to a file without erasing it?

    0x0A IS special. Its the ASCII code for a newline.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: How to write to a file without erasing it?

    Quote Originally Posted by gimbal2 View Post
    0x0A IS special. Its the ASCII code for a newline.
    Whoop-tee-doo! and 0x41 is the ASCII-code for an A!

    kind regards,

    Jos ;-)
    Build a wall around Donald Trump; I'll pay for it.

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: How to write to a file without erasing it?

    Its rather weak that the demo does not print out "This will complete the Demo" in hex numbers though. Someone got lazy.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default Re: How to write to a file without erasing it?

    Quote Originally Posted by Norm View Post
    You need to read the API doc for the class to see what methods it has and which ones might be useful for what you want to do.


    Neither. In a file it is just one of the 256 different values a byte can have. The meaning of any byte(s) in a file depends on what program is reading the file.
    Okay, I got it working with 0x465. For some reasons I can't explain it just seems to print this weird to the file. It goes to the line it's supposed to, but for some reason for one line it'll only print combobox 1 and 4
    then it will print combobox 2, 3 MANY MANY SPACES then 1 and 4 for the rest of the lines.
    is there something up with my print statement? I don't quite understand why this would print this way.

    Java Code:
    try {
                RandomAccessFile raf = new RandomAccessFile(writeFile,"rw"); 
                 raf.write(0x465);
                 raf.seek(0x465);   
                 raf.writeBytes("\n");
                                for(ct=1;ct<41;ct++){                        
                                  raf.writeBytes(",,,,,,,,,,,,,,,,,,,,,,");
                                 
                                  raf.writeBytes(teflonSpacer1);
                                  raf.writeBytes(",,"+teflonSpacer2+"\t");
                                  raf.writeBytes(",,"+teflonSpacer3);
                                  raf.writeBytes(",,"+teflonLot);                                                            
                               
                                  
                                }
                                raf.close(); 
            } 
            catch (IOException ex) {
                Logger.getLogger(ComponentFilli.class.getName()).log(Level.SEVERE, null, ex);
            }
            }

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How to write to a file without erasing it?

    Can you explain what it is you are trying to do?
    What do expect to be written to the file? Can you post the expected file's contents?
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: How to write to a file without erasing it?

    All I do is refer to my reply #8 and ask you whether or not the value 0x465 fits in a single byte.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  15. #15
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default Re: How to write to a file without erasing it?

    Quote Originally Posted by JosAH View Post
    All I do is refer to my reply #8 and ask you whether or not the value 0x465 fits in a single byte.

    kind regards,

    Jos
    I am well aware that 465 doesn't fit into a single byte.
    Each hex number is 4 bits.

    thus 2 hex numbers together would have to be 8 bit. and I understand this.

    3 hex numbers together is 12 bits.

    12 != 8.
    I tried all 8 bit numbers in hex and they didn't work for some odd reason.
    I don't understand exactly what 465 is doing but it somehow works.
    I suspect it may be chopping off some of the binary (in this case the extra hex number)
    effectively after chopping off the extra hex number it would act like an 8 bit number. Why this would work and not the same 8 bit number in hex, I don't know
    Last edited by innovo; 11-06-2015 at 07:27 PM.

  16. #16
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default Re: How to write to a file without erasing it?

    Quote Originally Posted by Norm View Post
    Can you explain what it is you are trying to do?
    What do expect to be written to the file? Can you post the expected file's contents?
    I made a java application in order to populate certain cells in a .csv file from a combobox.

    I expect to be able to write to certain parts of a pre-populated file is all.
    Last edited by innovo; 11-06-2015 at 07:34 PM.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How to write to a file without erasing it?

    I expect
    Can you describe what was in the file and if it was what you wanted to be there? Be specific. Show byte by byte
    For example:
    bytes 0-4 "XXXXX"
    bytes 5-1200 all 0s
    bytes 1201- 1202 0x1234
    etc
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default Re: How to write to a file without erasing it?

    Quote Originally Posted by Norm View Post
    Can you describe what was in the file and if it was what you wanted to be there? Be specific. Show byte by byte
    For example:
    bytes 0-4 "XXXXX"
    bytes 5-1200 all 0s
    bytes 1201- 1202 0x1234
    etc
    this is what the file reads

    Entered Data,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
    ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
    Serial #,,Part #,,Capacitance,,Esr,,Leakage,,Anode Lot,,,,,Cathode Lot,,Tantalum Lot,,Header Glassing,,Teflon Spacer 1,,Teflon Spacer 2,,Teflon Spacer 3,,Teflon Lot
    ,,,,,,,,,,,,,,,,,,,,,,1,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,4
    ,,2,,31,,,Cathode1,,tant1,,LOLWUT,,,,,,,,,,,


    whereas I'm trying to do
    ,,,,,,,,,,,,,,,,,,,,,,1,,2,,3,,4
    ,,,,,,,,,,,,,,,,,,,,,,1,,2,,3,,4
    ,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,,,,,,,,,,,,1,,2,,3,,4
    ,,,,,,,,,,,,,,,,,,,,,,1,,2,,3,,4
    ,,,,,,,,,,,,,,,,,,,,,,1,,2,,3,,4
    ,,,,,,,,,,,,,,,,,,,,,,1,,2,,3,,4
    ,,,,,,,,,,,,,,,,,,,,,,1,,2,,3,,4
    ,,,,,,,,,,,,,,,,,,,,,,1,,2,,3,,4
    ,,,,,,,,,,,,,,,,,,,,,,1,,2,,3,,4
    ,,,,,,,,,,,,,,,,,,,,,,1,,2,,3,,4
    ,,,,,,,,,,,,,,,,,,,,,,1,,2,,3,,4


    1 corresponds to combobox1

    2 corresponds to combobox 2
    3 corresponds to combobox 3
    and 4 corresponds to combobox 4
    Last edited by innovo; 11-06-2015 at 08:00 PM.

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How to write to a file without erasing it?

    When I execute the code I get two lines written to the file (a line is a string of bytes ending with a line end or the end of the file)
    e
    ,,,,,,,,,,,,,,,,,,,,,,1,,2 ,,3,,4,,,,,,,,,,,,,,,,,,,,,,1,,2 ,,3,,4,,,,,,,,,,,,,,,,,,,,,,1,,2 ,,3,,4,,,,,,,,,,,,,,,,,,,,,,1,,2 ,,3,,4,,,,,,,,,,,,,,,,,,,,,,1,,2 >>>> More of the same

    What program are you using to view the contents of the file

    I removed the seek() to save the 1125 skipped over bytes.
    Last edited by Norm; 11-06-2015 at 08:12 PM.
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default Re: How to write to a file without erasing it?

    Quote Originally Posted by Norm View Post
    When I execute the code I get two lines written to the file (a line is a string of bytes ending with a line end or the end of the file)
    e
    ,,,,,,,,,,,,,,,,,,,,,,1,,2 ,,3,,4,,,,,,,,,,,,,,,,,,,,,,1,,2 ,,3,,4,,,,,,,,,,,,,,,,,,,,,,1,,2 ,,3,,4,,,,,,,,,,,,,,,,,,,,,,1,,2 ,,3,,4,,,,,,,,,,,,,,,,,,,,,,1,,2 >>>> More of the same

    What program are you using to view the contents of the file

    I removed the seek() to save the 1125 skipped over bytes.
    I used seek to skip to a line I needed to go to. essentially I am trying to go to a line that's pre-populated, skip over text (horizontally in the file) and put the text from the combobox in specific spots. a.k.a where 1,,2,,3,,4 are supposed to go

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 03-17-2014, 02:03 AM
  2. How to repaint without erasing previous paint?
    By neal8929 in forum Java 2D
    Replies: 5
    Last Post: 08-31-2012, 11:39 PM
  3. Erasing Memory after user selections
    By Wolfie in forum New To Java
    Replies: 3
    Last Post: 04-15-2011, 11:21 PM
  4. How to open a file without erasing its contents?
    By spmchugh82 in forum New To Java
    Replies: 2
    Last Post: 12-17-2008, 01:52 AM
  5. Writing to excel file erasing existing formatting
    By jmHoekst in forum New To Java
    Replies: 1
    Last Post: 09-16-2008, 05:58 PM

Posting Permissions

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