Java Video Tutorial 31

Java File ClassIn this part of the Java Video Tutorial, I completely cover the Java File Class.

I’ll show you how to create and delete files and directories. How to pull lists of files from directories and manipulate them. We’ll look at the errors to watch out for and a whole bunch more.

All of the code follows the video and should be used to help you understand this topic better.

If you like videos like this share it

Code from the Video


import javax.swing.*;

public class Lesson31 extends JFrame{
	static String filePath,parentDirectory;
	static File randomDir, randomFile, randomFile2;
	public static void main(String[] args){
		// Creates a File object in memory
		randomDir = new File("/Users/derekbanas/Documents/workspace3/Java Code/Random");
		// Make a directory
		// Make a file named random.txt
		randomFile = new File("random.txt");
		// Make a file and define where to save it in the file system
		randomFile2 = new File("/Users/derekbanas/Documents/workspace3/Java Code/Random/random2.txt");
		// createNewFile and getCanonicalPath have to be called in 
		// a try block to catch IOException
		try {
			// createNewFile creates the file in the file system
			// Returns the path for the file
			filePath = randomFile.getCanonicalPath();
		} catch (IOException e) {
			// You have to catch the IOException
		// Check to see if the file exists in the current directory
		if (randomFile.exists()){
			System.out.println("File Exists");
			System.out.println("File is readable: " + randomFile.canRead());
			System.out.println("File is writable: " + randomFile.canWrite());
			System.out.println("File location: " + filePath);
			System.out.println("File name: " + randomFile.getName());
			// Since you created the file without defining a path this returns null
			System.out.println("Parent directory: " + randomFile.getParent());
			// This returns the parent because it was defined
			parentDirectory = randomFile2.getParent();
			System.out.println("File Two Parent Directory: " + parentDirectory);
			System.out.println("Is this a directory: " + randomDir.isDirectory());
			// list provides a string array containing all the files
			String[] filesInDir = randomDir.list();
			System.out.println("Files in Random Directory\n");
			// Use the enhanced for loop to cycle through the files
			for(String fileName : filesInDir){
				System.out.println(fileName + "\n");
			System.out.println("Is this a file: " + randomFile.isFile());
			System.out.println("Is this hidden: " + randomFile.isHidden());
			// Milliseconds since Jan 1, 1970 when modified
			System.out.println("Last modified: " + randomFile.lastModified());
			// Return size of file
			System.out.println("Size of file: " + randomFile.length());
			// Changes the name of the file
			randomFile2.renameTo(new File("/Users/derekbanas/Documents/workspace3/Java Code/Random/random3.txt"));
			// Output the full path for the file unless the path wasn't
			// defined when the File was created
			System.out.println("New Name: " + randomFile2.toString());
			new Lesson31();
		} else {
			System.out.println("File Doesn't Exist");
		// You call delete to delete a file
			System.out.println("File Deleted");
		// I could get an array of File objects from the directory
		File[] filesInDir = randomDir.listFiles();
		for(File fileName : filesInDir){
		// You can only delete a directory if it is empty
			System.out.println("Directory Deleted");
	public Lesson31(){
		// Creates a file chooser at the location specified
		JFileChooser fileChooser = new JFileChooser(randomDir);
		// Opens the file chooser

4 Responses to “Java Video Tutorial 31”

  1. ben says:

    Very interesting tutorial once again. One question, if i wanted to create for example a 3d cube that could be moved by a user to see the different faces from whatever angle, possible in a way turn it into a menu, with different items on each face. What programming knowledge and languages do you think would be needed to achieve something like that? I know it’s a slightly random question but i appreciate any help! Thanks.

    • admin says:

      It would be very easy to do that with Flash, but the internet is moving away from Flash. I’ll cover Java OpenGL in future tutorials. It would allow you to do that. HTML5 can also be used for that, but every browser uses it differently. I have solutions using all of those technologies in upcoming tutorials.

  2. DarkAngelRev says:

    Hello… Thanks for another great tutorial.
    I’ve tried it out, and in the line where mkdir() is, i got an error… is this related for you being on a mac and me on Windows? can you tell me how to solve this, please.

    Keep up the good work, you’re tutorials are by far the best ones…

    • Derek Banas says:

      You’re very welcome 🙂 What was the error specifically? Writing into Program Files directory as a regular-user is by default blocked by UAC under Windows 7. So, you can either work in your documents folder and if that doesn’t work then run Windows as the administrator. I hope that helps

Leave a Reply

Your email address will not be published.