java中目录的代码 java目录操作

关于java代码中文件路径的问题

这就是相对路径

成都创新互联公司专注于企业成都全网营销推广、网站重做改版、乐都网站定制设计、自适应品牌网站建设、成都h5网站建设商城网站建设、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为乐都等各大城市提供网站开发制作服务。

指的是相对于工程文件的位置而言

在eclipse的结构图中的位置

在windows的文件夹里的位置

在查看属性里的绝对路径的位置

代码来找文件路径

public class Test {

public static void main(String[] args) throws Exception {

System.out.println("当前目录的路径\t"+new File(".").getCanonicalPath());// "."表示当前目录

File file = new File("Buffered.txt");

if(!file.exists()){//如果不存在,就新建该文件

file.createNewFile();

}

System.out.println("Buffered.txt的绝对路径\t"+file.getCanonicalPath());

System.out.println("Buffered.txt的相对路径\t"+file.getPath());

}

}

输出

当前目录的路径 D:\space\workspace\Demo

Buffered.txt的绝对路径 D:\space\workspace\Demo\Buffered.txt

Buffered.txt的相对路径 Buffered.txt

java tree目录

private DefaultMutableTreeNode root, red, green, blue;

private JTree jtree1;

private JPanel jpanel1;

private JFrame frame;

public yimin() {

// TODO Auto-generated constructor stub

super();

init();

}

public void init() {

frame = new JFrame("jtree");

root = new DefaultMutableTreeNode("Color");

red = new DefaultMutableTreeNode("red");

green = new DefaultMutableTreeNode("green");

blue = new DefaultMutableTreeNode("blue");

root.add(red);

red.add(blue);

root.add(green);

jtree1 = new JTree(root);

jpanel1 = new JPanel();

JSplitPane jsplitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,

jtree1, jpanel1);

jsplitpane.setOneTouchExpandable(true);

jsplitpane.setMinimumSize(new Dimension(100, 50));

frame.getContentPane().add(jsplitpane);

frame.setSize(600, 500);

frame.setLocation(50, 50);

frame.setVisible(true);

frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

}

采纳吧,给分吧、

java遍历一个目录,输出这个那些最少一个文件的那些目录的绝对路径,这道题如何用java代码解决?

我就不多说了,直接上代码吧:

/**

* java遍历一个目录,输出这个那些最少一个文件的那些目录的绝对路径,这道题如何用java代码解决?

*

* @param args

*/

public static void main(String[] args) {

// 设置文件目录, 设置为user.dir目录,便于测试

File folder = new File(System.getProperty("user.dir"));

System.out.println("根目录: " + folder.getAbsolutePath());

System.out.println("---------------------------------------------------------------------------");

displayAtLeastOneFileFolderPath(folder);

System.out.println("---------------------------------------------------------------------------");

}

/**

* 显示 有一个文件以上的文件目录

* @param file 文件或目录

*/

private static void displayAtLeastOneFileFolderPath(File file) {

if (file != null) {

if (file.isDirectory()) {   // 只有目录才处理

File[] files = file.listFiles();

int fileCount = 0; // 文件数量,即不是文件夹的数量

if (null != files  files.length  0) {

for (File subFile : files) {

if (subFile.isFile()) {

fileCount ++; // 文件数目加一

} else {

// 继续检查下面的文件夹

displayAtLeastOneFileFolderPath(subFile);

}

}

}

if (fileCount  0) {    // 说明有文件,需要显示文件夹的全路径

System.out.println(file.getAbsolutePath() + ": 共有文件 " + fileCount + " 个!");

}

}

}

}

在我机器上的运行结果为:


文章名称:java中目录的代码 java目录操作
文章位置:http://ybzwz.com/article/doigipj.html