java File问题

[复制链接]
查看11 | 回复1 | 2008-6-6 22:41:14 | 显示全部楼层 |阅读模式
import java.io.*;
public class MakeDirectores {

private static void usage() {

System.err.println("Useage:MakeDirectories path1...\n" +

"Creates each path\n" +

"Usage:MakeDirectories -d path1...\n" +

"Deletes each path\n" +

"Usage:MakeDirectories -r path1 path2\n" +

"Renames from path1 to path2");

System.exit(1);

}

private static void fileData(File f) {

System.out.println("Absolute path:" + f.getAbsolutePath() +

"\n Can read:" + f.canRead() +

"\n Can write" + f.canWrite() +

"\n getName:" + f.getName() +

"\n getParent:" + f.getParent() +

"\n getPath:" + f.getPath() +

"\n length:" + f.length() +

"n lastModified:" + f.lastModified());

if (f.isFile()) {

System.out.println("It's afile");

} else if (f.isDirectory()) {

System.out.println("It'a directory");

}

}

public static void main(String[] args) {

if (args.length < 1) {

usage();

}

if (args[0].equals("-r")) {

if (args.length != 3) {

usage();

}

File old = new File(args[1]),

rname = new File(args[2]);

old.renameTo(rname);

return;

}

int count = 0;

boolean del = false;

if (args[0].equals("-d")) {

count++;

del = true;

}

count--;

while (++count < args.length) {

File f = new File(args[count]);

if (f.exists()) {

System.out.println(f + "exists");

if (del) {

System.out.println("deleting..." + f);

f.delete();

}

} else {

if (!del) {

f.mkdirs();

System.out.println("created" + f);

}

}

fileData(f);

}

}
}
我这段代码要怎么样还要加点什么~才可以让我从键盘上输入参数然后创建目录以及其它的操作?我是初学的请说仔细一点谢谢了~!

回复

使用道具 举报

千问 | 2008-6-6 22:41:14 | 显示全部楼层
这个 程序 现在 cmd 底下 编译好后 ,操作如下:java MakeDirectores 显示:Useage:MakeDirectories path1...Creates each pathUsage:MakeDirectories -d path1...Deletes each pathUsage:MakeDirectories -r path1 path2Renames from path1 to path2原因:if (args.length < 1) { usage();}args.length<1就是无参数的时候 ,调用usage()方法 ,显示上面的内容。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行