Home > Java > javaTutorial > body text

Java implements receiving file paths from the keyboard and printing file or folder names hierarchically

王林
Release: 2019-11-30 13:50:41
forward
3664 people have browsed it

Java implements receiving file paths from the keyboard and printing file or folder names hierarchically

1. Implementation effect

Requirements: Receive a folder path from the keyboard, and print all the files in the folder and the names of the folders hierarchically.

For example: Print all the files in the folder and the names of the folders hierarchically.

2. Implement logic

Get all files and folders, return the File array, and traverse the array. Whether it is a file or a folder, it needs to be printed directly. If it is a folder, call it recursively.

Related video tutorial sharing: java course

3. Implementation code

aaa is a folder, which contains bbb.txt, ccc.txt, Files such as ddd.txt have folders such as eee. There are fff.txt and ggg.txt in eee, which print out the hierarchy.

For example:

Java implements receiving file paths from the keyboard and printing file or folder names hierarchically

The code is as follows:

public class Test4 {
    public static void main(String[] args) {
 
        File dir = getDir();                //获取文件夹路径
 
        printLev(dir,0);
 
    }
 
    /*
     * 从键盘接收一个文件夹路径
     * 1,返回值类型File
     * 2,参数列表无
     */
 
    public static File getDir() {
 
        //1,创建键盘录入对象
 
        Scanner sc = new Scanner(System.in);
 
        System.out.println("请输入一个文件夹路径:");
 
        //2,定义一个无限循环
 
        while(true) {
 
            //3,将键盘录入的结果存储并封装成File对象
 
            String line = sc.nextLine();
 
            File dir = new File(line);
 
            //4,对File对象判断
 
            if(!dir.exists()) {
 
                System.out.println("您录入的文件夹路径不存在,请输入一个文件夹路径:");
 
            }else if(dir.isFile()) {
 
                System.out.println("您录入的是文件路径,请输入一个文件夹路径:");
 
            }else {
 
                //5,将文件夹路径对象返回
 
                return dir;
 
            }
 
        }
    }

    public static void printLev(File dir,int lev) {
 
        //1,把文件夹中的所有文件以及文件夹的名字按层级打印
 
        File[] subFiles = dir.listFiles();
 
        //2,遍历数组
 
        for (File subFile : subFiles) {
 
            for(int i = 0; i <= lev; i++) {
 
                System.out.print("\t");
 
            }
 
            //3,无论是文件还是文件夹,都需要直接打印
 
            System.out.println(subFile);
 
            //4,如果是文件夹,递归调用
 
            if(subFile.isDirectory()) {
 
                //printLev(subFile,lev + 1);
 
                printLev(subFile,++lev);
 
            }
 
        }
 
    }
 
}
Copy after login

For more related articles and tutorials, please visit: Java Language Introduction

The above is the detailed content of Java implements receiving file paths from the keyboard and printing file or folder names hierarchically. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template