在java中编目录的代码 在java中编目录的代码怎么写

用java编写程序,键盘输入一个目录名称,要求分别输出该目录中所有子目录名和以exam开头的文件名。

public static void findFileList(File dir,HashSet dirs,HashSet fileNames ) {

十载的波密网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。营销型网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整波密建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联公司从事“波密网站设计”,“波密网站推广”以来,每个客户项目都认真落实执行。

if (!dir.exists() || !dir.isDirectory()) {// 判断是否存在目录

return;

}

String[] files = dir.list();// 读取目录下的所有目录文件信息

for (int i = 0; i files.length; i++) {// 循环,添加文件名或回调自身

File file = new File(dir, files[i]);

if (file.isFile() file.getName().startsWith("exam")) {// 如果文件

fileNames.add(dir + "\\" + file.getName());

} else {// 如果是目录

dirs.add(dir);

findFileList(file,dirs,fileNames);// 回调自身继续查询

}

}

}

public static void main(String[] args) {

HashSet dirs = new HashSet();

HashSet fileNames = new HashSet();

findFileList(new File("D:\\zhidao"),dirs,fileNames);

System.out.println("目录:"+dirs);

System.out.println("exam开头的文件:"+fileNames);

}

如何用java程序在当前目录下创建一个子目录

用java程序在当前目录下创建一个子目录的方法是利用File对象的mkdirs方法。

完整代码如下:

// 获取当前图片的路径

String path = createImages.getAbsolutePath() + "/Images";

//创建文件对象f,根据path路径

File f = new File(path);

//如果当前不是一个目录就进入if

if (!f.isDirectory()) {

boolean success = f.mkdirs(); //创建一个目录

if (success) { //成功打印当前的路径

System.out.println("Created path: " + f.getPath());

} else { //失败的情况

System.out.println("Could not create path: " + f.getPath());

}

} else {

System.out.println("Path exists: " + f.getPath()); //子目录已存在。

}

关于mkdir:

mkdir()创建此抽象路径名称指定的目录(及只能创建一级的目录,且需要存在父目录),如果传入的path是多级路径,需要使用mkdirs()创建。

求java树形目录代码范例

给你一个。一共三个类。是个资源管理器的代码

// FileList.java

package tl.exercise.swing;

import java.awt.Color;

import java.awt.Component;

import javax.swing.JLabel;

import javax.swing.JList;

import javax.swing.ListCellRenderer;

import javax.swing.ListModel;

import javax.swing.event.ListDataListener;

public class FileList

extends JList {

// PathNode theNode;

FileListModel dataModel;

static final long serialVersionUID = 10;

public FileList() {

dataModel = new FileListModel();

setModel(dataModel);

this.setCellRenderer(new MyCellRenderer());

}

public void fireTreeSelectionChanged(I_fileSystem node) {

// Vector files = node.getFiles();

// theNode = node;

dataModel.setNode(node);

updateUI();

}

}

class FileListModel implements ListModel {

FileList theList;

I_fileSystem node;

char fileType = I_fileSystem.ALL;

public void setNode(I_fileSystem node) {

this.node = node;

}

public Object getElementAt(int index) {

if (node != null) {

return ((I_fileSystem) node).getChild(fileType, index);

} else {

return null;

}

}

public int getSize() {

if (node != null) {

return ((I_fileSystem) node).getChildCount(fileType);

} else {

return 0;

}

}

public void addListDataListener(ListDataListener l) {

}

public void removeListDataListener(ListDataListener l) {

}

}

class MyCellRenderer extends JLabel implements ListCellRenderer {

public MyCellRenderer() {

setOpaque(true);

}

public Component getListCellRendererComponent(

JList list,

Object value,

int index,

boolean isSelected,

boolean cellHasFocus)

{

FolderNode node = (FolderNode) value;

setIcon(node.getIcon());

setText(value.toString());

setBackground(isSelected ? Color.BLUE.darker().darker() : Color.WHITE);

setForeground(isSelected ? Color.WHITE : Color.BLACK);

return this;

}

}

package tl.exercise.swing;

//JExplorer.java

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.Toolkit;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JSplitPane;

import javax.swing.border.BevelBorder;

public class JExplorer {

public static void main(String[] args) {

// JFrame.setDefaultLookAndFeelDecorated(true);

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(new UI(frame));

frame.pack();

Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

int left = (screen.width - frame.getWidth()) / 2;

int top = (screen.height - frame.getHeight()) / 2;

frame.setLocation(left, top);

frame.setVisible(true);

}

}

