| | |
Storing classes in files
![]() |
Hello,
I am trying to do something very simple but my dial up connection is making it hard to research so I'd like some help! I'm trying to store instances of classes I created in a file. Something along these lines:
I know that there is some way to do this through serialization and I've also heard that something along the lines of FileOutputStream/FileInputStream will allow me to write/read the classes but I would appreciate a little guidance on their use in this context. I'm sorry to bother you guys but my dial up is hindering the research process! Thanks!
I am trying to do something very simple but my dial up connection is making it hard to research so I'd like some help! I'm trying to store instances of classes I created in a file. Something along these lines:
Java Syntax (Toggle Plain Text)
class apple { int flavor; String color; apple(int a, String b) { flavor = a; color = b; } } //..... apple RedDelicious = new apple(10, "red"); writeFile.write(RedDelicious);
I know that there is some way to do this through serialization and I've also heard that something along the lines of FileOutputStream/FileInputStream will allow me to write/read the classes but I would appreciate a little guidance on their use in this context. I'm sorry to bother you guys but my dial up is hindering the research process! Thanks!
Your class must implements a java.io.Serializable Interface and one more thing is that you have to use java.io.ObjectInputStream and java.io.ObjectOutputStream class to read and write objects respectively.
Java Syntax (Toggle Plain Text)
class apple implements java.io.Serializable { int flavor; String color; apple(int a, String b) { flavor = a; color = b; } } //..... apple RedDelicious = new apple(10, "red"); java.io.ObjectOutputStream os=new java.io.ObjectOutputStream(new java.io.FileOutputStream("data.txt")); os.writeObject(RedDelicious); os.flush();
Failure is not fatal, but failure to change might be. - John Wooden
![]() |
Similar Threads
- Help with classes in three different files (Python)
- How to configure GD (PHP)
- php.ini confusion (PHP)
- Wrtting into multiple files using loop (C++)
- classes and files (C++)
- Interface File and Multiple Classes (C++)
- C++ File Generator (C++)
- ClassView is not showing my classes (VC++, namespace, headers) ?!?!? (C++)
- External Hard Drive recommendations? suggested gigs? for media collecting/storing? (Storage)
- storing voice files... (Visual Basic 4 / 5 / 6)
Other Threads in the Java Forum
- Previous Thread: Creating an object and getting a value from it
- Next Thread: Interacting with other windows?
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application arguments array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class client code color component count database derby design eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui homework html ide if_statement image integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia keyword linux list macintosh map method methods midlethttpconnection mobile netbeans newbie nullpointerexception object open-source os problem producer program programming project projectideas property read recursion reference replaysolutions ria scanner search server set size sms sort sourcelabs splash sql sqlite stop string swing threads transforms tree ui unicode validation windows






