电脑技术学习

目录的递归遍历

dn001
内容: package io9;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import lang9.Str9;
import lang9.out;
//以上引入的lang9中的类很简单,读者可自行编写

public class File9 {
//list specified files int dir,include all the specified files in subdirectory.
// public static File[] listFiles(FileFilter9 filter, File dir) {
//listFilenames
public static String[] listFilenames(String dir) {
File[] ff = listFiles(new File(dir));
String[] ss = new String[ff.length];
for (int i = 0; i < ff.length; i++) {
ss[i] = ff[i].getAbsolutePath();
}
return ss;
}

//listfiles()
public static File[] listFiles() {
File dir = new File(System.getProperty("user.dir"));
return listFiles(dir);
}

//listdirs()
public static File[] listDirs() {
File dir = new File(System.getProperty("user.dir"));
return listDirs(dir);
}

//listfiles(dir)
public static File[] listFiles(File dir) {
if (!dir.exists() || !dir.isDirectory()) {
System.out.println("wrong File 23");
return null;
}

FileFilter9 filter = new FileFilter9(FileFilter9.FILE);
ArrayList array = list2ArrayList(filter, dir);
if (array == null) {
return null;
}
File[] files = new File[array.size()];
files = (File[]) array.toArray(files);
return files;
}

public static File[] listFiles(String pattern) {
return listFiles(pattern,new File(System.getProperty("user.dir")));
}

//listfiles(pattern,dir
public static File[] listFiles(String pattern, File dir) {
if (!dir.exists() || !dir.isDirectory()) {
System.out.println("wrong File 23");
return null;
}

FileFilter9 filter = new FileFilter9(pattern, FileFilter9.FILE);
ArrayList array = list2ArrayList(filter, dir);
if (array == null) {
return null;
}
File[] files = new File[array.size()];
files = (File[]) array.toArray(files);
return files;
}

public static File[] listDirs(File dir) {
if (!dir.exists() || !dir.isDirectory()) {
System.out.println("wrong File 23");
return null;
}

FileFilter9 filter = new FileFilter9(FileFilter9.DIR);
ArrayList array = list2ArrayList(filter, dir);
if (array == null) {
return null;
}
File[] files = new File[array.size()];
files = (File[]) array.toArray(files);
return files;
}

public static File[] listDirs(String pattern, File dir) {

if (!dir.exists() || !dir.isDirectory()) {
System.out.println("wrong File 23");
return null;
}
FileFilter filter = new FileFilter9(pattern, FileFilter9.DIR);
ArrayList array = list2ArrayList(filter, dir);
if (array == null) {
return null;
}
File[] files = new File[array.size()];
files = (File[]) array.toArray(files);
return files;
}

public static File[] list(File dir) {
if (!dir.exists() || !dir.isDirectory()) {
System.out.println("wrong File 24");
return null;
}

FileFilter filter = new FileFilter9(FileFilter9.BOTH);
ArrayList array = list2ArrayList(filter, dir);
if (array == null) {
return null;
}
File[] files = new File[array.size()];
files = (File[]) array.toArray(files);
return files;
}

public static File[] list(String pattern, File dir) {
if (!dir.exists() || !dir.isDirectory()) {
System.out.println("wrong File 25");
return null;
}

FileFilter filter = new FileFilter9(pattern, FileFilter9.BOTH);
ArrayList array = list2ArrayList(filter, dir);
if (array == null) {
return null;
}
File[] files = new File[array.size()];
files = (File[]) array.toArray(files);
return files;
}

public static ArrayList list2ArrayList(FileFilter filter, File dir) {
if (filter == null) {
filter = new FileFilter9(FileFilter9.BOTH);
}
if (dir.isFile()) {
System.out.println("wrong File9.11");
return null;
}
int i, j;
File[] specify, dirs; //specify means files or dirs specified by the invoker.
FileFilter9 filterDir = new FileFilter9(1);
ArrayList array = new ArrayList();
specify = dir.listFiles(filter);
dirs = dir.listFiles(filterDir);
if (specify != null) {
for (i = 0; i < specify.length; i++) {
array.add(specify[i]);
}
}
ArrayList array1;
if (dirs != null) {
for (i = 0; i < dirs.length; i++) {
array1 = File9.list2ArrayList(filter, dirs[i]);
if (array1 != null) {
array.addAll(array1);
}
}
}
return array;
}

public static String getRelativePath(File file, File dir) {
String sFile, sDir;
if (file == null) {
System.out.println("wrong,file9.33");
return null;
}
sFile = file.getAbsolutePath();
if (dir == null) {
return sFile;
}
sDir = dir.getParentFile().getAbsolutePath();
return Str9.replaceAll(sDir, "", sFile);
}

//firstName(;
public static String firstName(File f) {
int i = f.getName().lastIndexOf(".");
if (i > 0)
return f.getName().substring(0, i);
return null;
}

//lastName(;
public static String lastName(File f) {
int i = f.getName().lastIndexOf(".");
return f.getName().substring(i + 1);
}

public static String firstName(String s) {
return s.substring(0, s.lastIndexOf("."));
}

public static String lastName(String s) {
return s.substring(s.lastIndexOf(".") + 1);
}

//createNew()
public static File createNew() {
while (true) {
File f = new File(String.valueOf(Math.random()));
if (!f.exists())
return f;
}
}
public static File createNew(String pre) {
int i=1;
while (true) {
File f = new File(pre + i++);
if (!f.exists())
return f;
}
}

public static File oldest(String pattern) {
return oldest(pattern, new File(System.getProperty("user.dir")));
}

//oldest(pattern,dir)
public static File oldest(String pattern, File dir) {
File[] ff = listFiles(pattern, dir);
long l1 = Long.MAX_VALUE, l2;
int k = 0;
for (int i = 0; i < ff.length; i++) {
l2 = ff[i].lastModified();
if (l1 > l2) {
k = i;
l1 = l2;
}
}
return ff[k];
}

//exchange
public static boolean exchange(File f, File ff) {
return exchangeName(f, ff);
}

public static boolean exchangeName(File f, File ff) {
int i, j, k;
File f1;
do {
File f2 = new File(String.valueOf(Math.random()));
if (!f2.exists()) {
f1 = f2;
break;
}
}
while (true);
if (f.renameTo(f1)) {
if (ff.renameTo(f)) {
if (f1.renameTo(ff))
return true;
}
}
return false;
}
//rename
public static boolean rename(File d1,File d2){

File[] files=d1.listFiles();
if(d2.exists()) {
out.b("alreday exists!");
return false;
}
d2.mkdirs();
String d2s=d2.getPath();
for(int i=0;i if(!files[i].renameTo(new File(d2s+"\"+files[i].getName())))
return false;
}
return true;
}



}
Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd package io9;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import lang9.Str9;
import lang9.out;
//以上引入的lang9中的?

标签: