Import java.io.*;
import java.util.*;
/**
* <p>title: File operations </p>
* <p>description: Demo file deletion and access to file information </p>
* <p>copyright:copyright (c) 2003</p>
* <p>filename: </p>
* @version 1.0
*/
public class fileoperation{
/**
*<br> Method Description: Delete file
*<br> input parameter: String filename to be deleted
*<br> return Type: Boolean success is true
*/
public boolean delfile (String fileName) {
try{
//delete file
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> Method Description: Get file information
*<br> input parameter: String name file name
*<br> return type: string[] File information array
*/
public string[] GetFileInfo (String Name) {
try{
File File = new file (Name);
//Get the file modification date (a sentence returned)
Long modifiedtime = file.lastmodified ();
//Get file Length (unit: Bite)
Long filesize = File.length ();
//test file readable
Boolean CR = File.canread ();
//test file is writable
Boolean CW = File.canwrite ();
//test file is hidden
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> Method Description: Converts a millisecond number to a date
*<br> input parameter: Mill number of milliseconds
*<br> return type: String character format: Yyyy-mm-dd hh:mm
*/
public static String getdatestring (long Mill)
{
if (Mill < 0) return "";
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> Method Description: Main method
*<br> input Parameters:
*<br> return type:
*/
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;
}
}
}