当前位置: 代码迷 >> J2SE >> 帮帮小弟我“急”
  详细解决方案

帮帮小弟我“急”

热度:200   发布时间:2016-04-24 15:16:58.0
帮帮我“急”
/**
  *查看文件数量
  */
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);
}
}

------解决方案--------------------
学了一手,谢谢拉
  相关解决方案