Results 1 to 10 of 10
  1. #1
    sakshamkum is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Post writing an object of JFrame to disc

    hi,
    i want to make a program that works like notepad to save text files on the disc and the same program should read it.
    the method that comes to my mind is that we should write the object of the program that represents a text file and when we click the file the program recovers the data stored.
    the object should be opened when clicked by the user.
    i want to know that is my approach is correct or not.
    if yes then how can i do so and if not then please correct my approach to do the task.

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

    Default

    when we click the file the program recovers the data stored.
    Most of the program shouldn't be a problem. The quote above will require that you make changes in the OS to associate the filetype that you write out with your application.
    This can be done in Windows and Linux, I don't know about the Mac.

  3. #3
    sakshamkum is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Post reply

    hi,
    the thing i am trying to make, some people call it MDI(Multiple Document Interface)
    how can i do this in java

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

    Default

    No idea what a MDI is. Can you explain.

  5. #5
    JvlShz is offline Member
    Join Date
    Jun 2010
    Posts
    26
    Rep Power
    0

    Default

    to load:

    Java Code:
    BufferedReader br = new BufferedReader(new FileReader(yourFile));
    StringBuilder sb = new StringBuilder();
    final String NL = System.getProperty("line.separator");
    String line;
    while ((line = br.readLine()) !=null) {
     sb.append(line + NL);
    }
    yourTextArea.setText(sb.toString());
    to save:

    Java Code:
    PrintWriter pw = new PrintWriter(new FileWriter(yourFile));
    pw.print(yourTextArea.getText());
    pw.flush();
    pw.close();

  6. #6
    sakshamkum is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Post reply

    see the method writer a file to disc. i am talking of objects that are not files.
    example: to write object of class StringTokenizer[java.util.StringTokenizer]
    or an object of java.net.URL to disc or similar objects that have no connection with files.

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

    Default

    Everything that is written to a disk goes into a "file".
    When data from a file is read into a program, it is no longer a "file".

    Java objects can have their content saved to disk as a file via the process known as serialization. Read API doc for the Serializable interface.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by sakshamkum View Post
    see the method writer a file to disc. i am talking of objects that are not files.
    example: to write object of class StringTokenizer[java.util.StringTokenizer]
    or an object of java.net.URL to disc or similar objects that have no connection with files.
    the XxxxWriter classes are for writing text into text files. If you want to write objects to disk, you'll still have to write them into files -- you have no choice here that I know of, but you'd use an ObjectOutputStream that takes a FileOutputStream as a parameter in its constructor.

  9. #9
    sakshamkum is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default here i need a little more assistance

    i know that to searialize i have to implement java.io.Serializable
    but can you assist me more by telling the further step. what to do after searializing my JFrame object to write it on the disc and give it an icon which is visible to user and when user clicks it the object runs and displays a JFrame just like .txt files open when double clicked.

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

    Default

    give it an icon which is visible to user and when user clicks it the object runs
    That is not possible or at least not easily. To reconstruct the class from the data in the file, you must have a java program that knows how to read a serialized object and convert it to and object.

    just like .txt files open when double clicked.
    The opening of a .txt file in a specific program is an OS function. In Windows there is a table that associates all known file extensions with a command line to be executed when the file is double clicked. User's can change this table and add their own command lines to be executed when a file with a specific extension is doubleclicked.

    Therefore it would be possible for you to create a java program that would read the saved serialized file and reconstruct its objects and associate that program with the file by having the OS put an entry in its table of commandlines.

Similar Threads

  1. Replies: 5
    Last Post: 06-14-2010, 06:10 PM
  2. passing an object from one JDialog to Jframe
    By sinha in forum AWT / Swing
    Replies: 7
    Last Post: 12-16-2009, 04:56 PM
  3. Monitor Disc Head Using Java
    By zorroforce in forum New To Java
    Replies: 4
    Last Post: 09-18-2009, 07:51 AM
  4. Replies: 4
    Last Post: 04-22-2009, 11:02 PM
  5. burn disc in Java
    By nancyhung in forum Advanced Java
    Replies: 1
    Last Post: 04-02-2008, 12:54 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
  •