Not writing to file

I have a bean with the following constructor, it does not write to the file when used as a bean, but when i create a main method and create an instance of the class and run it as a class the file does not get written to. Why? and how do i make it write to a file when used as a bean

public Taxonamy()
            fw = new FileWriter("taxonamyTree.txt",true);
      catch(IOException e)
            setError("ERROR OPENING FILE");
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

When you say it doesn't write to the file (when used as a bean), do you mean that the exception is thrown or that an empty file is created?

Where is the bean being used?  It may be a permission problem (eg. no write permissions for the directory or file that you are trying to write to).

Try adding the following inside the catch:

System.out.println("IOException from Taxonamy:" + e.toString());

This should give you some more detail about the exception.  (If you're using Tomcat, the output will appear in /logs/catalina.out).
My guess would be the constructor is never called when it's used as a bean...

I'm not sure if this is normal behaviour for Beans, but I guess so, as your code is doing it ;-)

Can't you make the file writing a method, and then call invoke method from the JSP?
>>  My guess would be the constructor is never called when it's used as a bean.

Not really my area, but is this right?  Surely a bean constructor must be called somewhere.
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

danBoshAuthor Commented:
tim: i dont think it is that i just put the code in there for simplicity, it didnt work in other locations of the bean either
danBoshAuthor Commented:
and the constructer is called
Do you get an exception?
danBoshAuthor Commented:
it doesn't print to the logs and there is no catalina.out in the tomcat directory
Are you sure there's no catalina.out?  It should be in the logs directory below the tomcat installation.
Sounds like a securityexception...

It could be you dont have write privs to that directory (if you are running on Linux/Unix)

You might also be running Tomcat with the security manager, in which case you'll have to grant rights to create this file...

(at the bottom)
danBoshAuthor Commented:
jim: yes i perfomed a search for it, and it not there

tim: its windows, ill try that link
>>           fw = new FileWriter("taxonamyTree.txt",true);
The file will be created in the dir that tomcat starts from, most likely in tomcat/bin, not the dir where jsp page is. do you have the write permission in tomcat/bin?
danBoshAuthor Commented:
when i ran the class in command line i created the file taxonamyTree.txt i the web-inf\classes\..\..\ directory where the class is and it wrote to that file, so why would it write to somewhere different when used in conjuntion to a JSP? but ill gp and check anyway
because you current directory of dos windows is in that directory when you run in command line.
when you run in jsp, it's the directory that you start the tomcat.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
danBoshAuthor Commented:
outstanding ken, and thanks tim

glad to know your problem is resolved :-)

Good luck!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.