java目录代码,Java项目目录-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

java目录代码,Java项目目录

何用Java代码列出一个目录下所有的文件

你好提问者:

成都创新互联是一家集网站建设,槐荫企业网站建设,槐荫品牌网站建设,网站定制,槐荫网站建设报价,网络营销,网络优化,槐荫网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

如果解决了你的问题,请采纳,若有疑问请追问,谢谢!

package com.zyx.cn.baidu_test;

import java.io.File;

public class GetListFile {

public static void main(String[] args) {

showFileName("C:\\Test");

}

private static void showFileName(String path) {

File file =new File(path);

if(file.exists()){//判断文件是否存在

if(file.isDirectory()){//判断file是否是文件夹

File[] listFiles =file.listFiles();//获取文件下的子文件

// 目录下文件

if(listFiles.length == 0){

System.out.println("该文件夹下没有文件");

}

for (File f : listFiles) {

if(f.isDirectory()){//判断file是否是文件夹

System.out.println("文件夹:"+f.getName());

showFileName(f.getAbsolutePath());//文件夹就继续遍历下的子文件

}else if(f.isFile()){

System.out.println("文件:"+f.getName());

}else {

System.err.println("未知错误");

}

}

}

}

}

}

结果:

文件:Test.docx

文件:test.txt

文件:Wb.java

结果:

文件夹:Hello

文件:world.txt

文件:Test.docx

文件:test.txt

文件:Wb.java

求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创建目录或文件夹的方法?

1、File类的createNewFile根据抽象路径创建一个新的空文件,当抽象路径制定的文件存在时,创建失败

2、File类的mkdir方法根据抽象路径创建目录

3、File类的mkdirs方法根据抽象路径创建目录,包括创建必需但不存在的父目录

4、File类的createTempFile方法创建临时文件,可以制定临时文件的文件名前缀、后缀及文件所在的目录,如果不指定目录,则存放在系统的临时文件夹下。

5、除mkdirs方法外,以上方法在创建文件和目录时,必须保证目标文件不存在,而且父目录存在,否则会创建失败

示例代码如下:

package book.io;

import java.io.File;

import java.io.IOException;

public class CreateFileUtil {

public static boolean createFile(String destFileName) {

File file = new File(destFileName);

if(file.exists()) {

System.out.println("创建单个文件" + destFileName + "失败,目标文件已存在!");

return false;

}

if (destFileName.endsWith(File.separator)) {

System.out.println("创建单个文件" + destFileName + "失败,目标文件不能为目录!");

return false;

}

//判断目标文件所在的目录是否存在

if(!file.getParentFile().exists()) {

//如果目标文件所在的目录不存在,则创建父目录

System.out.println("目标文件所在目录不存在,准备创建它!");

if(!file.getParentFile().mkdirs()) {

System.out.println("创建目标文件所在目录失败!");

return false;

}

}

//创建目标文件

try {

if (file.createNewFile()) {

System.out.println("创建单个文件" + destFileName + "成功!");

return true;

} else {

System.out.println("创建单个文件" + destFileName + "失败!");

return false;

}

} catch (IOException e) {

e.printStackTrace();

System.out.println("创建单个文件" + destFileName + "失败!" + e.getMessage());

return false;

}

}

public static boolean createDir(String destDirName) {

File dir = new File(destDirName);

if (dir.exists()) {

System.out.println("创建目录" + destDirName + "失败,目标目录已经存在");

return false;

}

if (!destDirName.endsWith(File.separator)) {

destDirName = destDirName + File.separator;

}

//创建目录

if (dir.mkdirs()) {

System.out.println("创建目录" + destDirName + "成功!");

return true;

} else {

System.out.println("创建目录" + destDirName + "失败!");

return false;

}

}

public static String createTempFile(String prefix, String suffix, String dirName) {

File tempFile = null;

if (dirName == null) {

try{

//在默认文件夹下创建临时文件

tempFile = File.createTempFile(prefix, suffix);

//返回临时文件的路径

return tempFile.getCanonicalPath();

} catch (IOException e) {

e.printStackTrace();

System.out.println("创建临时文件失败!" + e.getMessage());

return null;

}

} else {

File dir = new File(dirName);

//如果临时文件所在目录不存在,首先创建

if (!dir.exists()) {

if (!CreateFileUtil.createDir(dirName)) {

System.out.println("创建临时文件失败,不能创建临时文件所在的目录!");

return null;

}

}

try {

//在指定目录下创建临时文件

tempFile = File.createTempFile(prefix, suffix, dir);

return tempFile.getCanonicalPath();

} catch (IOException e) {

e.printStackTrace();

System.out.println("创建临时文件失败!" + e.getMessage());

return null;

}

}

}

public static void main(String[] args) {

//创建目录

String dirName = "D:/work/temp/temp0/temp1";

CreateFileUtil.createDir(dirName);

//创建文件

String fileName = dirName + "/temp2/tempFile.txt";

CreateFileUtil.createFile(fileName);

//创建临时文件

String prefix = "temp";

String suffix = ".txt";

for (int i = 0; i  10; i++) {

System.out.println("创建了临时文件:"

+ CreateFileUtil.createTempFile(prefix, suffix, dirName));

}

//在默认目录下创建临时文件

for (int i = 0; i  10; i++) {

System.out.println("在默认目录下创建了临时文件:"

+ CreateFileUtil.createTempFile(prefix, suffix, null));

}

}

}

