
















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An introduction to java i/o streams, explaining what they are, why they are used, and the different categories and important streams. It covers the java io model, basic streams (byte and character), stream packages, and how streams work. It also includes examples and explanations of filtered streams and buffered streams.
Typology: Slides
1 / 24
This page cannot be seen from the preview
Don't miss anything!
An Introduction
Outline
Java I/O Model
water plant into the pipes of a water system
as water is directed through a complex system of pipes
Streams
java.io
BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter ByteArrayInputStream ByteArrayOutputStream CharArrayReader CharArrayWriter DataInputStream DataOutputStream File FileDescriptor FileInputStream FileOutputStream FilePermission FileReader FileWriter FilterInputStream FilterOutputStream FilterReader FilterWriter
InputStream InputStreamReader LineNumberInputStream LineNumberReader ObjectInputStream ObjectInputStream.GetField ObjectOutputStream ObjectOutputStream.PutField ObjectStreamClass ObjectStreamField OutputStream OutputStreamWriter PipedInputStream PipedOutputStream PipedReader PipedWriter PrintStream PrintWriter PushbackInputStream PushbackReader
RandomAccessFile Reader SequenceInputStream SerializablePermission StreamTokenizer StringBufferInputStream StringReader StringWriter Writer
How do I
read an int
from a file?
Basic Streams
InputStream BufferedInputStream DataInputStream FileInputStream StringBufferInputStream
Reader BufferedReader FileReader StringReader
Stream Packages
Core streams Other pacakges may include specialized input/output stream classes
Basic Input/Output Streams Deals directly with the underlying system for input/output operations Filter/Formatting Streams Provide extra features Sometimes filter the contents Example A stream that provides capability of reading a line of text to a basic input stream A stream that connects a basic input stream to a file A stream that converts a basic stream in tokens Pipe streams Connect a stream to some process Used for inter-process communication
Used to mark the stream for future reference related with reset
Returns the stream to a marked position
Returns a boolean indicating whether stream support marking
For closing a stream Most of the time the an unclosed stream is closed by the garbage collector Number of files opened in a time may be limited so it is necessary to close some streams
Output Stream class
Writes a single byte of data to the output stream Overloaded method
For writing an array of bytes to the output stream
Writes content of byte array to an output stream starting from the given position upto the length specified
To validate or ensure the writing of content from the internal buffer to the destination
To close the output stream
Example-01: Reading from keyboad
(Using core stream)
import java.io.; public class IOApp{*
public static void main(String args[ ]){
Byte buffer[ ] =new byte[255]; System.out.println(“Type a line of text:”); try{ System.in.read(buffer,0,255); System.out.println(“The line you typed is = “); String inputStr=new String(buffer); System.out.println(inputStr)l } catch(Exception e){e.printStackTrace();} } //end main } //end class
Filtered Streams
FilterInputStream(InputStream in) Comments [ There is no such FilterInputStream ]
LineNumberInputStream lineCount=new LineNumberInputStream(System.in); adding line number to the data read using System.in through LineNumberInputStream object linecount
Buffered Streams Data Streams StringBufferInputStream Other Streams
Buffered Streams
Single byte read/write For each reading/writing each byte separate call is made to the operating system resources Operating system calls are costly Other bytes wait until the first one is read/written
Bytes accumulated in a buffer After certain limit the data is transferred Buffers can be externally flushed Buffers can be defined externally
Tries to read as much data as possible in a single read call
Writes data to destination only if the buffer is full Writes data when flush is called
Buffered Streams
Handling Files
FileInputStream
Creates a stream using file name File is supposed to be present in the same directory
Creates a stream using file object File information is provided by the file object such as file name, file path , encoding etc
Creates a stream using file descriptor