项目是 onJava8-Examples-Maven-master 码云github都有
下面代码的 Cheese.dat 这个相对路径找不到,当然其他的类中这样的相对路径都是错误的 哭
代码
// streams/FileToWordsBuilder.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class FileToWordsBuilder {
Stream.Builder<String> builder = Stream.builder();
public FileToWordsBuilder(String filePath)
throws Exception {
Files.lines(Paths.get(filePath))
.skip(1) // Skip the comment line at the beginning
.forEach(line -> {
for (String w : line.split("[ .?,]+"))
builder.add(w);
});
}
Stream<String> stream() {
return builder.build();
}
public static void
main(String[] args) throws Exception {
new FileToWordsBuilder("Cheese.dat").stream()
.limit(7)
.map(w -> w + " ")
.forEach(System.out::print);
}
}
/* Output:
Not much of a cheese shop really
*/
运行结果
Exception in thread "main" java.nio.file.NoSuchFileException: Cheese.dat
at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:230)
at java.nio.file.Files.newByteChannel(Files.java:361)
at java.nio.file.Files.newByteChannel(Files.java:407)
at java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:384)
at java.nio.file.Files.newInputStream(Files.java:152)
at java.nio.file.Files.newBufferedReader(Files.java:2784)
at java.nio.file.Files.lines(Files.java:3744)
at java.nio.file.Files.lines(Files.java:3785)
at FileToWordsBuilder.<init>(FileToWordsBuilder.java:15)
at FileToWordsBuilder.main(FileToWordsBuilder.java:29)
Process finished with exit code 1
路径在这
输出class文件在这
开始时class 里面没有 Cheese.dat,后面我加了个文件夹子标记为resource,就有了
运行成功
然而在idea中还是找不到,求大佬解惑.