java显示目录文件列表和删除目录
*/
.hljs {
display: block;
overflow-x: auto;
padding: 0.5em;
color: #333;
background: #f8f8f8;
}
.hljs-comment,
.hljs-template_comment,
.diff .hljs-header,
.hljs-javadoc {
color: #998;
font-style: italic;
}
.hljs-keyword,
.css .rule .hljs-keyword,
.hljs-winutils,
.javascript .hljs-title,
.nginx .hljs-title,
.hljs-subst,
.hljs-request,
.hljs-status {
color: #333;
font-weight: bold;
}
.hljs-number,
.hljs-hexcolor,
.ruby .hljs-constant {
color: #099;
}
.hljs-string,
.hljs-tag .hljs-value,
.hljs-phpdoc,
.tex .hljs-formula {
color: #d14;
}
.hljs-title,
.hljs-id,
.coffeescript .hljs-params,
.scss .hljs-preprocessor {
color: #900;
font-weight: bold;
}
.javascript .hljs-title,
.lisp .hljs-title,
.clojure .hljs-title,
.hljs-subst {
font-weight: normal;
}
.hljs-class .hljs-title,
.haskell .hljs-type,
.vhdl .hljs-literal,
.tex .hljs-command {
color: #458;
font-weight: bold;
}
.hljs-tag,
.hljs-tag .hljs-title,
.hljs-rules .hljs-property,
.django .hljs-tag .hljs-keyword {
color: #000080;
font-weight: normal;
}
.hljs-attribute,
.hljs-variable,
.lisp .hljs-body {
color: #008080;
}
.hljs-regexp {
color: #009926;
}
.hljs-symbol,
.ruby .hljs-symbol .hljs-string,
.lisp .hljs-keyword,
.tex .hljs-special,
.hljs-prompt {
color: #990073;
}
.hljs-built_in,
.lisp .hljs-title,
.clojure .hljs-built_in {
color: #0086b3;
}
.hljs-preprocessor,
.hljs-pragma,
.hljs-pi,
.hljs-doctype,
.hljs-shebang,
.hljs-cdata {
color: #999;
font-weight: bold;
}
.hljs-deletion {
background: #fdd;
}
.hljs-addition {
background: #dfd;
}
.diff .hljs-change {
background: #0086b3;
}
.hljs-chunk {
color: #aaa;
}
#container {
padding: 15px;
}
pre {
border: 1px solid #ccc;
border-radius: 4px;
display: block;
background-color: #f8f8f8;
}
pre code {
white-space: pre-wrap;
}
.hljs,
code {
font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;
}
:not(pre) > code {
padding: 2px 4px;
font-size: 90%;
color: #c7254e;
background-color: #f9f2f4;
white-space: nowrap;
border-radius: 4px;
}
-->
以d:\a目录为例,假设D:\a目录内的结构如下:
d:\a
|--a.sql
|--back.log
|--b
| |--e
| | |--1.txt
| | |--2.txt
| | `--3.txt
| `--f
| |--4.txt
| |--5.txt
| `--6.txt
|--c
| |--e
| | |--ace1.txt
| | |--ace2.txt
| | `--ace3.txt
| `--f
| |--4.txt
| |--5.txt
| `--6.txt
`--d
|--a.java
|--abc (1).txt
|--abc (2).txt
|--abc (3).txt
|--b.java
`--c.java
4.1 示例1:列出整个目录中的文件(递归)
思路:
1.遍历目录d:\a。
2.每遍历到d:\a中的一个目录就遍历这个子目录。因此需要判断每个遍历到的元素是否是目录。
以下是从普通代码到递归代码前的部分代码:
File dir = new File("d:/a");
File[] file_list = dir.listFiles();
for (File list : file_list) {
if (list.isDirectory()) {
File dir_1 = list.listFiles(); //此处开始代码重复,且逻辑上可能会无限递归下去
if (dir_1.isDirectory()) {
....
}
} else {
System.out.println(list.getAbsolutePath());
}
}
对重复的代码部分进行封装,于是使用递归方法,既封装代码,又解决无限递归问题。最终代码如下:
import java.io.*;
public class ListAllFiles {
public static void main(String[] args) {
File dir = new File("d:/a");
System.out.println("dir------>"+dir.getAbsolutePath());
listAll(dir);
}
public static void listAll(File dir) {
File[] file_list = dir.listFiles();
for (File file : file_list) {
if (file.isDirectory()) {
System.out.println("dir------>"+file.getAbsolutePath());
listAll(file);
} else {
System.out.println("file------>"+file.getAbsolutePath());
}
}
}
}
4.2 示例2:列出整个目录中的文件(队列)
思路:
1.遍历给定目录。将遍历到的目录名放进集合中。
2.对集合中的每个目录元素进行遍历,并将遍历到的子目录添加到集合中,最后每遍历结束一个目录就从集合中删除它。
3.这样一来,只要发现目录,就会一直遍历下去,直到某个目录整个都遍历完,开始遍历下一个同级目录。
需要考虑的是使用什么样的集合。首先集合内目录元素无需排序、不同目录内子目录名可能重复,因此使用List集合而非set集合,又因为频繁增删元素,因此使用linkedlist而非arraylist集合,linkedlist集合最突出的特性就是FIFO队列。
相比于递归遍历,使用队列遍历目录的好处是元素放在容器中,它们都在堆内存中,不容易内存溢出。
import java.util.*;
import java.io.*;
public class ListAllFiles2 {
public static void main(String[] args) {
File dir = new File("d:/a");
Queue<File> file_queue = new Queue<File>(); //构建一个队列
File[] list = dir.listFiles();
for (File file : list) { //遍历顶级目录
if(file.isDirectory()) {
System.out.println("dir------>"+file.getAbsolutePath());
file_queue.add(file);
} else {
System.out.println("file------>"+file.getAbsolutePath());
}
}
while (!file_queue.isNull()) { //从二级子目录开始,逐层遍历
File subdirs = file_queue.get(); //先取得二级子目录名称
File[] subFiles = subdirs.listFiles();
for (File subdir : subFiles) { //遍历每个下一级子目录
if(subdir.isDirectory()) {
System.out.println("dir------>"+subdir.getAbsolutePath());
file_queue.add(subdir); //如果内层还有子目录,添加到队列中
} else {
System.out.println("file------>"+subdir.getAbsolutePath());
}
}
}
}
}
class Queue<E> {
private LinkedList<E> linkedlist;
Queue() {
linkedlist = new LinkedList<E>();
}
public void add(E e) {
linkedlist.addFirst(e); //先进
}
public E get() {
return linkedlist.removeLast(); //先出
}
public boolean isNull() {
return linkedlist.isEmpty();
}
}
4.3 示例3:树形结构显示整个目录中的文件(递归)
思路:
1.先列出一级目录和文件。
2.如果是目录,则加一个构成树形的前缀符号。然后再遍历这个目录,在此需要递归遍历。
import java.io.*;
public class TreeFiles {
public static void main(String[] args) {
File dir = new File("d:/a");
System.out.println(dir.getName());
listChilds(dir,1);
}
public static void listChilds(File f,int level) {
String prefix = "";
for(int i=0;i<level;i++) {
prefix = "| " + prefix;
}
File[] files = f.listFiles();
for (File file : files) {
if(file.isDirectory()) {
System.out.println(prefix + file.getName());
listChilds(file,level+1);
} else {
System.out.println(prefix + file.getName());
}
}
}
}
结果如下:
a
| a.sql
| b
| | e
| | | 1.txt
| | | 2.txt
| | | 3.txt
| | f
| | | 4.txt
| | | 5.txt
| | | 6.txt
| back.log
| c
| | e
| | | ace1.txt
| | | ace2.txt
| | | ace3.txt
| | f
| | | 4.txt
| | | 5.txt
| | | 6.txt
| d
| | a.java
| | abc (1).txt
| | abc (2).txt
| | abc (3).txt
| | b.java
| | c.java
4.4 删除整个目录
import java.io.*;
public class FileDelete {
public static void main(String[] args) {
File file = new File("d:/a");
rm(file);
}
public static void rm(File f) {
if(!f.exists()){
System.out.println("file not found!");
return;
} else if(f.isFile()) {
f.delete();
return;
}
File[] dir = f.listFiles();
for(File file : dir) {
rm(file);
}
f.delete();
}
}
注:若您觉得这篇文章还不错请点击右下角推荐,您的支持能激发作者更大的写作热情,非常感谢!
java显示目录文件列表和删除目录的更多相关文章
- apache显示目录文件列表
在apache服务器下访问一个目录,如果没有index.html/index.php,则会报错. 为了访问文件夹: 1. 在 /var/www/html 目录下新建 /d/ mkdir d 2. t ...
- tomcat 显示目录文件列表
conf/web.xml中,listings改为true,重启 http://liusu.iteye.com/blog/794613 <servlet> <servlet-name& ...
- Java学习-042-获取目录文件列表(当前,级联)
以下三个场景,在我们日常的测试开发中经常遇到: 软件自动化测试,在进行参数测试时,我们通常将所有相似功能的参数文件统一放在一个目录中,在自动化程序启动的时候,获取资源参数文件夹中所有参数文件,然后解析 ...
- Linux显示只显示目录文件
Linux显示只显示目录文件 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ ls -l -d */ drwxr-xr-x 2 root root 4096 1 ...
- 显示目录文件命令 - ls
1) 命令名称:ls 2) 英文原意:list 3) 命令所在路径:/bin/ls 4) 执行权限:所有用户 5) 功能描述:显示目录文件 6) 语法: ls 选项[-ald][文件或目录] -a 显 ...
- nginx 目录文件列表功能配置
工作中常常有写不能有网页下载东西的需求,在Apache下搭建完成后直接导入文件即可达到下载/显示文件的效果,而Nginx也可以满足这样的需求(nginx 目录列表功能默认是关闭的),这时就需要配置. ...
- 开启Nginx的目录文件列表功能
ngx_http_autoindex_module 此模块用于自动生成目录列表,ngx_http_autoindex_module只在 ngx_http_index_module模块未找到索引文件时 ...
- 开启nginx目录文件列表功能
ngx_http_autoindex_module 此模块用于自动生成目录列表,ngx_http_autoindex_module只在 ngx_http_index_module模块未找到索引文件时 ...
- eclipse svn新增文件不显示在文件列表,只有修改文件可以提交!
1.情景展示 eclipse修改的文件可以正常提交,但是新增的文件没有显示在提交列表中,导致无法提交! 2.解决方案 选中要提交的文件-->右键-->Team-->提交 勾选上这 ...
随机推荐
- 模仿J2EE的session机制的App后端会话信息管理
此文章只将思想,不提供具体完整实现(博主太懒,懒得整理),有疑问或想了解的可以私信或评论 背景 在传统的java web 中小型项目中,一般使用session暂存会话信息,比如登录者的身份信息等.此机 ...
- 解决No enclosing instance of type * is accessible
写一个内部类,并在构造函数中初始化时,遇到报错,搜索问题后发现,有网友出现过类似的问题,下面这个是说的浅显明白的,并确实解决了问题.于是,以下内容照搬过来,不再多费键盘了. public class ...
- 《java.util.concurrent 包源码阅读》17 信号量 Semaphore
学过操作系统的朋友都知道信号量,在java.util.concurrent包中也有一个关于信号量的实现:Semaphore. 从代码实现的角度来说,信号量与锁很类似,可以看成是一个有限的共享锁,即只能 ...
- 简单的OC程序
知识点 1.#import的用途: 1> 跟#include一样,拷贝文件的内容 2> 可以自动防止文件的内容被重复拷贝 2.#import <Foundation/NSObjCRu ...
- 全内存的redis用习惯了?使用基于硬盘存储类似redis的nosql产品ssdb呢?
首先说一下背景,在双十一的时候,我们系统接受X宝的订单推送,同事原先的实现方式是使用redis的List作为推送数据的承载,在非大促的场景下, 一切运行正常,内存占用大概3-4G,机器是16G内存.由 ...
- MySQL的外键,修改表,基本数据类型,表级别操作,其他(条件,通配符,分页,排序,分组,联合,连表操作)
MySQL的外键,修改表,基本数据类型,表级别操作,其他(条件,通配符,分页,排序,分组,联合,连表操作): a.创建2张表 create table userinfo(nid int not nul ...
- Serializable 都这么牛逼了,Parcelable 还要你何用?
一些闲聊 距离上一篇文章似乎又是很久了,看起来也没有很多反馈,催更就更不用说了.哈哈,放弃了. 话说最近公司在招聘一批至少 5 年开发经验的 Android 开发工程师,我也是忙开了花,激动得不行呀. ...
- Python数据分析中 DataFrame axis=0(0轴)与axis=1(1轴)的理解
python中的axis究竟是如何定义的呢?他们究竟代表是DataFrame的行还是列? 直接上代码people=DataFrame(np.random.randn(5,5), columns=['a ...
- 2016普及组t3海港
好的,说说这道题的思路,爆搜队列嘛: 用一个结构体队列存每个人来的时间和他的国籍,用一个vis数组存每个人来的次数,是第一次来sum便加一. 然后从前面第一个人开始扔(原谅我用这个词,因为我找不到更好 ...
- ibv_open_device()函数
struct ibv_context *ibv_open_device(struct ibv_device *device); 描述 函数会创建一个RDMA设备相关的context:可以通过ibv_c ...