@Controller
public class HelloController {
@RequestMapping("/workers")
public String workers(Model model){
try (Stream<String> stream = Files.lines(Paths.get("Workers.txt"))) {
int randomNum = (int)(Math.random() * 30);
String worker = stream
.filter(o -> o.contains(String.valueOf(randomNum)))
.collect(Collectors.joining());
model.addAttribute("worker",worker);
} catch (IOException e) {
e.printStackTrace();
}
return "workers";
}
}
当我输入Map地址时,我会例外。如何在这种情况下阅读文本文档
java.nio.file.NoSuchFileException: Workers.txt
at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:92)
at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:116)
at java.base/sun.nio.fs.UnixFileSystemProvider.newFileChannel(UnixFileSystemProvider.java:182)
at java.base/java.nio.channels.FileChannel.open(FileChannel.java:292)
at java.base/java.nio.channels.FileChannel.open(FileChannel.java:345)
at java.base/java.nio.file.Files.lines(Files.java:4104)
at java.base/java.nio.file.Files.lines(Files.java:4196)
at pl.coderslab.Homework_02.web.controler.HelloController.workers(HelloController.java:20)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
工人.txt
1, Sukhmani Dunne
2, Sheikh Derrick
3, Zunairah Schaefer
4, Arielle Soto
5, Padraig Monroe
6, Sahib Almond
7, Penny Huang
8, Madeline Blackburn
9, Mahima Estrada
10, Rowena Naylor
11, Whitney Valencia
12, Tyrone Dean
13, Lucca Hart
14, Isaiah Murray
15, Bodhi Serrano
16, Marjorie Finley
17, Carol Singleton
18, Dayna Coates
19, Cindy Goddard
20, Owais Davie
21, Richard Forbes
22, Muhammad Wainwright
23, Meredith England
24, Riaz Daugherty
25, Konnor Blundell
26, Fahmida Compton
27, Pooja Britton
28, Randall Pratt
29, Ariella Barnes
30, Ahmed Benton
1条答案
按热度按时间olhwl3o21#
NoSuchFileException
意味着在服务器的文件系统中找不到该文件。在代码中,您只使用文件名“workers.txt”。尝试使用绝对路径打开文件,这可能是调试问题并找到解决方案的第一步。