aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes saving JTree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "saving JTree" Watch "saving JTree" New topic
Author

saving JTree

m pap
Greenhorn

Joined: Jun 13, 2001
Posts: 13
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

Joined: Jan 17, 2001
Posts: 200
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

Joined: Jun 13, 2001
Posts: 13
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: saving JTree