Results 1 to 6 of 6

Thread: read/write file

  1. #1
    akl
    akl is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default read/write file

    FileInputStream in=new FileInputStream(fileName);
    FileOutputStream out=new FileOutputStream(fileName");
    nByte=in.read();
    while(nByte!=-1){
    out.write(change(nByte));
    nByte=in.read();
    }

    How to read and write in the same file?
    Why this code doesn't work correctly ?

  2. #2
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: read/write file

    Take a look here Java 103 : File-handling under Java there are two good examples of read and Write to a file ..

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: read/write file

    Quote Originally Posted by akl View Post
    Why this code doesn't work correctly ?
    It does work correctly; your assumptions how stuff works is incorrect; instead, you should've read the API documentation for the FileOutputStream class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    akl
    akl is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: read/write file

    I want read and write to file at a same time in same file.
    Maybe solution is use temporary file.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: read/write file

    Quote Originally Posted by akl View Post
    I want read and write to file at a same time in same file.
    Maybe solution is use temporary file.
    Yep, that's the way to go: read the (previous) data, do with it what you have to do and finally write back the upated (new) data to the file; possibly you don't even need a temporary file.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    akl
    akl is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: read/write file

    Your suggesting. save previous data to data array, then write back data to the file?

Similar Threads

  1. Replies: 8
    Last Post: 07-25-2012, 11:00 AM
  2. Using Read/Write From File Using Scanner
    By javaisntcoffee123 in forum New To Java
    Replies: 4
    Last Post: 04-15-2010, 04:35 AM
  3. Replies: 5
    Last Post: 02-05-2009, 11:28 AM
  4. Read and Write file
    By mrdestroy in forum New To Java
    Replies: 13
    Last Post: 10-31-2008, 01:11 PM
  5. File read/write problems
    By p900128 in forum New To Java
    Replies: 4
    Last Post: 06-27-2008, 01:15 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
  •