import java.io.*;
import java.util.*;
/**
* <p>title: 文件操作</p>
* <p>description: 演示文件的删除和获取文件的信息</p>
* <p>copyright: copyright (c) 2003</p>
* <p>filename: </p>
* @version 1.0
*/
public class fileoperation{
/**
*<br>方法说明:删除文件
*<br>输入参数:string filename 要删除的文件名
*<br>返回类型:boolean 成功为true
*/
public boolean delfile(string filename){
try{
//删除文件
boolean success = (new file(filename)).delete();
if (!success) {
system.out.println("delete file error!");
return false;
}else{
return true;
}
}catch(exception e){
system.out.println(e);
return false;
}
}
/**
*<br>方法说明:获取文件信息
*<br>输入参数:string name 文件名
*<br>返回类型:string[] 文件信息数组
*/
public string[] getfileinfo(string name){
try{
file file = new file(name);
//获取文件修改日期(返回的是句)
long modifiedtime = file.lastmodified();
//获取文件长度(单位:bite)
long filesize = file.length();
//测试文件是否可读
boolean cr = file.canread();
//测试文件是否可写
boolean cw = file.canwrite();
//测试文件是否隐藏
boolean ih = file.ishidden();
string[] stemp = new string[6];
stemp[0] = string.valueof(filesize);
stemp[1] = getdatestring(modifiedtime);
stemp[2] = string.valueof(cr);
stemp[3] = string.valueof(cw);
stemp[4] = string.valueof(ih);
stemp[5] = string.valueof(file.getcanonicalpath());
return stemp;
}catch(exception e){
system.out.println(e);
return null;
}
}
/**
*<br>方法说明:将毫秒数字转换为日期
*<br>输入参数:mill 毫秒数
*<br>返回类型:string 字符 格式为:yyyy-mm-dd hh:mm
*/
public static string getdatestring(long mill)
{
if(mill < 0) return "";
date date = new date(mill);
calendar rightnow = calendar.getinstance();
rightnow.settime(date);
int year = rightnow.get(calendar.year);
int month = rightnow.get(calendar.month);
int day = rightnow.get(calendar.day_of_month);
int hour = rightnow.get(calendar.hour_of_day);
int min = rightnow.get(calendar.minute);
return year + "-" + (month <10 ? "0" + month : "" + month) + "-"
+ (day <10 ? "0" + day : "" + day)
+ (hour <10 ? "0" + hour : "" + hour)+":"
+ (min <10 ? "0" + min : "" + min);
}
/**
*<br>方法说明:主方法
*<br>输入参数:
*<br>返回类型:
*/
public static void main(string[] args){
try{
fileoperation fo = new fileoperation();
if(args.length==0){
return;
}else{
string cmd = args[0];
if(cmd.equals("del")){
boolean bdel = fo.delfile(args[1]);
system.out.println(bdel);
}else if(cmd.equals("info")){
string[] stemp = fo.getfileinfo(args[1]);
for(int i=0;i<stemp.length;i++)
system.out.println(stemp[i]);
}
}
}catch(exception e){
return;
}
}
}
闽公网安备 35060202000074号