1 / 70

Streams

Streams. CSC 171 FALL 2004 LECTURE 22. Make up exam. Friday 12/3 11AM-12:10PM. READING ASSIGNMENT. Horstmann Chapter 15 – Streams Horstmann Chapter 14 – Exceptions Optional Horstmann Chapter 16 – System Design Horstmann Chapter 17, 18, 19 Prep for CSC 172. EXAM. In class 12/9 Arrays

mgina
Télécharger la présentation

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 CSC 171 FALL 2004 LECTURE 22

  2. Make up exam • Friday 12/3 11AM-12:10PM

  3. READING ASSIGNMENT • Horstmann Chapter 15 – Streams • Horstmann Chapter 14 – Exceptions • Optional • Horstmann Chapter 16 – System Design • Horstmann Chapter 17, 18, 19 • Prep for CSC 172

  4. EXAM • In class 12/9 • Arrays • Exceptions • Streams

  5. Streams • In Java a stream provides access to sequences of bytes. • Bytes can come from a variety of sources • Console input/output • Files stored on the computer’s disk • Network connections

  6. _________ access sequences of bytes.

  7. ___Streams______ access sequences of bytes.

  8. Simplest File input • Create a FileReader • Use its read method to read a single character-returns the next char as an int or the integer -1 at end of input • Test for -1 • If not -1, cast to char • Close the file when done

  9. ___________ and ________________ access sequences of characters.

  10. _readers______ and ____writers____ access sequences of characters.

  11. Reading FileReader reader = new FileReader("input.txt"); int next = reader.read() ; char c; if (next != -1) c = (char)next(); ... reader.close()

  12. Writing FileWriter writer = new FileWriter("output.txt");...char c='';...writer.write(c); ...write.close();

  13. The read method returns an _________________, either ___________ at end of file, or another value which needs to be cast to ___________, or ___________.

  14. The read method returns an ___integer_____, either ____-1_______ at end of file, or another value which needs to be cast to ___char____, or ____byte_______.

  15. You must _______________ files that you no longer need.

  16. You must ___close______ files that you no longer need.

  17. TEXT FORMAT • Human-readable form • Sequence of characters-Integer 12345 stored as characters "1" "2" "3" "4" "5" • Use Reader and Writer and their subclasses

  18. Writing text files • Use a PrintWriter -breaks up strings into individual characters -sends them one at a time to a FileWriter • Create a PrintWriter FileWriter writer = new FileWriter("output.txt"); PrintWriter out = new PrintWriter(writer); • Use print and println out.println(29.95); out.println(new Rectangle(5,10,15,25)); out.println("Hello, World!");

  19. Use a BufferedReader -Reads a character at a time from a FileReader-Assembles the characters into a line and returns it • Use Integer.parseInt or Integer.parseDouble to convert the strings to numbers • Code FileReader reader = new FileReader ("input.txt"); BufferedReader in = new BufferedReader(reader); String inputLine = in.ReadLine(); double x = Double.parseDouble(inputLine);

  20. Basic streams, readers, and writers can process only _____________________. You need to combine them with other classes to process _______________ or _________________.

  21. Basic streams, readers, and writers can process only _individual bytes or chars______. You need to combine them with other classes to process ___lines______ or ____objects______.

  22. When writing text files, use ________________ class and the _____________________ methods. When reading text files, use _______________ class and the ____________ method.

  23. When writing text files, use _____PrintWriter______ class and the ____print/println__________ methods. When reading text files, use _____BufferedReader________ class and the ___readLine______ method.

  24. File • File class describes disk files and directories • Create a File objectFile inputFile = new File("input.txt"); • Some file methods delete renameTo exists • Constructing a FileReader from a File object FileReader reader = new FileReader(inputFile) ;

  25. A File object describes a ___________ or __________________.

  26. A File object describes a _____file_______ or _______directory___________.

  27. You can pass a File object to the constructor of a file ____________, _________, or stream.

  28. You can pass a File object to the constructor of a file ____reader_________, ___writer________, or stream.

  29. File Chooser

  30. File Choser • Construct a file chooser object • Call its showOpenDialog or showSaveDialog method • Specify null or the user interface component over which to pop up the dialog • If the user chooses a file, these methods return JFileChooser.APPROVE_OPTION • If the user cancels the selection, these methods return JFileChooser.CANCEL_OPTION • If a file is chosen, use GetSelectedFile method to obtain a File object describing the file

  31. Code JFileChooser chooser = new JFileChooser(); FileReader in = null; if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { File selectedFile= chooser.getSelectedFile(); in = new FileReader(selectedFile); }

  32. The ______________________________ dialog lets users select a file by navigating directories.

  33. The _JFileChooser__________ dialog lets users select a file by navigating directories.

  34. Command Line Java MyProgram -d file.txt class MyProgram { public static void main(String[] args) { ... // arg[0] == “-d” // arg[1] == “file.txt” } }

  35. When you launch a program from the command line, you can specify arguments after the program name. The program can access these strings by processing the ______________ parameter of the ___________ method.

  36. When you launch a program from the command line, you can specify arguments after the program name. The program can access these strings by processing the _____”args”___ parameter of the ___main___ method.

  37. Binary Format • More compact and efficient • Integer 12345 stored as 00 00 48 57 • Use InputStream and OutputStream and their subclasses

  38. File IO • We have seen character I/O • Sometimes, we want to have other data types stored in file • It’s all just bits

  39. Consider saving an array a = new float[5][5]; for(int i = 0 ; i<a.length;i++) for(int j = 0 ; j<a[i].length;j++) a[i][j] = ((float)i+1) / ((float)j+1) ;

  40. Open the File private void saveArray() { try { JFileChooser chooser = new JFileChooser(); PrintWriter out1 = null; if (chooser.showOpenDialog(null)== JFileChooser.APPROVE_OPTION) { File selectedFile = chooser.getSelectedFile(); FileWriter writer = new FileWriter(selectedFile); out1 = new PrintWriter(writer);

  41. Write the array for(int i = 0 ; i<a.length;i++) for(int j = 0 ; j<a[i].length;j++){ out1.println(a[i][j] + " "); System.out.println(" " + a[i][j]); } writer.close(); } } catch (IOException e) {System.out.println("problem");} }

  42. Open the file private void restoreArray() { try { JFileChooser chooser = new JFileChooser(); BufferedReader in1 = null; if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { File selectedFile = chooser.getSelectedFile(); FileReader reader = new FileReader(selectedFile); in1 = new BufferedReader(reader);

  43. Read the array for(int i = 0 ; i<a.length;i++) for(int j = 0 ; j<a[i].length;j++){ String s1 = in1.readLine(); System.out.println(s1); a[i][j] = (float) Double.parseDouble(s1); } reader.close(); } } catch (IOException e) {System.out.println("problem");} repaint(); }

  44. Text File 1.0 0.5 0.33333334 0.25 0.2 2.0 1.0 0.6666667 0.5 0.4 3.0 1.5 1.0 0.75 0.6 4.0 2.0 1.3333334 1.0 0.8 5.0 2.5 1.6666666 1.25 1.0 Text File

  45. Byte by Byte Text file 303 bytes 0000000 1 . 0 \r \n 0 . 5 0000020 \r \n 0 . 3 3 3 3 3 3 3 4 0000040 \r \n 0 . 2 5 0000060 \r \n 0 . 2 \r \n 2 0000100 . 0 \r \n 1 . 0 0000120 \r \n 0 . 6 6 6 6 6 6 7 0000140 \r \n 0 . 5 \r 0000160 \n 0 . 4 \r \n 3 . 0 0000200 \r \n 1 . 5 0000220 \r \n 1 . 0 \r \n 0 . 7 0000240 5 \r \n 0 . 6 0000260 \r \n 4 . 0 \r \n 2 0000300 . 0 \r \n 1 . 3 3 3 3 0000320 3 3 4 \r \n 1 . 0 0000340 \r \n 0 . 8 \r 0000360 \n 5 . 0 \r \n 2 . 5 0000400 \r \n 1 . 6 6 6 6 6 6 6 0000420 \r \n 1 . 2 5 0000440 \r \n 1 . 0 \r \n 0000457

  46. Open the file (binary) private void saveArray2() { try { JFileChooser chooser = new JFileChooser(); DataOutputStream out1 = null; if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { File selectedFile = chooser.getSelectedFile(); FileOutputStream writer = new FileOutputStream(selectedFile); out1 = new DataOutputStream(writer);

  47. Write the file for(int i = 0 ; i<a.length;i++) for(int j = 0 ; j<a[i].length;j++){ out1.writeFloat(a[i][j]); System.out.println(" " + a[i][j]); } writer.close(); } } catch (IOException e) {System.out.println("problem");} } }

  48. Open the file private void restoreArray2() { try { JFileChooser chooser = new JFileChooser(); DataInputStream in1 = null; if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { File selectedFile = chooser.getSelectedFile(); FileInputStream reader = new FileInputStream(selectedFile); in1 = new DataInputStream(reader);

  49. Read the file for(int i = 0 ; i<a.length;i++) for(int j = 0 ; j<a[i].length;j++){ a[i][j] = in1.readFloat(); } reader.close(); } } catch (IOException e) {System.out.println("problem");} repaint(); }

More Related