1 / 26

Streams and File I/O

Streams and File I/O. October 23, 2013. 10.1 AN OVERVIEW OF STREAMS AND FILE I/O . A stream is a flow of data. The data might be characters, numbers, or bytes consisting of binary digits. If the data flows into your program, the stream is called an input stream.

ermin
Download Presentation

Streams and File I/O

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 and File I/O October 23, 2013

  2. 10.1 AN OVERVIEW OF STREAMS AND FILE I/O • A stream is a flow of data. • The data might be characters, numbers, or bytes consisting of binary digits. • If the data flows into your program, the stream is called an input stream. • If the data flows out of your program, the stream is called an output stream.

  3. Stream

  4. Stream • A stream is an object that either • Delivers data from program to a destination, such as a file or the screen • Takes data from a source, such as a file or the keyboard, and delivers the data to program

  5. Text Files and Binary Files • All the data in any file is stored as bits – that is a long sequence of 0s and 1s. • Text file – bits are interpreted as sequence of characters • Binary file – all other file • Java program can read both text and binary files

  6. Text File and a Binary File Containing the Same Values

  7. 10.2 TEXT-FILE I/O • Creating/Writing to a Text File • Appending to a Text File • Reading from a Text File

  8. Creating/Writing to a Text File • Stream variable – reference to stream • Class PrintWriter in java.io connects a file to an output stream • Overwrites file contents

  9. LISTING 10.1: Writing Output to a Text File (1/2)

  10. LISTING 10.1: Writing Output to a Text File (2/2)

  11. Writing Output to a Text File

  12. Appending to a Text File • Append – write at the end of the file • Old content of the file will remain • PrintWriter does not have appropriate constructor • We need class FileOutputStream from java.io

  13. Reading from a Text File • Scanner class • Constructor of Scanner does not take a String argument as filename • Constructor of Scanner accepts an instance of File class • File has a constructor to take a String argument as filename

  14. LISTING 10.2:Reading from a Text File (1/3)

  15. LISTING 10.2:Reading from a Text File (2/3)

  16. Reading from a Text File (3/3)

  17. Additional Methods in the Class Scanner

  18. 10.3 TECHNIQUES FOR ANY FILE:The Class File • The class File provides a way to represent file names in a general way. • A string “treasure.txt” is just a string to Java • Class File create an object which Java recognize as a name of a file.

  19. LISTING 10.3 Reading a File Name and Then the File (1/2)

  20. LISTING 10.3 Reading a File Name and Then the File (2/2)

  21. Reading a File Name and Then the File

  22. Using Path Names (1/3) • When file and the program are in the same directory – we specify only filename • If they are in different location – we have to specify path name instead of filename • Full path name – complete path name starting from the root directory • Relative path name – path to the file starting from the current working directory

  23. Using Path Names (2/3)

  24. Using Path Names (3/3) • Java program accepts • Unix format path name in Unix, Windows • Windows format path name in Unix, Windows

  25. Some Methods in the Class File

  26. Check a text file for reading

More Related