我必须打印目录树(如树命令),示例:
.
+---A
| +---IMAGES
| +---BACKUP
+---ADOKS
| +---ROZDZIAL_2
| +---ROZDZIAL_3
| +---ROZDZIAL_4
+---AMSC2005
| +---AMSC2004
+---FCCS2005
| +---source
| +---TMP
+---LODZ2004
+---ZAKOPANE2004
+---DYDAKTYKA
| +---DYDAKTYKA_ISI
| | +---COLLS
| | | +---Q1
| | | +---Q2
| | | +---RAZEM
| | | +---RYSUNKI_COLL1
| | | +---RYSUNKI_COLL2
| | +---IMAGES
| | +---src
| | +---WYKLAD5
| +---DYDAKTYKA_PRG
| +---images
| +---POMOC
+---DYDAKTYKA_KST
| +---images
| +---src
+---DYDAKTYKA_WPR
+---images
+---src
我尝试了以下代码:
private static void getInto(String p, int n) {
File path = new File(p);
File[] list = path.listFiles();
for (int i = 0; i < list.length; i++) {
if (list[i].isDirectorhowny()) {
for (int j = 0; j < n; j++)
if (WHAT HERE?)
System.out.print("| ");
else
System.out.print(" ");
System.out.println("+--" + list[i].getName().toString());
getInto(list[i].getPath(), n + 1);
}
}
}
我试了几个版本,还是不行。这该怎么做? If应该放什么条件?我知道这很简单,但我做不到。
最佳答案
我就是这样做的。
代码
import java.io.File;
public class FileAssert {
/**
* Pretty print the directory tree and its file names.
*
* @param folder
* must be a folder.
* @return
*/
public static String printDirectoryTree(File folder) {
if (!folder.isDirectory()) {
throw new IllegalArgumentException("folder is not a Directory");
}
int indent = 0;
StringBuilder sb = new StringBuilder();
printDirectoryTree(folder, indent, sb);
return sb.toString();
}
private static void printDirectoryTree(File folder, int indent,
StringBuilder sb) {
if (!folder.isDirectory()) {
throw new IllegalArgumentException("folder is not a Directory");
}
sb.append(getIndentString(indent));
sb.append("+--");
sb.append(folder.getName());
sb.append("/");
sb.append("\n");
for (File file : folder.listFiles()) {
if (file.isDirectory()) {
printDirectoryTree(file, indent + 1, sb);
} else {
printFile(file, indent + 1, sb);
}
}
}
private static void printFile(File file, int indent, StringBuilder sb) {
sb.append(getIndentString(indent));
sb.append("+--");
sb.append(file.getName());
sb.append("\n");
}
private static String getIndentString(int indent) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < indent; i++) {
sb.append("| ");
}
return sb.toString();
}
}
结果
+--folder1/
| +--a.txt
| +--folder2/
| | +--b1.txt
| | +--b2.txt
| | +--b3.txt
| | +--folder3/
| +--folder4/
单元测试
import static org.junit.Assert.*;
import java.io.File;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class FileAssertTest {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
private File folder1;
@Before
public void setUp() {
folder1 = temporaryFolder.newFolder("folder1");
}
@Test
public void testPrintDirectoryTreeWhenFolderIsEmpty() {
// Invoke
String actual = FileAssert.printDirectoryTree(folder1);
// Verify
assertEquals("+--folder1/\n", actual);
}
private static final String EXPECTED_FCOF = "" + "+--folder1/\n"
+ "| +--a.txt\n";
@Test
public void testPrintDirectoryTreeWhenFolderContainsOneFile()
throws Exception {
// Setup
File aFile = new File(folder1, "a.txt");
assertTrue(aFile.createNewFile());
// Invoke
String actual = FileAssert.printDirectoryTree(folder1);
// Verify
assertEquals(EXPECTED_FCOF, actual);
}
private static final String EXPECTED_COMPLEX = "+--folder1/\n"
+ "| +--a.txt\n" + "| +--folder2/\n" + "| | +--b1.txt\n"
+ "| | +--b2.txt\n" + "| | +--b3.txt\n" + "| | +--folder3/\n"
+ "| +--folder4/\n";
@Test
public void testPrintDirectoryTreeComplex() throws Exception {
// Setup
File aFile = new File(folder1, "a.txt");
assertTrue(aFile.createNewFile());
File folder2 = new File(folder1, "folder2");
assertTrue(folder2.mkdir());
File b1File = new File(folder2, "b1.txt");
assertTrue(b1File.createNewFile());
File b2File = new File(folder2, "b2.txt");
assertTrue(b2File.createNewFile());
File folder3 = new File(folder2, "folder3");
assertTrue(folder3.mkdir());
File b3File = new File(folder2, "b3.txt");
assertTrue(b3File.createNewFile());
File folder4 = new File(folder1, "folder4");
assertTrue(folder4.mkdir());
// Invoke
String actual = FileAssert.printDirectoryTree(folder1);
// Verify
assertEquals(EXPECTED_COMPLEX, actual);
}
}
关于java - 打印目录树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10655085/
我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
是否可以在应用程序中包含的gem代码中知道应用程序的Rails文件系统根目录?这是gem来源的示例:moduleMyGemdefself.included(base)putsRails.root#returnnilendendActionController::Base.send:include,MyGem谢谢,抱歉我的英语不好 最佳答案 我发现解决类似问题的解决方案是使用railtie初始化程序包含我的模块。所以,在你的/lib/mygem/railtie.rbmoduleMyGemclassRailtie使用此代码,您的模块将在
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/
HashMap中为什么引入红黑树,而不是AVL树呢1.概述开始学习这个知识点之前我们需要知道,在JDK1.8以及之前,针对HashMap有什么不同。JDK1.7的时候,HashMap的底层实现是数组+链表JDK1.8的时候,HashMap的底层实现是数组+链表+红黑树我们要思考一个问题,为什么要从链表转为红黑树呢。首先先让我们了解下链表有什么不好???2.链表上述的截图其实就是链表的结构,我们来看下链表的增删改查的时间复杂度增:因为链表不是线性结构,所以每次添加的时候,只需要移动一个节点,所以可以理解为复杂度是N(1)删:算法时间复杂度跟增保持一致查:既然是非线性结构,所以查询某一个节点的时候
遍历文件夹我们通常是使用递归进行操作,这种方式比较简单,也比较容易理解。本文为大家介绍另一种不使用递归的方式,由于没有使用递归,只用到了循环和集合,所以效率更高一些!一、使用递归遍历文件夹整体思路1、使用File封装初始目录,2、打印这个目录3、获取这个目录下所有的子文件和子目录的数组。4、遍历这个数组,取出每个File对象4-1、如果File是否是一个文件,打印4-2、否则就是一个目录,递归调用代码实现publicclassSearchFile{publicstaticvoidmain(String[]args){//初始目录Filedir=newFile("d:/Dev");Datebeg