lundi 28 avril 2014

want to delete least used shortcut from desktop and add to temp folder


Vote count:

0





package lastaccessfile;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.File;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Access {

public String[] files; // take icons/files/folders into array
public String[] files1;
public int total; // calculate total number of icons/files/folders
public String[] filename = new String[30]; // Store path of each icons/files/folders
public String[] filenameonly = new String[30]; // Store filename of each icons/files/folders
public int[] time = new int[30]; // Store created time of each icons/files/folders
public static int i = 0; // index variable
public int a;
public int option;

public void calculatTime(String dirPath, String[] files, int option) {
/* Calculate the creaed, access and modified time of each icons/files/folders and store
* this into an array
*/

if (files.length == 0) {
System.out.println("The directory is empty");
} else {
for (String aFile : files) {
System.out.println(aFile);
String pathOFIcon = dirPath.concat(aFile);
javaxt.io.File file = new javaxt.io.File(pathOFIcon);

filename[i] = pathOFIcon;
filenameonly[i] = aFile;
if (option == 1) {
System.out.println("Created: " + file.getCreationTime());
time[i] = calculateDiffBetDate(file.getCreationTime());
} else if (option == 2) {
System.out.println("Accessed: " + file.getLastAccessTime());
time[i] = calculateDiffBetDate(file.getLastAccessTime());
} else if (option == 3) {
System.out.println("Modified: " + file.getLastModifiedTime());
time[i] = calculateDiffBetDate(file.getLastModifiedTime());
} else {
System.out.println("Wrong Input");
}
i++;
}

}
}

public void print() {
/* Print each icons/files/folders along with
* time of creation
*/

for (int j = 0; j < total; j++) {
System.out.println("File names: " + filename[j]);
System.out.println("Days names: " + time[j]);
}
}

public void sort() {
/*Sort the array which store time of creation of each icons/files/folders
* using insertion sort
*/
int x, y;
for (x = 0; x < total; x++) {
for (y = x + 1; y < total; y++) {
if (time[x] < time[y]) {
int temp = time[x];
String tempfilename = filename[x];
String tempfilenameonly = filenameonly[x];
time[x] = time[y];
filename[x] = filename[y];
filenameonly[x] = filenameonly[y];
time[y] = temp;
filename[y] = tempfilename;
filenameonly[y] = tempfilenameonly;
}
}
}
}

public void move() {
/*Firt find the 1/3rd number of icons/files/folders from total, and move that much icons/files/folders
* into temp folder created into Document
*/
int newtotal = total / 3;
File f = new File("C:/Documents and Settings/Administrator/My Documents/temp");
f.mkdir();
if (filename.length == 0) {
System.out.println("There is no file on desktop ");
} else {
for (int m = 0; m < newtotal; m++) {
File source = new File(filename[m]);
File gg = new File(f, filenameonly[m]);
System.out.println("*******" + gg);
try {
boolean success = source.renameTo(gg);

} catch (SecurityException ex) {
System.out.println("Exception while Renaming file " + ex);
}
}
}


}

public int calculateDiffBetDate(Date TimeVariable) {
/* This function calculate the difference between todays date and created date of each icons/files/folders
*/
Date date = new Date();
SimpleDateFormat form = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
String dt1 = form.format(date);

String dt2 = form.format(TimeVariable);
String dateStart = dt2;//"01/14/2012 09:29:58";
String dateStop = dt1;//"01/15/2014 10:31:48";

Date d1 = null;
Date d2 = null;

try {
d1 = form.parse(dateStart);
d2 = form.parse(dateStop);

//in milliseconds
long diff = d2.getTime() - d1.getTime();

long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);

//diffArr[i]=(int)diffDays;

System.out.print(diffDays + "\n");
a = (int) diffDays;

/* System.out.print(diffHours + " hours, ");
System.out.print(diffMinutes + " minutes, ");
System.out.print(diffSeconds + " seconds.\n");*/

} catch (Exception e) {
e.printStackTrace();
}
return a;
}

public static void main(String args[]) {
Access a1 = new Access();

String dirPath = "C:/Documents and Settings/All Users/Desktop";
File dir = new File(dirPath);
a1.files = dir.list();
String dirPATH = "C:/Documents and Settings/All Users/Desktop/";

String dirPath1 = "C:/Documents and Settings/Administrator/Desktop";
File dir1 = new File(dirPath1);
a1.files1 = dir1.list();
String dirPATH1 = "C:/Documents and Settings/Administrator/Desktop/";
a1.total = a1.files.length + a1.files1.length;

//a1.total = a1.files.length;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Give your Option: ");
System.out.println("1: Creation Time ");
System.out.println("2: Last Access Time ");
System.out.println("3: Last Modified Time ");
try {
a1.option = Integer.parseInt(br.readLine());
} catch (Exception e) {
}
a1.calculatTime(dirPATH, a1.files, a1.option);
a1.calculatTime(dirPATH1, a1.files1, a1.option);
System.out.println();
a1.print();
System.out.println();
a1.sort();
System.out.println();
a1.print();
System.out.println();
a1.move();
}
}


error I have downloadeed it from net and it gives javaxt package error and the code logic is for last used according to me pls do modify whreeever neccessary and answer quickly javaxt.io.File file = new javaxt.io.File(pathOFIcon);-error



asked 36 secs ago






Aucun commentaire:

Enregistrer un commentaire