Java源码-目录遍历
来源:互联网 发布:c语言打印倒九九乘法表 编辑:程序博客网 时间:2024/06/05 10:35
如果要导出文件夹下的文件目录,可使用DOS的tree命令。
当然,你也可以使用Java程序。
代码如下:
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 GUIpublic 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 specifiespublic 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<Path> 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 nameprivate 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
- Java源码-目录遍历
- C++源码之目录遍历
- java遍历resource目录
- java遍历目录结构
- java 遍历磁盘目录
- java之遍历目录
- java 遍历目录
- java 递归遍历目录
- java遍历文件目录
- JAVA遍历目录
- Java遍历目录
- java遍历磁盘目录
- Java遍历文件目录
- java递归遍历目录
- Java遍历文件目录
- Java遍历目录
- java遍历文件目录方法
- java遍历目录或文件
- 《JavaBean》----Java中的内省机制二
- Library Module详解
- MDI/MDIX接口
- (9)算数运算符
- hdu3032 Nim or not Nim?(SG函数)
- Java源码-目录遍历
- HDU 1874 畅通问题续 最短路径 迪杰斯特拉算法
- mtd编译
- 函数声明、函数表达式、匿名函数、立即执行函数
- hdoj 1596 1598 <变形最短路之最优路---贪心--枚举>
- Android从入门到放弃系列--目录
- No new migrations found. Your system is up-to-date.
- 初识AndroLua+
- 二叉堆