Presentation is loading. Please wait.

Presentation is loading. Please wait.

Files. What are files? A series of data bits. a place for output a place to obtain input.

Similar presentations


Presentation on theme: "Files. What are files? A series of data bits. a place for output a place to obtain input."— Presentation transcript:

1 Files

2 What are files? A series of data bits. a place for output a place to obtain input

3 Where is a file stored? Typically on a hard disk. Not typically in ram…. Ram access time is in NS Disk access time is in MS RAM is 1 Million times faster than disk. But disk is cheaper/byte than ram. Cost-performance tradeoff.

4 What is the File class? A class in the java.io package Instances of the File class do not have to correspond to files in the OS. A File instance can correspond to a directory. Not directories will contain files! A File instance that points to a directory can list files!!

5 What is a fully-qualified file-name? directory+file name –/home/lyon/foo.java –c:\autoexec.bat –A string with a file separator –The file separator is OS dependent –for example: '\' = windows file sep, on mac it is ‘:’ '/' = unix file sep

6 How do you find out what your file separator is? You need to know! –In order to formulate fully-qualified file-names –In order to navigate through the file system utils.SystemUtils.getFileSeparator(); public static String getFileSeparator() { return System.getProperty(“file.separator"); }

7 What Is a Path Separator? On windows it is a “;”. for example: –PATH=c:\lyon\bin;c:\windows\bin –The path separator separates out fully-qualified paths! -Why do we need this? -To list multiple files or directories on the same line. -Eg. C:\ora9ias\bin;%SystemRoot%\system32;%SystemRoot%;%S ystemRoot%\System32\Wbem;C:\Program Files\Metrowerks\CodeWarrior\Other Metrowerks

8 How do I get the path separator? System.getProperty("path.separator"); or: –utils.SystemUtils.getPathSeparator();

9 How do I prompt the user for a directory? use futils.Futil.getDirFile public static void main(String args[]) { testGetReadDirFile(); } public static void testGetReadDirFile() { System.out.println(getReadDirFile("select a file")); }

10 How do I prompt the user for a file? public static void main(String[] args) { isSwing = false; System.out.println(getReadFile("select a file").toString()); isSwing = true; System.out.println(getReadFile("select a file").toString()); } Use File f = futils.Futil.getReadFile(“select a file”); File f = futils.Futil.getWriteFile(“select a file”); Both static methods.

11 How do we get the parent directory? public static File getReadDirFile(String prompt) { File f = Futil.getReadFile(prompt); return f.getParentFile(); }

12 Sample of the directory selector…

13 How do we get a read file? public static void testGetReadFile() { System.out.println( Futil.getReadFile( "select a file")); } public static void main(String args[]) { testGetReadFile(); }

14 What is a FileNameFilter? an interface in java.io requires an implementation of –boolean accept(File f, String name);

15 Why do I care about FileFilters? Lets me create a class of instances that will eliminate the files I don’t care about. Let me select, for example all files that end with “.java” or “.doc” or “.txt”. I can select only those files that are acceptable. The fileFilter implements the accept method

16 What good is a FileNameFilter? Enables control of how files are listed in a directory if f is an instance of the File class then –f.list();// files in a directory. –f.list(fnf); // uses fnf to list the files. –This is an example of polymorphism

17 What is the Ls class? class that reside in futils. ls is a unix command (like DIR).

18 How do I list The files in a directory? File f = new File(“c:”); File fa[] = f.list(); File fa1[] = f.list(fnf); Where fnf = file name filter.

19 How do I list all the files in all the subdirectories? A file system has a tree structure. The root might be “c:”. Listing all the files in all the subdirectories from the root list all the files in the file system.

20 Why list the files in subdirectories? To help search for files. To process all the files in the folder and subfolders. –Example: print all the files compile all the files delete all the files build a catalog.

21 How do I list all the files in all the subdirectories from a given root? private static void getSubdirectoryExample() { String suffix = In.getString("enter a suffix for a file list"); String prefix = In.getString("enter a prefix for a file list"); String midfix = In.getString("enter a midfix for a file list"); WildFilter fnf = new WildFilter(prefix, midfix, suffix); DirList d = new DirList(fnf); File f[] = d.getFiles(); if (f == null || f.length == 0) { In.message("no files found with "); return; } File aFile = (File) In.multiPrompt(f, "select a file", "file selection dialog"); In.message("you select:" + aFile); }


Download ppt "Files. What are files? A series of data bits. a place for output a place to obtain input."

Similar presentations


Ads by Google