1 / 11

Streams and Files: Input & Output

Streams and Files: Input & Output. Files and Stream. Files can be used to: Store Java classes, programs. Store pictures, music, videos. Can also use files to store program Input/Output. Stream is a flow of input or output data: Characters. Numbers. Bytes. Concept of a Stream.

emiko
Télécharger la présentation

Streams and Files: Input & Output

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 Files: Input & Output

  2. Files and Stream • Files can be used to: • Store Java classes, programs. • Store pictures, music, videos. • Can also use files to store program Input/Output. • Streamis a flow of input or output data: • Characters. • Numbers. • Bytes.

  3. Concept of a Stream • Streams are implemented as objects of special stream classes: • Class Scanner • Object System.out

  4. Why Use Files for I/O • Input of the keyboard and also screen output deal with temporarydata: • In other words, when program ends, data is gone. • E.g., consider a java program that sorts an array. • Data in a file remains after program ends: • Can be used next time when program runs. • Can be also used by another program.

  5. Text Files and Binary Files • All data in files stored as binary digits: • That is, long series of zeros and ones. • Files treated as a sequence of characters called text files, e.g., Java program source code: • They can be viewed and edited with text editors. • All the other files are called binary files, e.g., movie and music files: • The access requires a specialized program.

  6. you must import package java.io “PrintWriter” is a keyword declare stream variable for referencing the stream always need the try & catch block class PrintWriter defines methods needed to create &write to a text file file name can be also defined by the user • file can be written to with method println • data initially goes to the memory buffer and then goes to the file closing file empties buffer and disconnectsthe stream

  7. Some Points • When creating a file: • Inform the user of ongoing I/O events, program should not be silent. • A file has two names in the program: • The file name used by the OS, e.g., “out.txt”. • The stream namevariable, e.g., “outputStream”. • Opening and writing to a file overwrites pre-existing file in a directory.

  8. you must import package java.io “Scanner” is a keyword declare stream variable for referencing the stream class Scannerdefines methods needed to read from a text file always need the try & catch block hasNextLine() returns true if more input data is available nextLine()reads one more line from the file closing file empties buffer and disconnectsthe stream

  9. here we first read the file name and then we read the file the user enters the name of the file that we intend to read from these 3 lines are a replacement for the following statement: String fileName = ‘’out.txt’’;

More Related