| Author |
write to a file in another class
|
patrick tang
Ranch Hand
Joined: Dec 16, 2001
Posts: 44
|
|
hi all, i have a problem regarding file/io in a class i create a file structure class Store { private File storename, manager, catalogue, order; public Store(String sn) throws IOException { storename = new File("mall", sn); storename.mkdirs(); manager = new File(storename, "manager"); manager.createNewFile(); catalogue = new File(storename, "catalogue"); catalogue.createNewFile(); order = new File(storename, "order"); order.createNewFile(); } public File getStoreName() throws IOException { return storename; } public String getCatalogue() throws IOException { return catalogue.getCanonicalPath(); } in another class i was trying to write contents of a textarea to the file catalogue (which is supposed to be lcoated at ./mall/storename/catalogue class StoreManagerGUI extends JFrame implements ActionListener { ...... public void actionPerformed(ActionEvent e) { Object o = e.getSource(); if ( o == buttonOK ) { try { Store s = new Store(textfieldStoreName.getText()); File f1 = s.getStoreName(); fos1 = new FileOutputStream(f1); dos1 = new DataOutputStream(fos1); dos1.writeUTF(textfieldStoreName.getText()); dos1.close(); String f2 = s.getCatalogue(); fos2 = new FileOutputStream(f2); dos2 = new DataOutputStream(fos2); dos2.writeUTF(textareaCat.getText()); dos2.close(); } catch(FileNotFoundException fnfe) { System.out.println("file not found"); } catch(IOException ioe) { System.out.println("io exception"); } } } ...... } but i always got filenotfound exception. i think the problem is at String f2 = s.getCatalogue(); though f2 does contain the pathname of the file (c:\...\mall\storename\catalogue) that i wanna write to. i can see the file catalogue created in my hard drive, but i just cant locate it and use it as the parameter to the fileoutputstream. i've tried many ways and still dont get it. can anybody pls give me a hint? thanks
|
 |
Les Dsouza
Greenhorn
Joined: Jan 29, 2002
Posts: 27
|
|
The problem appears to be in: File f1 = s.getStoreName(); fos1 = new FileOutputStream(f1); f1 actually represents a folder. ref your code: Hence exception will be thrown when you try to open an fileoutputstream to it. hope this solves your problem.
|
 |
patrick tang
Ranch Hand
Joined: Dec 16, 2001
Posts: 44
|
|
you are rite, les. that's the problem. thanks for helping.
|
 |
 |
|
|
subject: write to a file in another class
|
|
|