输出结果:

创建目录D:/work/temp/temp0/temp1成功!

目标文件所在目录不存在,准备创建它!

创建单个文件D:/work/temp/temp0/temp1/temp2/tempFile.txt成功!

创建了临时文件:D:work emp emp0 emp1 emp5171.txt

创建了临时文件:D:work emp emp0 emp1 emp5172.txt

创建了临时文件:D:work emp emp0 emp1 emp5173.txt

创建了临时文件:D:work emp emp0 emp1 emp5174.txt

创建了临时文件:D:work emp emp0 emp1 emp5175.txt

创建了临时文件:D:work emp emp0 emp1 emp5176.txt

创建了临时文件:D:work emp emp0 emp1 emp5177.txt

创建了临时文件:D:work emp emp0 emp1 emp5178.txt

创建了临时文件:D:work emp emp0 emp1 emp5179.txt

创建了临时文件:D:work emp emp0 emp1 emp5180.txt

在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5181.txt

在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5182.txt

在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5183.txt

在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5184.txt

在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5185.txt

在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5186.txt

在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5187.txt

在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5188.txt

在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5189.txt

在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5190.txt

如何用JAVA代码创建一个文件夹?

File类里面有两个方法可以实现:\x0d\x0a一个是mkdir():创建此抽象路径名指定的目录。\x0d\x0a另外一个是mkdirs(): 创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。\x0d\x0a\x0d\x0a比如你想在A文件夹创建一个B文件夹,并在B文件夹下创建c和D文件夹,可以用下面的代码实现:\x0d\x0a\x0d\x0aimport java.io.File;\x0d\x0a\x0d\x0apublic class Test {\x0d\x0a public static void main(String args[]) {\x0d\x0a File file = new File("D:\\A\\B\\C");\x0d\x0a file.mkdirs();\x0d\x0a \x0d\x0a file = new File("D:\\A\\B\\D");\x0d\x0a file.mkdir();\x0d\x0a }\x0d\x0a}

java创建目录

贴上正确代码:你的代码本来没有什么问题的,但是只能够通过DOS下去运行,如果要在eclipse这些工具运行只能在arguments里面输入参数,谢谢采纳

import java.io.*;

import java.util.Scanner;

public class Exceptle13_1 {

public void newFolder(String newfolder) {

try {

String filepath = newfolder;

File myPath = new File(filepath);

if (!myPath.exists()) {

myPath.mkdir();

}

} catch (Exception e) {

System.out.println("新建目录存在");

e.printStackTrace();

}

}

public static void main(String[] args) {

String mynewpath = new Scanner(System.in).next();

System.out.println(mynewpath);

Exceptle13_1 createNewFolder = new Exceptle13_1();

createNewFolder.newFolder(mynewpath);

}

}


文章标题:java目录代码,Java项目目录
转载来源:http://kswsj.cn/article/dsseoop.html

其他资讯