gpt4 book ai didi

java - Eclipse Java Web 服务无法读取文件

转载 作者:行者123 更新时间:2023-11-30 10:29:55 25 4
gpt4 key购买 nike

我用 Eclipse 创建了一个 Java 网络服务,昨天运行良好,但现在 Scanner 突然无法找到该文件,即使它位于同一目录中!

我已经尝试找到它的绝对路径并提供确切路径,但它仍然坚持找不到它。但是,如果我只是将它作为 Java 应用程序运行,它会找到它!我在尝试使用 Web 服务读取文件时做错了什么吗?

我的代码是这样的:

try {
File file = new File("./data.txt");
Scanner sc = new Scanner(file);
String[] arr = new String[3];

while(sc.hasNextLine()) {
String s = sc.nextLine();
arr = s.split(",");
}
}catch (FileNotFoundException e) {
e.printStackTrace();
}

作为 Web 服务运行时我不断遇到的错误是

   file foundjava.io.FileNotFoundException: ./data.txt (The system cannot fin
d the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at PasswordReset.passwordReset(PasswordReset.java:92)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.axis.providers.java.RPCProvider.invokeMethod(RPCProvider.java:397
)
at org.apache.axis.providers.java.RPCProvider.processMessage(RPCProvider.java:1
86)
at org.apache.axis.providers.java.JavaProvider.invoke(JavaProvider.java:323)
at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:
32)
at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)
at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)
at org.apache.axis.handlers.soap.SOAPService.invoke(SOAPService.java:454)
at org.apache.axis.server.AxisServer.invoke(AxisServer.java:281)
at org.apache.axis.transport.http.AxisServlet.doPost(AxisServlet.java:699)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at org.apache.axis.transport.http.AxisServletBase.service(AxisServletBase.java:
327)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Application
FilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterCh
ain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Application
FilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterCh
ain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.ja
va:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.ja
va:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase
.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171
)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102
)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java
:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Proce
ssor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(Abstrac
tProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:
314)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.ja
va:61)
at java.lang.Thread.run(Unknown Source)

有人知道问题出在哪里吗?

最佳答案

您在错误的路径中搜索文件。或者程序启动时未创建文件。请验证这两个解决方案,前一段时间有同样的问题。

关于java - Eclipse Java Web 服务无法读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43845468/

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