Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java I/O Streams: Understanding Input and Output Streams in Java, Slides of Java Programming

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

2011/2012

Uploaded on 07/03/2012

aapti
aapti 🇮🇳

4.6

(28)

82 documents

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ava-programming
An
Introduction
Java Short Course
Day-07
J
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download Java I/O Streams: Understanding Input and Output Streams in Java and more Slides Java Programming in PDF only on Docsity!

ava - programming

An Introduction

Java Short Course

Day-

J

Outline

  • Streams

 What

 Why

  • Streams categories
  • Important Streams
  • Streams and files

Java I/O Model

  • Based on the concept of input/output stream
  • Streams can be thought of physical streams of water flowing from a

water plant into the pipes of a water system

  • Data flowing in streams can be directed in may different ways much

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

  • Byte Streams

 Support byte input and output (8 bit input output)

 Termed as input or output streams

 InputStream  BufferedInputStream  DataInputStream  FileInputStream  StringBufferInputStream

  • Character Streams

 Supports same methods but for characters (16 bit input output)

 These streams are termed as reader and write

 Reader  BufferedReader  FileReader  StringReader

Stream Packages

  • Streams are classes
  • java.io package

 Core streams  Other pacakges may include specialized input/output stream classes

  • Steam categories

 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

 mark(int position)

 Used to mark the stream for future reference related with reset

 reset( )

 Returns the stream to a marked position

 boolean markSupported( )

 Returns a boolean indicating whether stream support marking

 close

 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

  • Provides basic functionality for all output streams
  • Important methods

 write(int b )

 Writes a single byte of data to the output stream  Overloaded method

 void write(byte [ ] b)

 For writing an array of bytes to the output stream

 void write(byte[ ], int offset, int length)

 Writes content of byte array to an output stream starting from the given position upto the length specified

 flush( )

 To validate or ensure the writing of content from the internal buffer to the destination

 close( )

 To close the output stream

Example-01: Reading from keyboad

(Using core stream)

 Reading from the Keyboard

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

  • Steam chaining

 Two or more streams can be joined , i.e. end of one stream start of the

other stream

 Chaining provide extra functionality to be incorporated into streams

 Chaining Syntax

 FilterInputStream(InputStream in)  Comments [ There is no such FilterInputStream ]

 Example

 LineNumberInputStream lineCount=new LineNumberInputStream(System.in);  adding line number to the data read using System.in through LineNumberInputStream object linecount

 Some useful FilterStreams

 Buffered Streams  Data Streams  StringBufferInputStream  Other Streams

Buffered Streams

  • Help speed up the program execution
  • Normal Read/Write operations

 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

  • Buffered Read/Write Opertions

 Bytes accumulated in a buffer  After certain limit the data is transferred  Buffers can be externally flushed  Buffers can be defined externally

  • BufferedInputStream

 Tries to read as much data as possible in a single read call

  • BufferedOutputStream

 Writes data to destination only if the buffer is full  Writes data when flush is called

Buffered Streams

  • Constructors

 public BufferedInputStream(InputStream in)

 public BufferedInputStream(InputStream in,int bufferSize)

 public BufferedOutputStream(OutputStream out)

 public BufferedOutputStream(OutputStream out,int bufferSize)

Handling Files

  • File Security

 File access is disallowed to Browser by default

 File access is disallowed to java applets

 Event volunteer access to file system is not allowed to java applets

  • FileInputStream
  • FileOutputStream
  • File class

FileInputStream

  • reading from a file
  • Constructors

 FileInputStream(String name)

 Creates a stream using file name  File is supposed to be present in the same directory

 FileInputStream(File file)

 Creates a stream using file object  File information is provided by the file object such as file name, file path , encoding etc

 FileInputStream(FileDescriptor fdesc)

 Creates a stream using file descriptor

  • Methods

 Most of the methods are inherited from the InputStream class

 Try finding more methods