简体   繁体   中英

Why FileWriter doesn't create a new file?

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)

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() .

Workaround. ( Fedora linux ).

If I create an empty file from a terminal window with touch command, FileWriter can then save it. This proves that my user account has write access to create a new file in that location. Why can't FileWriter do that as the same user???

Previous comments do not explain this. This seem relatively new I've not seen this before.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM