考虑以下代码:
m_Writer = new PrintWriter(new FileWriter("LoginHistory.dat")); m_Writer.println(Integer.toString(s_NumOfLogins)); m_Writer.println(m_LoginHistoryStr); m_Writer.close();
为什么我找不到名为LoginHistory.dat的文件? 谢谢
更新:我刚发现在行后出现异常:
m_Writer = new PrintWriter(new FileWriter("LoginHistory.dat"));
其细节是: 任何想法是什么真正的问题?
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)
显然,您无权在尝试创建该文件的位置创建该文件,因此出现“拒绝访问”消息。您需要找到一个允许创建文件的目录。如果不确定要在哪里创建文件,则可以使用File.getAbsolutePath()或查看其完整路径File.getCanonicalPath()。
File.getAbsolutePath()
File.getCanonicalPath()