Unformatted text preview:

Slide 1OutlineSlide 317.1 IntroductionSlide 5Text vs Binary FilesSlide 7Examples of Basic File ProcessingSlide 9The java.io packageSlide 11Slide 12The File ClassSlide 14Slide 15Slide 16Slide 17Slide 18Slide 19Files and Streams1-Based on slides from Deitel & Associates, Inc.- Revised by T. A. YangOutline•Streams•Files, Text files vs Binary files •Related Java classes: java.io.FileReader (for reading streams of characters)java.io.FileWriter (for writing streams of characters)java.io.File (for handling file attributes, folders, etc.)java.io.InputStream (for reading streams of raw bytes)java.io.FileInputStreamjava.io.ObjectInputStreamjava.io.OutputStream (for writing streams of raw bytes)java.io.FileOutputStreamjava.io.ObjectOutputStream 23417.1Introduction•Why use files?−Data stored in variables and arrays are temporary (in memory)−For long-term retention of data, computers use files. −Computers store files on secondary storage devices −hard disks, optical disks, flash drives and magnetic tapes. −Data maintained in files are persistent data because they exist beyond the duration of program execution.5•Java views each file as a sequential stream of bytes. •Every operating system provides a mechanism to determine the end of a file, such as an end-of-file marker or a count of the total bytes in the file. A Java program simply receives an indication from the operating system when it reaches the end of the stream. (system independent)•How about objects in your program? Can they be saved in files and retrieved later?6•File streams can be used to input and output data as bytes or characters. •Streams that input and output characters are known as character-based streams, representing data as a sequence of characters. −Files created using character-based streams are referred to as text files. Text files can be read by text editors. •Streams that input and output bytes are known as byte-based streams, representing data in its binary format. −Files that are created using byte-based streams are referred to as binary files.−Binary files are read by programs that understand the specific content of the file and the ordering of that content.Text vs Binary Files7•Java programs perform file processing by using classes from package java.io. –FileReader (for character-based input from a file)–FileWriter (for character-based output to a file)–FileInputStream (for byte-based input from a file) –FileOutputStream (for byte-based output to a file)•You open a file by creating an object of one of these stream classes. The object’s constructor opens the file.8ExamplesofBasicFileProcessing•Example Java applications by topics: http://sce.uhcl.edu/yang/teaching/JavaProgrammingExamplesandRelatedTopics.htm FileReaderTest // Demonstration of reading input from a data file using FileReader classFileInputStreamExample // Demonstration of reading input from a data file using FileInputStream classScanning a data file using the Scanner classReading a data file into an array of bytes and printing words by words on console output //based on FileReaderTest9•A Java program opens a file by creating an object and associating a stream of bytes or characters with it.e.g., FileInputStream fStream = new FileInputStream(args[0]);-Can also associate streams with different devices. e.g., network cards, game controllers, mouse, …•Java creates three stream objects when a program begins executing:–System.in (the standard input stream object) normally inputs bytes from the keyboard–System.out (the standard output stream object) normally outputs character data to the screen–System.err (the standard error stream object) normally outputs character-based error messages to the screen.10Thejava.io package•Pre-defined classes support input and output of various types of data:−Primitive data−Objects•To perform such input and output operations, objects of classes ObjectInputStream and ObjectOutputStream can be used together with the byte-based file stream classes FileInputStream and FileOutputStream. •The complete hierarchy of classes in package java.io can be viewed in the online documentation at http://download.oracle.com/javase/6/docs/api/java/io/package-tree.html11• For example, to write an object that can be read later: FileOutputStream fos = new FileOutputStream("t.tmp"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeInt(12345); oos.writeObject("Today"); oos.writeObject(new Date()); oos.close(); Src: http://download.oracle.com/javase/6/docs/api/java/io/ObjectOutputStream.html • To read the object saved in t.tmp: FileInputStream fis = new FileInputStream("t.tmp"); ObjectInputStream ois = new ObjectInputStream(fis); int i = ois.readInt(); String today = (String) ois.readObject(); Date date = (Date) ois.readObject(); ois.close();Src: http://download.oracle.com/javase/6/docs/api/java/io/ObjectInputStream.html12•Exercise: Combine the code segments from the page above to create a Java application that first writes some objects into the file t.tmp and then reads the data back and have them displayed on the console output.•Challenges:a) Appropriate import statements need to be added.b) Appropriate exception handling are needed.•Sample solution: ObjectInputOutputDemo.java13TheFileClass•Class java.io.File provides information about files and directories. •Figure 17.2 lists some common File methods.•http://download.oracle.com/javase/6/docs/api/java/io/File.html141516•Sample Application using the File class: FileDemonstration.java //Fig. 17.3 from Deitel & Deitel, Java How to Program, 9e.17•The for-each loop in Java:File name = new File( path );… String[] directory = name.list();for ( String directoryName : directory ) System.out.println( directoryName );•Syntax: for (x : y) …Meaning: For each x in y, …y must be of type array or collectionSee http://leepoint.net/notes-java/flow/loops/foreach.html •More information about the enum type: http://download.oracle.com/javase/tutorial/java/javaOO/enum.html18•A separator character is used to separate directories and files in the path. •On Windows, the separator character is a backslash (\). •On Linux/UNIX, it’s a forward slash (/). •Java processes both characters identically in a path name. •When building Strings that represent path information, use File.separator to obtain the local computer’s proper separator. –This


View Full Document

UHCL CSCI 3134 - Files and Streams

Download Files and Streams
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Files and Streams and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Files and Streams 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?