E N D
Streams Introduction to Programming Gordon College
Psal 1:1 Blessed is the man who does not walk in the counsel of the wicked or stand in the way of sinners or sit in the seat of mockers. But his delight is in the law of the LORD, and on his law he meditates day and night. He is like a tree planted by streams of water, which yields its fruit in season and whose leaf does not wither. Whatever he does prospers.
Camera to file ff d8 ff e0 00 10 4a 46 49 46 00 01 01 Stream.jpg 000100010000ffdb00430006
Stream.jpg ff d8 ff e0 00 10 4a 46 49 46 00 01 01 File to Laptop 000100010000ffdb00430006
Laptop to Laptop ff d8 ff e0 00 10 4a 46 49 46 00 01 01 000100010000ffdb00430006
Outline • Data Hierarchy • What’s a Stream • Different Types of Streams • Text Streams • Meta-File Information • Binary Files and Random Files • Accessing Pages Through a Network
What’s a Stream • Managed flow of data between programs & any external entity that can provide or receive data. Java stream-processing capabilities: • read & write data in files • read & write data in memory • read & write data over network connections
Different Types of Streams 1. Direction of stream Which way does the stream flow? • input stream - from file to process • output stream - from process to file
Different Types of Streams 2. Kinds of stream • Byte-based streams • Store data in Binary format. • Called “binary files”. • Data must be converted to a character form before it is displayed for the user. • Character-based streams • Stores the data in Character format. • Called “text files”. • Also typically has special characters (like \n - which signifies the end) • produced and used by text editors
Different Types of Streams For Example: • Binary Files: The value 5 is stored in binary format as 101…or more specifically it is stored using an integer format (like 2’s complement) so it would be 000000000000101. Types: financial files, picture files, program executables etc. • Test Files: The value 5 in character format would be 00000000 00110101 which is the ASCII value for 5 in Unicode format. Types: text files (.txt), html, xml, etc.
Different Types of Streams • Network Streams Textbook Example: Simple Weather
Text Streams • System.out is a static stream which is automatically open and ready to accept output data: System.out.print("Enter Date (mm/dd/yyyy): "); for ( int x= 1; x <= 10; x++) { date = date + (char) System.in.read(); } System.in, System.out, System.err. (can be redirected) Example: Redirection example.
Text Streams • Simple Example - input from the keyboard: private static String readString() throws IOException { String tempS=""; int tempC; while ((tempC = System.in.read())!='\n') { tempS += (char) tempC; } return tempS; } Example: TextFileMaker1 (example of redirection)
Text Streams • Simple example – input from a file 1. Create the input stream object: BufferedReader input = new BufferedReader(new FileReader(filename)); 2. Use readLine boolean booleanValue = (new Boolean(input.readLine())).booleanValue(); 3. Close file input.close(); See example: TextFileAccessor
Text Streams write write println – independent of the particular stream PrintWriter file1 = new PrintWriter (new FileWriter(“file.txt” ) ); file1.println(“A line of text”);
Text Streams read read readLine – returns null reference at EOF BufferedReader file1 = new BufferedReader(new FileReader(“file.txt” ) ); file1.readLine();
Text Streams • Simple example – input from a file 1. Create the input stream object: BufferedReader input = new BufferedReader(new FileReader(filename)); 2. Read the lines String curLine = someBufferedReader.readLine(); while(curLine != null) { … curLine = someBufferedReader.readLine(); } 3. Close file input.close(); See example: TextFileAccessor
Meta-File Information • What sort of information does a file system contain? File class - An abstract representation of file and directory pathnames File name = new File( “filename.dat”); See examples: FileTest1 & FileTest
Meta-File Information • Methods for class getName() isFile() isDirectory() isAbsolute() lastModified() length() getPath() getAbsolutePath() getParent()