java读取文件名

importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.io.File;

publicclassReadFile{
publicReadFile(){
}
/**
*读取某个文件夹下的所有文件
*/
publicstaticbooleanreadfile(Stringfilepath)throwsFileNotFoundException,IOException{
try{

Filefile=newFile(filepath);
if(!file.isDirectory()){
System.out.println("文件");
System.out.println("path="+file.getPath());
System.out.println("absolutepath="+file.getAbsolutePath());
System.out.println("name="+file.getName());

}elseif(file.isDirectory()){
System.out.println("文件夹");
String[]filelist=file.list();
for(inti=0;i<filelist.length;i++){
Filereadfile=newFile(filepath+"\\"+filelist[i]);
if(!readfile.isDirectory()){
System.out.println("path="+readfile.getPath());
System.out.println("absolutepath="
+readfile.getAbsolutePath());
System.out.println("name="+readfile.getName());

}elseif(readfile.isDirectory()){
readfile(filepath+"\\"+filelist[i]);
}
}

}

}catch(FileNotFoundExceptione){
System.out.println("readfile()Exception:"+e.getMessage());
}
returntrue;
}

/**
*删除某个文件夹下的所有文件夹和文件
*/


/*publicstaticbooleandeletefile(Stringdelpath)
throwsFileNotFoundException,IOException{
try{

Filefile=newFile(delpath);
if(!file.isDirectory()){
System.out.println("1");
file.delete();
}elseif(file.isDirectory()){
System.out.println("2");
String[]filelist=file.list();
for(inti=0;i<filelist.length;i++){
Filedelfile=newFile(delpath+"\\"+filelist[i]);
if(!delfile.isDirectory()){
System.out.println("path="+delfile.getPath());
System.out.println("absolutepath="
+delfile.getAbsolutePath());
System.out.println("name="+delfile.getName());
delfile.delete();
System.out.println("删除文件成功");
}elseif(delfile.isDirectory()){
deletefile(delpath+"\\"+filelist[i]);
}
}
file.delete();

}

}catch(FileNotFoundExceptione){
System.out.println("deletefile()Exception:"+e.getMessage());
}
returntrue;
}*/

publicstaticvoidmain(String[]args){
try{
readfile("e:/videos");
//deletefile("D:/file");
}catch(FileNotFoundExceptionex){
}catch(IOExceptionex){
}
System.out.println("ok");
}

}

你可能感兴趣的:(java)