gpt4 book ai didi

java - 为什么 FileWriter 不创建新文件?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:33:39 24 4
gpt4 key购买 nike

考虑以下代码:

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

关于java - 为什么 FileWriter 不创建新文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10767117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com