Java 简单小程序 急!!!
利用I/O类库,编写一个dir命令,要求:不输入命令行参数显示当前文件夹的所有文件,输入命令行参数则显示当前文件夹中的制定文件类型。如:输入java显示所有扩展名为。ja...
利用I/O类库,编写一个dir命令,要求:不输入命令行参数显示当前文件夹的
所有文件,输入命令行参数则显示当前文件夹中的制定文件类型。如:输入java
显示所有扩展名为。java的文件。 展开
所有文件,输入命令行参数则显示当前文件夹中的制定文件类型。如:输入java
显示所有扩展名为。java的文件。 展开
2个回答
展开全部
import java.io.File;
import java.io.FilenameFilter;
import java.util.Scanner;
public class IoFilter {
private static Scanner san = new Scanner(System.in);
public static File[] doFilter(File dist, String tail) {
FilenameFilter filter = new JavaFileFilter(tail);
File[] fs = dist.listFiles(filter);
return fs;
}
public IoFilter() {
}
public static void list(File [] fs) {
if(fs == null) {
return;
}
for(File f: fs) {
System.out.println(f.getName());
}
}
public static void main(String[] args) {
String path = ".";
System.out.print("Input dist path: ");
path = san.nextLine();
File dist = new File(path);
System.out.print("filter(input dir *): ");
String input = san.nextLine();
input = input.trim();
if(!input.matches("dir.*")) {
System.err.println("Error Command!");
System.exit(-1);
return;
} else if(input.equals("dir")){
IoFilter.list(IoFilter.doFilter(dist, null));
return;
} else {
String tail = null;
tail = input.replaceFirst("dir\\s", "");
IoFilter.list(IoFilter.doFilter(dist, tail));
return;
}
}
}
class JavaFileFilter implements FilenameFilter {
private String tail = ".*";
private String filter = ".*\\.";
public JavaFileFilter(String tail) {
this.tail = tail;
}
public boolean accept(File dir, String name) {
if (this.tail == null) {
this.filter = ".*";
} else {
this.filter = ".*\\." + this.tail;
}
return name.matches(this.filter);
}
}
import java.io.FilenameFilter;
import java.util.Scanner;
public class IoFilter {
private static Scanner san = new Scanner(System.in);
public static File[] doFilter(File dist, String tail) {
FilenameFilter filter = new JavaFileFilter(tail);
File[] fs = dist.listFiles(filter);
return fs;
}
public IoFilter() {
}
public static void list(File [] fs) {
if(fs == null) {
return;
}
for(File f: fs) {
System.out.println(f.getName());
}
}
public static void main(String[] args) {
String path = ".";
System.out.print("Input dist path: ");
path = san.nextLine();
File dist = new File(path);
System.out.print("filter(input dir *): ");
String input = san.nextLine();
input = input.trim();
if(!input.matches("dir.*")) {
System.err.println("Error Command!");
System.exit(-1);
return;
} else if(input.equals("dir")){
IoFilter.list(IoFilter.doFilter(dist, null));
return;
} else {
String tail = null;
tail = input.replaceFirst("dir\\s", "");
IoFilter.list(IoFilter.doFilter(dist, tail));
return;
}
}
}
class JavaFileFilter implements FilenameFilter {
private String tail = ".*";
private String filter = ".*\\.";
public JavaFileFilter(String tail) {
this.tail = tail;
}
public boolean accept(File dir, String name) {
if (this.tail == null) {
this.filter = ".*";
} else {
this.filter = ".*\\." + this.tail;
}
return name.matches(this.filter);
}
}
展开全部
import java.io.File;
import java.io.FileFilter;
import java.util.Date;
public class Dir{
public static void main(final String[] args){
File[] subItems;
if(args.length==0){
subItems=new File(".").listFiles();
}
else{
subItems=new File(".").listFiles(new FileFilter(){
public boolean accept(File f){
return f.getName().toLowerCase().endsWith("."+args[0].toLowerCase());
}
});
}
for(int i=0;i<subItems.length;i++){
System.out.println(String.format("%1$-28s\t%2$12s\t%3$s",subItems[i].getName(),subItems[i].isDirectory()?"<DIR>":String.valueOf(subItems[i].length()),new Date(subItems[i].lastModified()).toLocaleString()));
}
}
}
import java.io.FileFilter;
import java.util.Date;
public class Dir{
public static void main(final String[] args){
File[] subItems;
if(args.length==0){
subItems=new File(".").listFiles();
}
else{
subItems=new File(".").listFiles(new FileFilter(){
public boolean accept(File f){
return f.getName().toLowerCase().endsWith("."+args[0].toLowerCase());
}
});
}
for(int i=0;i<subItems.length;i++){
System.out.println(String.format("%1$-28s\t%2$12s\t%3$s",subItems[i].getName(),subItems[i].isDirectory()?"<DIR>":String.valueOf(subItems[i].length()),new Date(subItems[i].lastModified()).toLocaleString()));
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询