gpt4 book ai didi

java - 如何循环调用函数显示文件之间的父子关系

转载 作者:太空宇宙 更新时间:2023-11-04 12:37:24 25 4
gpt4 key购买 nike

我有一个文件夹(“all_folders”),其中包含 5 个子文件夹(“folder_1”、“folder_2”、“folder_3”、“folder_4”和“folder_5”)。每个子文件夹都包含 2 个文本文件,其名称类似于“file_1.txt”、“file_2.txt”等。每个文本文件都包含下一个文件的地址,例如“file_1.txt”内容是GOTO“file_2.txt”。以同样的方式,一个文件可以有多个地址,而这些文件又可以有其他文件的地址。基本上它就像一棵二叉树。我希望用户输入一个文件名,他想知道他输入的文件包含的所有地址。我想要的输出应该像二叉树。即 file_10 包含文件 file_7 、 file_8 和 file_9 的地址。同样,file_9 包含 file_6 和 file_4 的地址。file_8 包含 file_5 的地址。file_7 不包含任何文件地址等等......我已附加我想要的输出图像以及我拥有的文件和文件夹。

到目前为止,我已经编写了以下代码,其中我将 file_10 包含的地址(假设用户输入 file_10)存储在数组列表中,并能够打印该地址。但现在我希望重复此代码,直到文件没有任何地址(请参阅图像以了解所需的输出)。我计划使用 JTree 将输出显示为二叉树,如图所示。但这是第二件事,首先我需要获得输出。

  1. 我需要有关如何重复调用函数来显示所有文件地址的帮助。
  2. 其次,我正在使用数组列表,但我担心的是,我是否需要拥有与树中父子关系级别一样多的数组列表。因为目前我只有5个文件夹和10个文件,但可能会增加。所以会有很多数组列表。

你能帮我实现这个输出吗?由于这是一个很大的代码,我尝试尽可能地编写注释,但很抱歉,因为我是初学者,所以我可能没有遵循代码中的良好实践。

输出图像: Output Required

附加 all_folder 文件: https://drive.google.com/open?id=0B9hvL6YZBpoTRkVYV0dUWEU5V2M

我的代码如下:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class FindFile
{
String result;
static ArrayList<String> storeAllFileName = new ArrayList<String>(); // This array list will store all file names from all the sub-folders of all_folders
static int i = 0;

public void listFilesAndFilesSubDirectories(String directoryName)
{
File directory = new File(directoryName);
File[] fList = directory.listFiles();
for (File file : fList)
{
if (file.isFile())
{
if (file.getName().endsWith(".txt")) // Checking if the file is
// a text file
{
storeAllFileName.add(file.getName().toLowerCase());
i++;
}
} else if (file.isDirectory())
{
listFilesAndFilesSubDirectories(file.getAbsolutePath());
}
}
}

public static void main(String[] args) throws FileNotFoundException
{
recurrenceFileFind();
}

public static void recurrenceFileFind() throws FileNotFoundException
{
FindFile FindFile = new FindFile();

String fileName = "file_10.txt"; // Hardcoded this value assuming user
// have entered file_10.txt
final String directoryName = "C:\\all_folders"; // Hardcoded this value
// assuming all folder
// of user are placed in
// C:\all_folders
// directory

FindFile.listFilesAndFilesSubDirectories(directoryName);
FindFile.searchDirectory(new File(directoryName), fileName);

System.out.println("\nFile Found at: " + FindFile.getResult());
String filedirectoryName = FindFile.getResult(); // Passing the location
// of the file found
// at so that now we
// can read the text
// of the file and
// search for the
// address of child
// files

File file = new File(filedirectoryName);
Scanner in = new Scanner(file);

ArrayList<String> viewText = new ArrayList<String>(); // This array list
// will store the
// content of the
// file

while (in.hasNext())
{
viewText.add(in.next().toLowerCase()); // Store the content of file
// in a array list viewText
}

ArrayList<String> comparingList = new ArrayList<String>(viewText); // copy
// viewText
// array
// List
// to
// new
// array
// list
// comparingList

comparingList.retainAll(storeAllFileName); // store only those address
// in the comparingList for
// which we have file with
// that name in any of the
// sub-folder, as the file
// can have extra content
// like GOTO or any other
// words

System.out.println("\n\"" + file.getName() + "\"" + " contains below files:");

allListPrint(comparingList); // printing address of files which the
// parent file contains

}

public void searchDirectory(File directory, String fileNameToSearch)
{

if (directory.isDirectory())
{
search(directory, fileNameToSearch);
} else
{
System.out.println(directory.getAbsoluteFile() + " is not a directory!");
}

}

private void search(File directory, String fileNameToSearch)

{
if (directory.isDirectory())
{
System.out.println("Searching directory ... " + directory.getAbsoluteFile());
if (directory.canRead())
{
for (File temp : directory.listFiles())
{
if (temp.isDirectory())
{
search(temp, fileNameToSearch);
} else
{
if (fileNameToSearch.equalsIgnoreCase(temp.getName().toLowerCase()))
{
result = (temp.getAbsoluteFile().toString());
}

}
}

} else
{
System.out.println(directory.getAbsoluteFile() + "Permission Denied");
}
}

}

private static void allListPrint(ArrayList<String> List) // method to print
// array list
{
Iterator<String> itr = List.iterator();
while (itr.hasNext())
{
System.out.println(itr.next());
}

}

public String getResult()
{
return result;
}

}

最佳答案

这是一个递归解决方案。我假设你可以创建 HashMap<String,Node>从你自己的文件目录中。我刚刚手动创建了这样的HashMap为了省时间。但自动完成是非常简单的。一次性读取所有文件并创建 Node对于每个文件,在第二遍中更新它们的 children field 。

class Node {

String name;
List<Node> children = new ArrayList();

public Node(String name) {
this.name = name;
}
}

public class FileTree {

//recursive function for returning children
public void retChildHeirarchy(Node n) {

if (n == null) {
return;
}

for (Node child : n.children) {
retChildHeirarchy(child);
System.out.println(child.name);
}
}

public static void main(String[] args) {

HashMap<String, Node> treeStructure = new HashMap<>();

/*To save time, I manually create the nodes and update HashMap of Nodes
but you can do it automatically.
*/
Node f4 = new Node("file_4");
Node f6 = new Node("file_6");
Node f7 = new Node("file_7");
Node f8 = new Node("file_8");
Node f9 = new Node("file_9");
Node f10 = new Node("file_10");

//update f_10
f10.children.add(f9);
f10.children.add(f8);
f10.children.add(f7);
//update f9
f9.children.add(f6);
f9.children.add(f4);

treeStructure.put("file_4", f4);
treeStructure.put("file_6", f6);
treeStructure.put("file_7", f7);
treeStructure.put("file_8", f8);
treeStructure.put("file_9", f9);
treeStructure.put("file_10", f10);

FileTree ft = new FileTree();
//call the recursive function for the Node that you want:
ft.retChildHeirarchy(f9);
}
}

输出如下。注意f10递归函数工作正常,但是当手动更新 f10 时我没有将 5、2、3 和 1 添加到其子项列表中。

ft.retChildHeirarchy(f9);

file_6
file_4

ft.retChildHeirarchy(f10);

file_6
file_4
file_9
file_8
file_7

关于java - 如何循环调用函数显示文件之间的父子关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37173054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com