Java Program to randomly select a movie and play which is not viewed recently
This program searches for mp4,avi or mkv files in your mentioned directory and opens a file, more than 4 months old randomly from the list!
You need a javaxt-core jar file... you may download it from: http://www.javaxt.com/downloads/
extract it and add it to your project's build path using:-
Right Click on your project -> Build Path -> Add Library -> User Library -> Click on User Libraries tab ->
Click 'New' tab -> give it a name and click 'OK' -> Click 'Add jars' tab -> Go to Location of your downloaded jar file which will be in the 'bin' folder of your download -> Click 'OK'.
That's it.. we are all set :)
/**
*
*/
package main;
/**
* @author sundaram
*
*/import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Random;
import javaxt.utils.Date;
/**
* This class will handle the gathering of all files from a particular directory
*
* @author SwiftStriker00
*/
public class AllFiles {
/**
* This will retrieve a list of all files from current
* folder and all sub folders
*
* @param rootDir - path of the starting directory
* @return A list of Files
*/
public static ArrayList<File> retriveAllFiles( String rootDir){
ArrayList<File> tempList = new ArrayList<File>();
File theRootDir = new File(rootDir);
//Retrieve all folders (current and any sub)
ArrayList<File> folders = getAllDir(theRootDir);
tempList.addAll(retriveFiles(rootDir));
//For all of the folders
for (int i = 0; i < folders.size(); i++) {
//Get all the files in the folder
File[] currentFile = folders.get(i).listFiles();
if(currentFile!=null)
for (int k = 0; k < currentFile.length; k++) {
//If the current file isn't a directory
//Add it to the list of all files
if( currentFile[k].isDirectory() == false
&& (currentFile[k].getName().endsWith(".avi") || currentFile[k].getName().endsWith(".mkv") || currentFile[k].getName().endsWith(".mp4"))){
tempList.add(currentFile[k]);
}
}
}
return tempList;
}
/**
* This will retrieve a list of files from only the current
* directory
*
* @param rootDir - path of the starting directory
* @return A list of Files
*/
public static ArrayList<File> retriveFiles (String rootDir){
File f = new File( rootDir );
ArrayList<File> temp = new ArrayList<File>();
File[] currentFile = f.listFiles();
if(currentFile!=null)
for (int k = 0; k < currentFile.length; k++) {
//If the current file isn't a directory
//Add it to the list of all files
if( currentFile[k].isDirectory() == false
&& (currentFile[k].getName().endsWith(".avi") || currentFile[k].getName().endsWith(".mkv") || currentFile[k].getName().endsWith(".mp4"))){
temp.add(currentFile[k]);
}
}
return temp;
}
/**
* Will iterate through all directories gathering all folders & sub folders
*
* @param rootURL - starting File
* @return A list of ALL folders inside of the root URL
*/
private static ArrayList<File> getAllDir(File rootURL) {
ArrayList<File> temp = new ArrayList<File>(), //This will hold our queued folders
fill = new ArrayList<File>(), //List of end results
subs = new ArrayList<File>(); //Sub folders
//Add our initial to start search (Breadth First Search)
temp.add(rootURL);
while (!temp.isEmpty()) {
//Dequeue Folder
File next = temp.remove(0);
//Add it to the return list if not done so already and not blank
if (!fill.contains(next) && !next.getAbsolutePath().equals("")) {
fill.add(next);
}
//Get sub folders
if(next.isDirectory()){
fill.addAll(retriveFiles(next.getName()));
subs = getSubs(next);
}
//for each folder, add it to temp if not done so already
for (File s : subs) {
if (!temp.contains(s)) {
temp.add(s);
}
}
//clear for next iteration
subs.clear();
}
return fill;
}
/**
* This method will retrieve all the sub folders from the current directory
* that was passed in
* @param cur - Current directory that the user is in
* @return A list of folders
*/
private static ArrayList<File> getSubs(File cur) {
//Get a list of all the files in folder
ArrayList<File> temp = new ArrayList<File>();
File[] fileList = cur.listFiles();
//for each file in the folder
if(fileList!=null)
for (int i = 0; i < fileList.length; i++) {
//If the file is a Directory(folder) add it to return, if not done so already
File choose = fileList[i];
if ( choose.isDirectory() && !temp.contains(choose)) {
temp.add(choose);
}
}
return temp;
}
/**
* Main method of program this program will find files for you
*
* @param args first and only argument is the path you wish to start
*/
public static void main(String[] args) {
//Directory Name to search for files, here it's 'G' drive
String startingPath = "G:\\";
ArrayList<File> newList = AllFiles.retriveAllFiles(startingPath);
ArrayList<File> finalList = new ArrayList<File>();
Date d = new Date();
Calendar c = new GregorianCalendar();
//This program searches for files Last Accessed 4 months ago, you many change it.
c.set(d.getYear(), d.getMonth()-4, d.getDay());
for(File f: newList){
javaxt.io.File file = new javaxt.io.File(f.getPath());
if(file.getLastAccessTime().before(c.getTime())){
finalList.add(f);
}
}
System.out.println(finalList.size()+" "+newList.size());
//Randomly select a file from the list
Random g = new Random();
int r = Math.abs(g.nextInt());
r = r % finalList.size();
File file = new File(finalList.get(r).getAbsolutePath());
try {
Desktop.getDesktop().open(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
You need a javaxt-core jar file... you may download it from: http://www.javaxt.com/downloads/
extract it and add it to your project's build path using:-
Right Click on your project -> Build Path -> Add Library -> User Library -> Click on User Libraries tab ->
Click 'New' tab -> give it a name and click 'OK' -> Click 'Add jars' tab -> Go to Location of your downloaded jar file which will be in the 'bin' folder of your download -> Click 'OK'.
That's it.. we are all set :)
/**
*
*/
package main;
/**
* @author sundaram
*
*/import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Random;
import javaxt.utils.Date;
/**
* This class will handle the gathering of all files from a particular directory
*
* @author SwiftStriker00
*/
public class AllFiles {
/**
* This will retrieve a list of all files from current
* folder and all sub folders
*
* @param rootDir - path of the starting directory
* @return A list of Files
*/
public static ArrayList<File> retriveAllFiles( String rootDir){
ArrayList<File> tempList = new ArrayList<File>();
File theRootDir = new File(rootDir);
//Retrieve all folders (current and any sub)
ArrayList<File> folders = getAllDir(theRootDir);
tempList.addAll(retriveFiles(rootDir));
//For all of the folders
for (int i = 0; i < folders.size(); i++) {
//Get all the files in the folder
File[] currentFile = folders.get(i).listFiles();
if(currentFile!=null)
for (int k = 0; k < currentFile.length; k++) {
//If the current file isn't a directory
//Add it to the list of all files
if( currentFile[k].isDirectory() == false
&& (currentFile[k].getName().endsWith(".avi") || currentFile[k].getName().endsWith(".mkv") || currentFile[k].getName().endsWith(".mp4"))){
tempList.add(currentFile[k]);
}
}
}
return tempList;
}
/**
* This will retrieve a list of files from only the current
* directory
*
* @param rootDir - path of the starting directory
* @return A list of Files
*/
public static ArrayList<File> retriveFiles (String rootDir){
File f = new File( rootDir );
ArrayList<File> temp = new ArrayList<File>();
File[] currentFile = f.listFiles();
if(currentFile!=null)
for (int k = 0; k < currentFile.length; k++) {
//If the current file isn't a directory
//Add it to the list of all files
if( currentFile[k].isDirectory() == false
&& (currentFile[k].getName().endsWith(".avi") || currentFile[k].getName().endsWith(".mkv") || currentFile[k].getName().endsWith(".mp4"))){
temp.add(currentFile[k]);
}
}
return temp;
}
/**
* Will iterate through all directories gathering all folders & sub folders
*
* @param rootURL - starting File
* @return A list of ALL folders inside of the root URL
*/
private static ArrayList<File> getAllDir(File rootURL) {
ArrayList<File> temp = new ArrayList<File>(), //This will hold our queued folders
fill = new ArrayList<File>(), //List of end results
subs = new ArrayList<File>(); //Sub folders
//Add our initial to start search (Breadth First Search)
temp.add(rootURL);
while (!temp.isEmpty()) {
//Dequeue Folder
File next = temp.remove(0);
//Add it to the return list if not done so already and not blank
if (!fill.contains(next) && !next.getAbsolutePath().equals("")) {
fill.add(next);
}
//Get sub folders
if(next.isDirectory()){
fill.addAll(retriveFiles(next.getName()));
subs = getSubs(next);
}
//for each folder, add it to temp if not done so already
for (File s : subs) {
if (!temp.contains(s)) {
temp.add(s);
}
}
//clear for next iteration
subs.clear();
}
return fill;
}
/**
* This method will retrieve all the sub folders from the current directory
* that was passed in
* @param cur - Current directory that the user is in
* @return A list of folders
*/
private static ArrayList<File> getSubs(File cur) {
//Get a list of all the files in folder
ArrayList<File> temp = new ArrayList<File>();
File[] fileList = cur.listFiles();
//for each file in the folder
if(fileList!=null)
for (int i = 0; i < fileList.length; i++) {
//If the file is a Directory(folder) add it to return, if not done so already
File choose = fileList[i];
if ( choose.isDirectory() && !temp.contains(choose)) {
temp.add(choose);
}
}
return temp;
}
/**
* Main method of program this program will find files for you
*
* @param args first and only argument is the path you wish to start
*/
public static void main(String[] args) {
//Directory Name to search for files, here it's 'G' drive
String startingPath = "G:\\";
ArrayList<File> newList = AllFiles.retriveAllFiles(startingPath);
ArrayList<File> finalList = new ArrayList<File>();
Date d = new Date();
Calendar c = new GregorianCalendar();
//This program searches for files Last Accessed 4 months ago, you many change it.
c.set(d.getYear(), d.getMonth()-4, d.getDay());
for(File f: newList){
javaxt.io.File file = new javaxt.io.File(f.getPath());
if(file.getLastAccessTime().before(c.getTime())){
finalList.add(f);
}
}
System.out.println(finalList.size()+" "+newList.size());
//Randomly select a file from the list
Random g = new Random();
int r = Math.abs(g.nextInt());
r = r % finalList.size();
File file = new File(finalList.get(r).getAbsolutePath());
try {
Desktop.getDesktop().open(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Comments
Post a Comment