遍历目录树java源码,Java源码-目录遍历

从源码理解TreeMap.java package java.util;import java.io.Serializable;import java.util.function.BiConsumer;import java.util.function.BiFunction;import java.util.function.Consumer;/** * 基于红黑树的NavigableMap接口实现 * Map是根据关

如果要导出文件夹下的文件目录,可使用DOS的tree命令。

当然,你也可以使用Java程序。

转载自:http://www.blogjava.net/CarpenterLee/archive/2016/04/27/430268.html 总体介绍 之所以把HashSet和HashMap放在一起讲解,是因为二者在Java里有着相同的实现,前者仅仅是对后者做了一层包装,也就是说HashSet里面有一个HashMap(适配器模式)。因此

代码如下:

package example;

//Fig. 15.12: JFileChooserDemo.java

//Demonstrating JFileChooser.

import java.io.IOException;

import java.nio.file.DirectoryStream;

import java.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.Paths;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

public class JFileChooserDemo extends JFrame

{

private final JTextArea outputArea; // displays file contents

// set up GUI

public JFileChooserDemo() throws IOException

{

super("JFileChooser Demo");

outputArea = new JTextArea();

add(new JScrollPane(outputArea)); // outputArea is scrollable

analyzePath(); // get Path from user and display info

}

// display information about file or directory user specifies

public void analyzePath() throws IOException

{

// get Path to user-selected file or directory

Path path = getFileOrDirectoryPath();

if (path != null && Files.exists(path)) // if exists, display info

{

// gather file (or directory) information

StringBuilder builder = new StringBuilder();

builder.append(String.format("%s:%n", path.getFileName()));

builder.append(String.format("%s a directory%n",

Files.isDirectory(path) ? "Is" : "Is not"));

builder.append(String.format("%s an absolute path%n",

path.isAbsolute() ? "Is" : "Is not"));

builder.append(String.format("Last modified: %s%n",

Files.getLastModifiedTime(path)));

builder.append(String.format("Size: %s%n", Files.size(path)));

builder.append(String.format("Path: %s%n", path));

builder.append(String.format("Absolute path: %s%n",

path.toAbsolutePath()));

if (Files.isDirectory(path)) // output directory listing

{

builder.append(String.format("%nDirectory contents:%n"));

// object for iterating through a directory's contents

DirectoryStream directoryStream =

Files.newDirectoryStream(path);

for (Path p : directoryStream)

builder.append(String.format("%s%n", p));

}

outputArea.setText(builder.toString()); // display String content

}

else // Path does not exist

{

JOptionPane.showMessageDialog(this, path.getFileName() +

" does not exist.", "ERROR", JOptionPane.ERROR_MESSAGE);

}

} // end method analyzePath

// allow user to specify file or directory name

private Path getFileOrDirectoryPath()

{

// configure dialog allowing selection of a file or directory

JFileChooser fileChooser = new JFileChooser();

fileChooser.setFileSelectionMode(

JFileChooser.FILES_AND_DIRECTORIES);

int result = fileChooser.showOpenDialog(this);

// if user clicked Cancel button on dialog, return

if (result == JFileChooser.CANCEL_OPTION)

System.exit(1);

// return Path representing the selected file

return fileChooser.getSelectedFile().toPath();

}

public static void main(String[] args) throws IOException

{

JFileChooserDemo application = new JFileChooserDemo();

application.setSize(400, 400);

application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

application.setVisible(true);

}

} // end class JFileChooserDemo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值