遍历并仅获取文件名?

时间:2019-05-16 03:55:17

标签: java java-8

我想遍历目录并获得仅丢弃路径的文件名?我通过java 8 walk方法尝试过,但是路径随文件名一起打印。

这是我到目前为止尝试过的:

  • 不能遍历末尾,而是打印最近的文件名。
    File folder = new File("E:\\Netbeans_Don't_Delete\\Testing");
    File[] listOfFiles =    folder.   listFiles  ();

    for(
    int i = 0;i<listOfFiles.length;i++)
    {
        if (listOfFiles[i].isFile()) {
            System.out.println("File " + listOfFiles[i].getName());
        }
    }
  • 这可以遍历,但可以打印整个路径,但是我想要最后一个文件名
   try(

        Stream<Path> paths = 
                    Files.walk(Paths.get("E:\\Netbeans_Don't_Delete\\Testing")))
        paths.filter(Files::isRegularFile).forEach(System.out::println);
      }

1 个答案:

答案 0 :(得分:1)

使用getFileName获取文件名。

//ConfigureServices
services.AddNodeServices(node => node.ProjectPath = Path.GetFullPath("path/to/nodejs/project"));

//Configure
app.UseSpa(spa =>
{
    app.ApplicationServices.GetRequiredService<INodeServices>().InvokeAsync<object>("path/to/your/entry.js");
    spa.UseProxyToSpaDevelopmentServer("http://localhost:3000"); //remove it if you don't need asp.net core application to proxy for your background service
});