Results 1 to 15 of 15
  1. #1
    miki_ir is offline Member
    Join Date
    Sep 2011
    Posts
    15
    Rep Power
    0

    Default help me : idea or sample or guide about file i/o

    Hi
    i want to write little book library management program for my teacher . i write it in c++ in one day but teacher want it by java
    i know java and use it but i/o and work with file is so amazing and hard in java.
    assume that we have members that have : id , name, family ,mobile number
    how i add , delete , edit of members in file
    i know work with file and etc
    i saw that said use ObjectInputStream and ObjectOutputStream
    i use them but not good to me
    any one has a sample that program for example write any number of record in file
    and when we want can read record and edit them?
    any one has simple way to add record of data to file or find one record in file and edit it?
    if u have sample i ask it .
    thank u ;
    apologies for my bad english

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

  3. #3
    miki_ir is offline Member
    Join Date
    Sep 2011
    Posts
    15
    Rep Power
    0

    Default

    dear
    this is one week i read and search i tired .
    this is my question
    how i add id ,name ,family ,phone of members to file?
    and how i find on member record in file and edit it's data ?
    how delete record of one member in file ?
    any one have code that can help me?

  4. #4
    miki_ir is offline Member
    Join Date
    Sep 2011
    Posts
    15
    Rep Power
    0

    Default

    any one have idea how i do this?
    thanks

  5. #5
    Alerhau's Avatar
    Alerhau is offline Senior Member
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    158
    Rep Power
    4

    Default

    I can help you to create a file called save.txt, then if this helps,
    I might help you with loading a file too.

    Java Code:
    final Formatter x;
    					
    					try {
    						x = new Formatter("save.txt");
    						
    						x.format("%s %s %s %d", *String 1*, *String 2*, *String 3*, *Integer 1*);
    						x.close();
    						
    						JOptionPane.showMessageDialog(null, "File Sucessfully Created");
    					} catch (Exception exc) {
    						JOptionPane.showMessageDialog(null, "File Creation Did'nt Work");
    					}
    This should work out in order to create a file, if I missed anything or this does'nt works out,
    then go ahead and ask me a question.

    I'm helping you because I know how difficult it is when you can't find any examples/ get examples.
    I hope nobody tells me that I'm breaking the rules,
    I'm just trying to help.

    Kind,
    Regards Hållenius
    Currently Developing a Game Called Loaie, You Can Download It From "The Loaie Website".

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default

    how i add id ,name ,family ,phone of members to file?
    First you must decide how the pieces of data(fields) are to be organized in a record of the file. How will you know the end of the id field and the beginning of the name field? Will there be a separating character like a ; or a |
    Or will the fields be of fixed length?
    What determines the end of a record and the beginning of the next record?
    Will the data all be text data like what is in a String or will some be binary like int?

    You need to decide on the above before you can design code to search and update the file.

  7. #7
    miki_ir is offline Member
    Join Date
    Sep 2011
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    First you must decide how the pieces of data(fields) are to be organized in a record of the file. How will you know the end of the id field and the beginning of the name field? Will there be a separating character like a ; or a |
    Or will the fields be of fixed length?
    What determines the end of a record and the beginning of the next record?
    Will the data all be text data like what is in a String or will some be binary like int?

    You need to decide on the above before you can design code to search and update the file.
    wow my god : this is too hard . i should consider all these you say?
    ok : all field are string but one field is int for state .

    no one haven't similar program that use file and add update search record of data in the file before me ?

  8. #8
    miki_ir is offline Member
    Join Date
    Sep 2011
    Posts
    15
    Rep Power
    0

    Default

    Norm this good for one record ?
    *1;Tom;Olsen;09765554432# : * for beginning of record ; -->for end of field and # --> for end of record
    then what i should to do?

    when i want edit my post in this forum the save button not work.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Ya, there seems to be a problem with editing right now, I'll talk to the powers that be. For now, just try to edit, and if you can't don't worry about mistakes and add new information via a new post.

    Writing to files seems challenging, but it's not. Have you went through the tutorials I linked? They cover very well how to write to and read from a file. The process also builds on smaller components and really will make the task seem easy. I suggest if you don't understand the tutorial, ask here, and we will do what we can to help you understand.

  10. #10
    miki_ir is offline Member
    Join Date
    Sep 2011
    Posts
    15
    Rep Power
    0

    Default

    thanks dear ;
    i read all of them ; i now how i write and read in/from file . but i want write on record of data like "1 Tom Olsen 09765554432" for any members.
    now how i consider this . i use what for adding record to file . if i use ObjectOutputStream then how i can access to file in random way ?
    i want sample that add delete and edite of record in file . this is hard?

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can get into random access files which may be a bit much. It may be easier to simply read the file(if it's small) into memory via some data structure, then search through it. It would be incredibly beneficial to create a class for all the information. Something like

    Java Code:
    public class Student{
      String name;
      ...
    }
    and give it methods and perhaps using serialization to read any write student objects will be helpful. There are many approaches, you can write the file with delimiters in place and then build the entries when reading the file, while writing to the file will create delimited strings to be written.

    Object Serialization

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default Re: help me : idea or sample or guide about file i/o

    how i can access to file in random way ?
    You can only access records in a file randomly if the records are all the same size (same number of bytes).
    Your records are all different lengths so you would not know where the nth record starts and ends which you would need to know to be able to read that record.

  13. #13
    miki_ir is offline Member
    Join Date
    Sep 2011
    Posts
    15
    Rep Power
    0

    Default Re: help me : idea or sample or guide about file i/o

    Quote Originally Posted by Norm View Post
    You can only access records in a file randomly if the records are all the same size (same number of bytes).
    Your records are all different lengths so you would not know where the nth record starts and ends which you would need to know to be able to read that record.
    then what i should to do?

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default Re: help me : idea or sample or guide about file i/o

    As sunde887 said, if it is a small file, you need to read all of the file into memory, make the changes to it and then write it out to the file, replacing what was there.
    If it is a large file, then you need to be a bit more clever. Perhaps something like using a temp file copy the old file to, updating it as you copy the file and then delete the input file and renaming the new output file to have the name of the original input file.

  15. #15
    miki_ir is offline Member
    Join Date
    Sep 2011
    Posts
    15
    Rep Power
    0

    Default Re: help me : idea or sample or guide about file i/o

    ok
    thanls all

Similar Threads

  1. sample jar file
    By etnad7 in forum New To Java
    Replies: 12
    Last Post: 10-28-2010, 09:04 AM
  2. Creating an executable jar file in IntelliJ IDEA
    By turanan in forum IntelliJ IDEA
    Replies: 0
    Last Post: 12-04-2009, 04:06 PM
  3. Sample build.xml file for EJB
    By deeparanbhor in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 04-27-2009, 02:16 PM
  4. How to calculate the sample rate of an audio file?
    By arnab321 in forum CLDC and MIDP
    Replies: 0
    Last Post: 12-28-2008, 01:39 PM
  5. Replies: 1
    Last Post: 02-16-2008, 04:33 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
  •