1 / 20

Streams

Streams. Introduction to Programming Gordon College.

joie
Download Presentation

Streams

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Streams Introduction to Programming Gordon College

  2. 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.

  3. Camera to file ff d8 ff e0 00 10 4a 46 49 46 00 01 01 Stream.jpg 000100010000ffdb00430006

  4. Stream.jpg ff d8 ff e0 00 10 4a 46 49 46 00 01 01 File to Laptop 000100010000ffdb00430006

  5. Laptop to Laptop ff d8 ff e0 00 10 4a 46 49 46 00 01 01 000100010000ffdb00430006

  6. 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

  7. Data Hierarchy

  8. 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

  9. 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

  10. 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

  11. 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.

  12. Different Types of Streams • Network Streams Textbook Example: Simple Weather

  13. 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.

  14. 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)

  15. 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

  16. Text Streams write write println – independent of the particular stream PrintWriter file1 = new PrintWriter (new FileWriter(“file.txt” ) ); file1.println(“A line of text”);

  17. Text Streams read read readLine – returns null reference at EOF BufferedReader file1 = new BufferedReader(new FileReader(“file.txt” ) ); file1.readLine();

  18. 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

  19. 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

  20. Meta-File Information • Methods for class getName() isFile() isDirectory() isAbsolute() lastModified() length() getPath() getAbsolutePath() getParent()

More Related