class UI extends JPanel {

// implements I_menuHandler{

static final long serialVersionUID = 0l;

static int LEFT_WIDTH = 200;

static int RIGHT_WIDTH = 300;

static int WINDOW_HEIGHT = 300;

JFrame frame = null;

public UI(JFrame frame) {

// EmptyBorder eb = new EmptyBorder(1,1,1,1);

this.frame = frame;

setPreferredSize(new Dimension(800, 600));

setBorder(new BevelBorder(BevelBorder.LOWERED));

setLayout(new BorderLayout());

FileList list = new FileList();

FileTree tree = new FileTree(list);

tree.setDoubleBuffered(true);

list.setDoubleBuffered(true);

JScrollPane treeView = new JScrollPane(tree);

treeView.setPreferredSize(

new Dimension(LEFT_WIDTH, WINDOW_HEIGHT));

JScrollPane listView = new JScrollPane(list);

listView.setPreferredSize(

new Dimension(RIGHT_WIDTH, WINDOW_HEIGHT));

JSplitPane pane =

new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treeView,

listView);

pane.setDividerLocation(300);

pane.setDividerSize(4);

// pane.setDoubleBuffered(true);

add(pane);

}

}

package tl.exercise.swing;

//FileTree.java

/***********************************************************

* Author: Jason

* email: tl21cen@hotmail.com

* CSDN blog:

***********************************************************/

import java.awt.Component;

import java.io.File;

import java.util.Vector;

import javax.swing.Icon;

import javax.swing.JTree;

import javax.swing.event.TreeExpansionEvent;

import javax.swing.event.TreeExpansionListener;

import javax.swing.event.TreeModelListener;

import javax.swing.event.TreeSelectionEvent;

import javax.swing.event.TreeSelectionListener;

import javax.swing.filechooser.FileSystemView;

import javax.swing.tree.DefaultTreeCellRenderer;

import javax.swing.tree.TreeModel;

import javax.swing.tree.TreePath;

public class FileTree extends JTree {

static final long serialVersionUID = 0;

private FileList theList;

public FileTree(FileList list) {

theList = list;

setModel(new FileSystemModel(new FolderNode()));

this.setCellRenderer(new FolderRenderer());

addTreeSelectionListener(new TreeSelectionListener() {

public void valueChanged(TreeSelectionEvent tse) {

}

});

this.setSelectionRow(0);

}

public void fireValueChanged(TreeSelectionEvent tse) {

TreePath tp = tse.getNewLeadSelectionPath();

Object o = tp.getLastPathComponent();

// theList.fireTreeSelectionChanged((PathNode)o);

theList.fireTreeSelectionChanged((FolderNode) o);

}

public void fireTreeCollapsed(TreePath path) {

super.fireTreeCollapsed(path);

TreePath curpath = getSelectionPath();

if (path.isDescendant(curpath)) {

setSelectionPath(path);

}

}

public void fireTreeWillExpand(TreePath path) {

System.out.println("Path will expand is " + path);

}

public void fireTreeWillCollapse(TreePath path) {

System.out.println("Path will collapse is " + path);

}

class ExpansionListener implements TreeExpansionListener {

FileTree tree;

public ExpansionListener(FileTree ft) {

tree = ft;

}

public void treeCollapsed(TreeExpansionEvent tee) {

}

public void treeExpanded(TreeExpansionEvent tee) {

}

}

}

class FileSystemModel implements TreeModel {

I_fileSystem theRoot;

char fileType = I_fileSystem.DIRECTORY;

public FileSystemModel(I_fileSystem fs) {

theRoot = fs;

}

public Object getRoot() {

return theRoot;

}

public Object getChild(Object parent, int index) {

return ((I_fileSystem) parent).getChild(fileType, index);

}

public int getChildCount(Object parent) {

return ((I_fileSystem) parent).getChildCount(fileType);

}

public boolean isLeaf(Object node) {

return ((I_fileSystem) node).isLeaf(fileType);

}

public int getIndexOfChild(Object parent, Object child) {

return ((I_fileSystem) parent).getIndexOfChild(fileType, child);

}

public void valueForPathChanged(TreePath path, Object newValue) {

}

public void addTreeModelListener(TreeModelListener l) {

}

public void removeTreeModelListener(TreeModelListener l) {

}

}

interface I_fileSystem {

final public static char DIRECTORY = 'D';

final public static char FILE = 'F';

final public static char ALL = 'A';

public Icon getIcon();

public I_fileSystem getChild(char fileType, int index);

public int getChildCount(char fileType);

public boolean isLeaf(char fileType);

public int getIndexOfChild(char fileType, Object child);

}

