** Math Game! Play Integers in Space!


A+    A−    B  
Home
Algebra
Math Games
  • Decimals in Space
  • Fraction Balls
  • Integers in Space
  • Math Man
  • Number Balls
  • Geometry
    Interactive
    Trigonometry
    Jobs
  • Tutoring jobs
  • New York Tutoring Jobs
  • White Plains, NY
  • Westchester County, NY
  • Chicago Math Jobs
  • Philadelphia
  • Teacher Resources
    On FaceBook!

    Recursively process folders

    A common real world use of recursion


    AP CS Home Page | AP CS AB Home Page | java.io.File at Sun Files for this project: 1) FileIndexer.java 2) FolderIndexer.java
    A very common use of recursion in the real world of programming involves recusively processing a folder that contains files and subfolders. You are probably familiar with 'folder tree's similar to the one pictured up above. Well, we're going to recursively process all files in a folder.

    Folders and Files in Java

    import java.io.*;
    • A File object can refer to a Folder or an actual file.
    • How to create a file object:
        File myDir = new File("C:/users/joe/myWork");
      • Note: The prior line does not actually create a file. Rather, all that you did in this line is create an object that represents a path to a folder that may or may not exist on your computer! Just because you decided to create a File object named myDir does not mean that this folder actually exists. Consider this analogy : It's the same as writing a newspaper article about a man named "Mr. Morris" (that's my name). Just becuase you wrote an article in a newspaper that "Mr. Morris went to the foodstore" does not mean that there is actually a real Mr. Morris or that he went to the foodstore. It's just data on a page (in this case a newspaper) that may or may not relate to the real world.
    Methods of File and related Class
    • System.getProperty("user.home")
      • This stores the folder of the current user such as
        C:\Users\joe smith
    The examples below will refer to the following File objects
    File myDir = new File("C:\users\jane smith\documents\myWork\);
    File myFile = new File("C:\users\jane smith\documents\abc.doc");
    
    
    • Constructor takes a String paramater
      • File myFile = new File("C:\Users\joe smith");
    • String getName()
        Method Call Return
        myDir.getName() myWork
        myFile.getName(); abc.doc
      • String getPath()
      • Method Call Return
        myFile.getPath() C:\users\jane smith\documents\abc.doc
        myDir.getPath(); C:\users\jane smith\documents\myWork
    • boolean exists() Returns true if the folder or file actually exists on the computer
      Useful methods for Directories and for this project in particular
    • listFiles()
      • returns an array of abstract path names (paths) denoting the directories and files inside a particular directory. Returns null if method is called by a file (see first example below myFile.listFiles() ).
      Method Call Return
      myFile.listFiles() null
      myDir.listFiles(); an array of abstract path names representing whatever files and folders are within myDir.

    Several self-descriptive methods
    • boolean isFile() self descriptive
    • boolean isDirectory() self-descriptive
    • boolean isHidden()
    • boolean canRead() : returns true if you are permitted to read the file referred to by the file object and false otherwise.
    Some of the methods up above are demonstrated in the following classFileDemo.java
    import java.io.*;
    public class FileDemo {
    
    public static void main(String[] args){
    	File myDir = new File("C:/users/jane smith/documents/myWork/");
    	File myFile = new File("C:/users/jane smith/documents/abc.doc");
    
    	System.out.println(myDir.getName());
    	System.out.println(myFile.getName());
    	System.out.println(myDir.getPath());
    	System.out.println(myFile.getPath());
    	}
    }
    
    
    Warm up Mini-project
    • Task #1) Find or create a folder in your H drive that has several files and folders in it.
    • Task #2) Write a class that
      • 1) Creates a file object that refers to the folder in task #1
      • 2) Prints out a list of all the files and folders within this folder
    Solution:

    The Project

    Files for this project: 1) FileIndexer.java 2) FolderIndexer.java
    Overview: I have created a class called FileIndexer.java that use a dialog box to select a folder on your hard drive and to display the path to the folder in a TextArea in a JFrame. ScreenShot:


    After you have hit "Open"...you will see a message like the one below displayed.

    Here's an example of what your final project should print out. In my code, I prepended a "-" before every directory to make the printout more readable. When I indexed my folder. I got the following print out.
    Requirements and Grading Guidelines
    AP Computer Science A AP Computer Science AB
      Requirements
    • Use of ArrayList
    • Use of some type of recursive method
      Requirements
    • Use of an Iterator
    • Use of the List interface (Not just by using an ArrayList)
      • One possibility is to declare an ArrayList whose reference type is List
      • ie
      • List<String> list = new ArrayList<String>();

    • Recursion
      • If you are shooting for an A on this assignment in the AB class you must create a true folder tree. In other words, each subfolder should be indented from its parent folder. See the image at the top of the screen

        You will get a B(all other things excluded) of some sort in the AB class if you produce a picture like the one below which does not indented nested subfolders.

    Some help for the AB class
    Since you'll probably want to convert an array to an ArrayList(listFiles() returns an array), here's some code to get you going.
    fileArrayList.java
    import java.io.File;
    import java.util.Arrays;
    import java.util.List;
    
    
    public class fileArrayList {
    
    	public fileArrayList(){
    		File[] filesInHomeDir = new File(System.getProperty("user.home")).listFiles();
    		List<File> wordList = Arrays.asList(filesInHomeDir);
    
    		for(File f: filesInHomeDir)
    			System.out.println(f);
    	}
    	
    	
    	public static void main(String[] args){
    	 fileArrayList p = new fileArrayList();
    	
    	}
    	
    }
    
    
    
    One Solution
    What A Final Project Might Look like

    Top