This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

saving JTree

 
m pap
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all.
Does anyone know how to code saving a JTree to a file. I want to be able to save a newly constructed Jtree to a file. Thanks.
 
Sajee Joseph
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Here is the code u can use to save a JTree. Remember that there is nothing special in a JTree. A JTree is an object too.
So let 'jtree' be the the JTREE object u want to save.
This code will help u save the JTREE in a file called 'MyObject'
try
{
FileOutputStream fos = new FileOutputStream("MyObject");
ObjectOutputStream oos= new ObjectOutputStream(fos);
oos.writeObject(jtree);
}
catch(Exception e)
{
}
Similarly u can read the fileobject using the follwing code
try
{
FileInputStream fis = new FileInputStream("MyObject");
ObjectInputStream oos= new ObjectInputStream(fis);
JTree jtree = (JTree)oos.readObject();
}
catch(Exception e)
{
}

 
m pap
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks.but one question
what if I want the file name to be an input from the user.
For instance the user was manapulating data, that by code is structured in a JTree, and wants to save their work. Therefore will save the changes to a file. I want to know how to save these changes to a jtree to a file. A file they will name. I hope I make sense.
All help is great.
thank you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic