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

21
Files

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

Files

What are files?

• A series of data bits.

• a place for output

• a place to obtain input

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.

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!!

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

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");• }

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%;%SystemRoot%\System32\Wbem;C:\Program Files\Metrowerks\CodeWarrior\Other Metrowerks

How do I get the path separator?

• System.getProperty("path.separator");

• or:– utils.SystemUtils.getPathSeparator();

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"));

}

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.

How do we get the parent directory?

public static File getReadDirFile(String prompt) {

File f = Futil.getReadFile(prompt);

return f.getParentFile();

}

Sample of the directory selector…

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();

}

What is a FileNameFilter?

• an interface in java.io

• requires an implementation of – boolean accept(File f, String name);

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

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

What is the Ls class?

• class that reside in futils.

• ls is a unix command (like DIR).

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.

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.

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.

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);• }