Solved

Not writing to file

Posted on 2003-11-27
16
303 Views
Last Modified: 2010-04-01
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()
{
      try
      {      
            fw = new FileWriter("taxonamyTree.txt",true);
            fw.write("kjffkjsdafdsj");
            fw.flush();
            fw.close();
      }
      catch(IOException e)
      {
            setError("ERROR OPENING FILE");
      
      }
}
0
Comment
Question by:danBosh
  • 6
  • 4
  • 3
  • +1
16 Comments
 
LVL 15

Expert Comment

by:jimmack
ID: 9830679
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).
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9830682
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?
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9830708
>>  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.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:danBosh
ID: 9830717
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
0
 

Author Comment

by:danBosh
ID: 9830720
and the constructer is called
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9830756
Do you get an exception?
0
 

Author Comment

by:danBosh
ID: 9830782
it doesn't print to the logs and there is no catalina.out in the tomcat directory
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9830851
Are you sure there's no catalina.out?  It should be in the logs directory below the tomcat installation.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9831033
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...

http://dbforums.com/arch/182/2003/8/731227

(at the bottom)
0
 

Author Comment

by:danBosh
ID: 9831345
jim: yes i perfomed a search for it, and it not there

tim: its windows, ill try that link
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 9831358
>>           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?
0
 

Author Comment

by:danBosh
ID: 9831380
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
0
 
LVL 14

Accepted Solution

by:
kennethxu earned 40 total points
ID: 9831410
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.
0
 

Author Comment

by:danBosh
ID: 9831433
outstanding ken, and thanks tim

0
 
LVL 14

Expert Comment

by:kennethxu
ID: 9831444
glad to know your problem is resolved :-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9831510
:-)

Good luck!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

One of the biggest threats in the cyber realm pertains to advanced persistent threats (APTs). This paper is a compare and contrast of Russian and Chinese APT's.
Binary Differential Replication, What it is, how it works and how it differs from standard delta file replication
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now