/**
*查看文件数量
*/
import java.lang.*;
import java.io.*;
public class Test{
static int tot;
public static void main(String []args){
Test fileTest = new Test();
try{
String path = args[0];
File file = new File(path);
fileTest.doDir(file);
String total = "文件数是 : "+tot;
System.out.println(total);
}catch(Exception err){
}
}
public void doDir (File file){
try{
File []files = file.listFiles();
for(int i=0; i <files.length;i++){
if(files[i].isDirectory()){
doDir(files[i]);
FileWriter writer=new FileWriter( "Data.txt ");
writer.write(total,0,files.length);
writer.flush();
}else{
doFile(files[i]);
tot++;
}
}
}catch(Exception error){
}
}
public void doFile(File file){
String path = file.getPath();
System.out.println(path);
}
}
总是有一个错误
------解决方案--------------------
doDir()里total未定义
------解决方案--------------------
给楼主修改过了。测试通过
import java.lang.*;
import java.io.*;
public class Test{
static int tot;
public static void main(String []args){
Test fileTest = new Test();
try{
String path = args[0];
File file = new File(path);
fileTest.doDir(file);
String total = "文件数是 : "+tot;
System.out.println(total);
}catch(Exception err){
}
}
public void doDir (File file){
try{
String total = "success ";
File []files = file.listFiles();
for(int i=0; i <files.length;i++){
if(files[i].isDirectory()){
doDir(files[i]);
FileWriter writer=new FileWriter( "Data.txt ");
writer.write(total,0,files.length);
writer.flush();
}else{
doFile(files[i]);
tot++;
}
}
}catch(Exception error){
}
}
public void doFile(File file){
String path = file.getPath();
System.out.println(path);
}
}
------解决方案--------------------
学了一手,谢谢拉