Java在控制台中漂亮输出路径的集合

huangapple go评论101阅读模式
英文:

Java set of path pretty output in console

问题

Sure, here is the translation of the provided content:

请在Java中提供一个好的解决方案,以漂亮的方式在控制台中打印java.nio.file.Path的集合。
例如:
路径:
/src/test/resources/TestFolder/Wave.java
/src/test/resources/TestFolder 
/src/test/resources/TestFolder/Mello.java 
/src/test/resources/TestFolder/TestFolder2/Dave2.java 
/src/test/resources/TestFolder/TestFolder2/Hello2.java 
/src/test/resources/TestFolder/TestFolder2

期望的结果:
TestFolder 
   Wave.java
   Mello.java 
   TestFolder2 
      Dave2.java 
      Hello2.java
英文:

Please advice good solution in Java how to pretty print in console Set of java.nio.file.Path.
For example:
Path:

/src/test/resources/TestFolder/Wave.java
/src/test/resources/TestFolder 
/src/test/resources/TestFolder/Mello.java 
/src/test/resources/TestFolder/TestFolder2/Dave2.java 
/src/test/resources/TestFolder/TestFolder2/Hello2.java 
/src/test/resources/TestFolder/TestFolder2

And expected result:

TestFolder 
   Wave.java
   Mello.java 
   TestFolder2 
      Dave2.java 
      Hello2.java 

答案1

得分: 3

没有内置的API调用可以实现这个。幸运的是,Java是一种编程语言,而你是一个程序员。让我们来编程吧! Java在控制台中漂亮输出路径的集合

你需要的工具:

relativize 或 getFileName

你可以使用 relativize 调用来生成相对于'根点'的路径。例如:

Paths.get("/src/test/resources").relativize(Paths.get("/src/test/resources/TestFolder/Mello.java"))

会变成代表:TestFolder/Mello.java的路径。

然而,也许你希望输出中的每个条目始终只是一个单独的文件名;在这种情况下,getFileName() 调用会剥离除最后一个以外的所有路径元素:Paths.get("/src/test/resources/TestFolder/TestFolder2/Hello2.java").getFileName() 会产生一个只包含 Hello2.java 的路径(如果需要作为字符串,只需在路径对象上调用 toString() 来获取)。

StringBuilder

可以使用 StringBuilder 类逐步生成更长的字符串片段。

repeat

如果你有一个表示你的'嵌套级别'的整数,在你的示例中,你想要文件名前面有一堆空格,数量等于某个倍数。你可以使用 repeat 调用将数字转换为包含那么多个空格的字符串:String prefix = " ".repeat(5); 会产生一个包含 10 个空格的字符串。

注意:这是较新的API;如果你使用的是旧版本的Java,而这个调用不起作用,你需要自己编写。它只是一个简单的for循环。

Files.isDirectory

要知道给定的文件是否是一个目录,你可以调用它;它返回 true 如果是目录,返回 false 如果不是。

Files.newDirectoryStream

这是'遍历'文件系统的一种方法:它允许你列出给定目录中的每个目录/文件:

Path somePathObject = Paths.get("/foo/bar");
try (var contents = Files.newDirectoryStream(somePathObject)) {
    for (Path content : contents) {
        .. 这将为'/foo/bar'中的每个文件/目录调用一次
    }
}

递归

最后,为了把所有东西结合起来:你会想要遍历给定起始点中的每个子项,如果是文件,则打印与我们嵌套级别相等的缩进数,然后是简单的文件名,然后继续处理下一个条目。对于目录条目,你希望这样做,然后进入该目录,增加嵌套级别。如果将嵌套级别设置为参数,你可以调用自己的方法,以目录作为新的'根点',并将'nestingLevel + 1' 作为嵌套级别传递。

祝你好运 & 玩得开心!

英文:

There is no built in API call that would do this. Fortunately, Java is a programming language, and you're a programmer. Let's program it! Java在控制台中漂亮输出路径的集合

The tools you need:

relativize, or getFileName

You can use the relativize call to produce paths that are relative to a 'root point'. For example:

Paths.get("/src/test/resources").relativize(Paths.get("/src/test/resources/TestFolder/Mello.java"))

turns into a path representing: TestFolder/Mello.java.

However, perhaps you want each entry in your output to always only be just a single file name; in that case, the getFileName() call strips out all path elements except the lasts one: Paths.get("/src/test/resources/TestFolder/TestFolder2/Hello2.java").getFileName() produces a path with just Hello2.java (if you need it as string, just call toString() on the path object to get that).

StringBuilder

The StringBuilder class can be used to produce a longer string piece by piece.

repeat

If you have an int representing your 'nesting level', in your example you want a bunch of spaces in front of the filename equal to some multiple of that. You can use the repeat call to turn a number into a string containing that number of spaces: String prefix = " ".repeat(5); produces a string containing 10 spaces.

NB: This is somewhat newer API; if you're on an old version of java and this call does not work, you'd have to write it yourself. It's just a single for loop.

Files.isDirectory

To know if any given file is a directory, you can call that; it returns true if it is and false if it is not.

Files.newDirectoryStream

This is one way to 'walk' a file system: This lets you list each dir/file in a given directory:

Path somePathObject = Paths.get("/foo/bar");
try (var contents = Files.newDirectoryStream(somePathObject)) {
    for (Path content : contents) {
        .. this is called once for each file/dir in '/foo/bar'
    }
}

recursion

Finally, to tie it all together: You'd want to walk through each child in a given starting point, if it is a file, print a number of spacers equal to our nesting level (which starts at 0), then the simple file name, and then move on to the next entry. For directory entries, you want to do that but then 'dive' into the directory, incrementing the nesting level. If you make the nesting level a parameter, you can call your own method, using the directory as new 'root point', and passing 'nestingLevel + 1' for the nesting level.

Good luck & Have fun!

huangapple
  • 本文由 发表于 2020年4月4日 19:59:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/61027729.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定