| | |
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 |
6 @param actuate android api applet application arc array arrays automation balls binary bluetooth bold business byte c++ chat class client code codesnippet collections compare component coordinates database defaultmethod doctype dragging ebook eclipse educational error file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework hql html ide ideas image ingres input integer internet intersect invokingapacheantprogrammatically j2me java java.xls javaexcel javaprojects jni jpanel jtextarea julia linux list map method methods mobile mysql netbeans newbie nextline parameter php pong problem program programming project recursion recursive rim scanner sell server set sms sort sql string sun swing swt threads tree web websites windows






