📄 folder.java
字号:
import java.io.*;
//与文件有关的操作:
/**
* auther:Liyan
* 操作包括:创建目录、删除目录、获得上层目录名称、当前目录或文件大小
* 目录或文件的重命名等。
*/
public class Folder
{
String sFileName="";
File theFile;
//构造函数:获取文件或目录名称。
Folder(String tmpFile)
{
if(tmpFile!=null){
//tmpFile=CommonMethods.DealWithGBCodeOfRequest(tmpFile);
System.out.println("file name:"+tmpFile);
theFile = new File(tmpFile);
sFileName=tmpFile;
}
}
//判断是否是目录:
public boolean isDirection()
{
return theFile.isDirectory();
}
//创建新的目录:
public boolean mkDirection()
{
System.out.println("make dir...");
try{
if( theFile.mkdir() ){
System.out.println("ok to create:"+theFile.getName() );
return true;
}
else{
System.out.println("failed to create:"+theFile.getName() );
return false;
}
}catch(Exception ex){
System.out.println("failed to create:"+ex.toString() );
return false;
}
}
//重命名:
public boolean renameTo(String tmpFileName)
{
File tmpFile = new File(tmpFileName);
if(theFile.exists()==false)
return false;
try{
return theFile.renameTo(tmpFile);
}catch(Exception ex){
return false;
}
}
//删除目录或文件夹:
public boolean delete()
{
if(theFile.exists()==false) {
return false;
}
try{
return theFile.delete();
}catch(Exception ex){
return false;
}
}
//删除目录或文件夹(包括目录内的子目录和文件):
public static boolean deltree(String tmpName)
{
if(tmpName!=null){
File tmpFile1= new File(tmpName);
if(tmpFile1.isDirectory()){
String[] sList = tmpFile1.list();
if(sList==null){
try{
return tmpFile1.delete();
}catch(Exception ex){
tmpFile1=null;
return false;
}
}else{
for(int i=0;i<sList.length;i++)
if(deltree(tmpName+""+File.separatorChar +""+sList[i])==false)
return false;
}
}else{
if(tmpFile1.exists()==false) {
tmpFile1=null;
return true;
}
try{
tmpFile1.delete();
tmpFile1=null;
return true;
}catch(Exception ex){
tmpFile1=null;
return false;
}
}
if(tmpFile1.exists()==true)
try{
return tmpFile1.delete();
}catch(Exception ex){
return false;
}
}
return false;
}
//显示文件夹中的内容:
public String[] showFolder()
{
if(theFile.exists()==false) {
return null;
}
try{
return theFile.list();
}catch(Exception ex){
return null;
}
}
//显示用户文件夹中的内容:若不存在用户文件夹,则创建
public String[] showUserFolder()
{
if(theFile.exists()==false) {
theFile.mkdir();
return null;
}
try{
return theFile.list();
}catch(Exception ex){
return null;
}
}
//取得当前文件的大小:
public long getLength()
{
return theFile.length();
}
//获得上层目录名称:
public String getParent()
{
return theFile.getParent();
}
//获得目录或文件的名称:
public String getName()
{
try{
return theFile.getAbsolutePath();
}catch(Exception ex){
return "";
}
}
//获得当前目录名:
public String getPath()
{
try{
return theFile.getPath();
}catch(Exception ex){
return "";
}
}
//文件夹大小的确定:
public static long getFileLength(String sTmp)
{
long lLength=0;
File tmpFile = new File(sTmp);
if(tmpFile.isDirectory()){
String[] sArray = tmpFile.list();
if(sArray!=null)
for(int i=0;i<sArray.length;i++)
lLength+=getFileLength(sTmp+""+File.separatorChar +""+sArray[i]);
}else
lLength+=tmpFile.length();
return lLength;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -