-1

这就是我在下面的 JUnit 测试类上尝试的。

String imagePath  = "/⁨Users⁩/⁨mymacbook/⁨Desktop⁩/⁨Web_Projects⁩/books⁩/EffectiveJava.jpg";
byte[] imageBytes = Files.readAllBytes(Paths.get(imagePath));

这是在测试类中,但是当我运行测试类时,出现以下错误。

java.nio.file.NoSuchFileException: /Users/mymacbook/Desktop/Web_Projects/books/EffectiveJava.jpg

上述路径是 Java 项目的外部路径。

任何帮助将不胜感激。

4

1 回答 1

1

May I suggest you use a system property and Path

String home = System.getProperty("user.home");
Path path = Paths.get(home, "Desktop", "Web_Projects⁩", "book", "EffectiveJava.jpg");

home will contain your home folder and path the full path with the correct separator for your OS

于 2019-10-07T15:59:04.910 回答