Why FileWriter doesn't create a new file?
Asked Answered
S

1

4

Consider the following code:

 m_Writer = new PrintWriter(new FileWriter("LoginHistory.dat")); 
 m_Writer.println(Integer.toString(s_NumOfLogins)); 
 m_Writer.println(m_LoginHistoryStr);
 m_Writer.close();   

Any ideas why I don't find any file called LoginHistory.dat?
Thanks

Update: I've just found that I get an exception after line:

m_Writer = new PrintWriter(new FileWriter("LoginHistory.dat"));

and its details are:
Any ideas what is the real problem?

Listening for transport dt_shmem at address: tomcat_shared_memory_id
27/05/2012 15:52:17 org.apache.catalina.startup.HostConfig checkResources
INFO: Undeploying context [/SignInAndGetIp]
27/05/2012 15:52:17 org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor SignInAndGetIp.xml from C:\Users\user\.netbeans\7.1.2\apache-tomcat-7.0.22.0_base\conf\Catalina\localhost
27/05/2012 15:52:17 org.apache.catalina.util.LifecycleBase start
INFO: The start() method was called on component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/SignInAndGetIp]] after start() had already been called. The second call will be ignored.
java.io.FileNotFoundException: LoginHistory.dat (‏‏Access denied)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
    at java.io.FileWriter.<init>(FileWriter.java:46)
    at signIn.SignInAndShowIPTableServlet.init(SignInAndShowIPTableServlet.java:60)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1228)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1147)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:836)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:304)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Spinnaker answered 26/5, 2012 at 14:34 Comment(7)
where did you look for your data file?Crigger
It's a java web application generated on NetBeans , so I checked all the project directory and sub-directoriesSpinnaker
I fit did not through some exception, it has generated the file. You might be looking at the wrong place. Try giving the absolute path.Allyl
Try calling m_Writer.flush() before you close m_Writer.Jahvist
Unless you specify a path, files will be created under your IDE's workspace path. Check that folder.Touzle
It will be created in the directory from which NetBeans starts your application server. Use absolute paths.Pilloff
This was the problem in my case!Fieldpiece
M
2

It seems plain that you don't have permission to create that file where you're trying to create it, hence the "Access denied" message. You'd need to find a directory where you're allowed to create files. If you're not sure where a file is going to be created, you can see its full path with File.getAbsolutePath() or File.getCanonicalPath().

Meggie answered 27/5, 2012 at 13:16 Comment(3)
Thanks for your comment but, I really don't know why I'm not allowed to create a file on my own Hard-disk, it is a bit weird.Spinnaker
I've also tried what you said but unfortunately I don't have any method like you mentioned before in both PrintWriter and FileWriter (These classes don't extend from File class)Spinnaker
It's called "security". Modern OSs limit normal users' permissions to write files. No, FileWriter isn't a File, so you need to create a new File and call the method on that. FileWriter also has a constructor that accepts a File object, so you could also use that if you like. Otherwise, just find out where you're allowed to create files and point your path to there.Meggie

© 2022 - 2024 — McMap. All rights reserved.