/**

* A data model for a JTree. This model explorer windows file system directly.

*

* p

* Perhaps there is a fatal bug with this design. For speed, each of instances

* of this model contains file objects of subdirectory, up to now, there isn't

* any method to release them until program be end. I'm afraid that the memory

* would be full of if the file system is large enough and JVM memery size

* setted too small.

*

* p

* I won't pay more attention to solve it. it isn't goal of current a exercise.

*

* @author Jason

*/

class FolderNode implements I_fileSystem {

// private static FolderNode theRoot;

private static FileSystemView fsView;

private static boolean showHiden = true;;

private File theFile;

private VectorFile all = new VectorFile();

private VectorFile folder = new VectorFile();

/**

* set that whether apply hiden file.

*

* @param ifshow

*/

public void setShowHiden(boolean ifshow) {

showHiden = ifshow;

}

public Icon getIcon() {

return fsView.getSystemIcon(theFile);

}

public String toString() {

// return fsView.

return fsView.getSystemDisplayName(theFile);

}

/**

* create a root node. by default, it should be the DeskTop in window file

* system.

*

*/

public FolderNode() {

fsView = FileSystemView.getFileSystemView();

theFile = fsView.getHomeDirectory();

prepareChildren();

}

private void prepareChildren() {

File[] files = fsView.getFiles(theFile, showHiden);

for (int i = 0; i files.length; i++) {

all.add(files[i]);

if (files[i].isDirectory()

!files[i].toString().toLowerCase().endsWith(".lnk")) {

folder.add(files[i]);

}

}

}

private FolderNode(File file) {

theFile = file;

prepareChildren();

}

public FolderNode getChild(char fileType, int index) {

if (I_fileSystem.DIRECTORY == fileType) {

return new FolderNode(folder.get(index));

} else if (I_fileSystem.ALL == fileType) {

return new FolderNode(all.get(index));

} else if (I_fileSystem.FILE == fileType) {

return null;

} else {

return null;

}

}

public int getChildCount(char fileType) {

if (I_fileSystem.DIRECTORY == fileType) {

return folder.size();

} else if (I_fileSystem.ALL == fileType) {

return all.size();

} else if (I_fileSystem.FILE == fileType) {

return -1;

} else {

return -1;

}

}

public boolean isLeaf(char fileType) {

if (I_fileSystem.DIRECTORY == fileType) {

return folder.size() == 0;

} else if (I_fileSystem.ALL == fileType) {

return all.size() == 0;

} else if (I_fileSystem.FILE == fileType) {

return true;

} else {

return true;

}

}

public int getIndexOfChild(char fileType, Object child) {

if (child instanceof FolderNode) {

if (I_fileSystem.DIRECTORY == fileType) {

return folder.indexOf(((FolderNode) child).theFile);

} else if (I_fileSystem.ALL == fileType) {

return all.indexOf(((FolderNode) child).theFile);

} else if (I_fileSystem.FILE == fileType) {

return -1;

} else {

return -1;

}

} else {

return -1;

}

}

}

class FolderRenderer extends DefaultTreeCellRenderer {

private static final long serialVersionUID = 1L;

public Component getTreeCellRendererComponent(JTree tree, Object value,

boolean sel, boolean expanded, boolean leaf, int row,

boolean hasFocus) {

I_fileSystem node = (I_fileSystem) value;

Icon icon = node.getIcon();

setLeafIcon(icon);

setOpenIcon(icon);

setClosedIcon(icon);

return super.getTreeCellRendererComponent(tree, value, sel, expanded,

leaf, row, hasFocus);

}

}

用java写一个代码实现: 如何列出某个目录下的所有子目录

/**

* 列出文件夹下的子文件夹名

* @param localRoot

* @throws content

*/

public static void list(String localRoot) throws Exception {

File[] fs = new File(localRoot).listFiles();

if ((fs == null) || (fs.length = 0)) {

System.out.println("空文件夹");

return;

}

for (File f : fs) {

if (f.isDirectory()) {

System.out.println("目录:"+ f.getName());

}

}

}

建立个class然后见个main方法调用一下就可以了!


文章名称:在java中编目录的代码 在java中编目录的代码怎么写
本文地址:http://bzwzjz.com/article/dddecde.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 定制网站建设 成都网站建设 四川成都网站建设 成都网站建设公司 成都网站建设 成都网站建设 营销型网站建设 专业网站建设 品牌网站建设 手机网站建设套餐 外贸网站建设 重庆企业网站建设 响应式网站设计方案 成都网站设计 企业网站设计 成都定制网站建设 网站制作 重庆企业网站建设 成都网站设计 成都网站设计 定制级高端网站建设 高端品牌网站建设