User:Andy Maloney/Notebook/Lab Notebook of Andy Maloney/2011/04/20/Tobacco seed movies

From OpenWetWare

Jump to: navigation, search

Movies

I have movies of seed growth of tobacco seeds in varying amounts of D2O. The movies are huge and long. They are about 20 minutes long and encompass 19 days worth of seed growth. In order to upload them to YouTube, I want to decrease the number of frames in the movie such that I still have the 19 days of growth except that the movie will only be 2 minutes long. In order to do that, I need to take out 10% of the frames I have now and re-encode the images to an avi.

I have a folder with all the images in it for one camera. I want to use Java to first list all the files in that folder.

Java list files in folder

This code seems to be popular on the interwebs so I just copied it from the Java Programming Forums. The most important point about copying this code, since I'm still a beginner, is to understand what the different parts of it does.

import java.io.File;
 
public class ListFiles {
	public static void main(String[] args) {
		String path = "/home/andy/Documents/TobaccoSeeds/Cam6/";
		String fileNames;
		File tempFile = new File(path);
		File[] listOfFileNames = tempFile.listFiles();
 
		for(int i = 0; i < listOfFileNames.length; i++) {
			if(listOfFileNames[i].isFile()) {
				fileNames = listOfFileNames[i].getName();
				System.out.println(fileNames);
			}
		}
 
	}
}

Okay, so the first line is

import java.io.File

which means I am importing a Java class file that deals with file names and directories that are OS independent. This is called an abstract file name or path since it doesn't have the usual C:/ or \\ in the path or file name.

The class starts out as usual, I have the typical

public class ListFiles {
	public static void main(String[] args) {

definition of the class I am writing in the code, i.e. ListFiles and I start the program using the main method. The next line defines a variable called path which is a String. I give the path variable a value namely, the path to the folder holding all the jpgs. I suppose that in the future when I figure it out, I will be able to add an argument to the main method that asks for the path when the program is run. Something to learn...

The next line declares another variable called fileNames that does not have a default value but it is a string. Since I want this program to list the file names of the files in the folder, I'm declaring a variable called fileNames that will be used in the println function to spit out the names of the files in the folder.

The next line is new to me.

File tempFile = new File(path);

Since I have imported the File class in this program, I can use the functions defined in it to do things in my program. It would appear that this line uses the class File to make a temporary file called tempFile in the default temporary directory of my OS. This file is usually empty but I am giving it values by stating

new File(path);

The temporary file is given values that are abstract file paths to the files in the folder given by path.

Okay, so this is what this program does so far.

  • I import the Java class File.
  • I define my public class ListFiles.
  • I start the program using the main method.
  • I declare a variable called path that is just the path to the folder that is containing my jpgs.
  • I initialize a temporary file called tempFile that contains the abstract path to all the files in the folder defined by path.

The next line is defining an array called listOfFileNames that is an array of abstract path names. listOfFileNames is given the values in tempFile in an array format.

I'm not entirely sure why one has to do this. It would seem that making a temporary file with the values in the file being the file names in the folder is a wasted step. But, removing it breaks the code. What I think is going on is that the code first has to generate the data in the tempFile (i.e. the file names). Once it has done that, I then have to index the data by putting it into an array. This is opposed to possibly making an array that appends data to it with the file names. I think that this is not the way one does things. I'm pretty sure you have to initialize an array with a specific size and this is why I had to dump the data into a temporary file.

The next bit is pretty straight forward. I want to print out all the file names in the folder. To do that, I run a loop over the length of the array listOfFileNames.

	for(int i = 0; i < listOfFileNames.length; i++) {
		if(listOfFileNames[i].isFile()) {
			fileNames = listOfFileNames[i].getName();
			System.out.println(fileNames);
		}
	}

Now, the if statement ensures that the listed files are only files and not directories, hence the use of isFile(). The getName() command does basically what it says, it gets the name of the file and defines it as the fileNames variable. This in turn is printed out with the println command.

Sorting the output

The next step I want to accomplish is sorting the output of the program. Right now I have the files named Cam6-0001.jpg - Cam6-42303.jpg which doesn't put the files in numerical order since I only have 3 leading zeros.

Wow. Sorting is not easy.

Personal tools