Download - Java stream

Transcript
Page 1: Java stream

JAVA I/O Streams

Prepared by

Miss. Arati A. Gadgil

Page 2: Java stream

2

I/O Streams

An I/O Stream represents an input source or an output destination. A stream can represent many different kinds of sources and destinations, including disk files, devices, other programs, and memory arrays.

Streams support many different kinds of data, including simple bytes, primitive data types, localized characters, and objects. Some streams simply pass on data; others manipulate and transform the data in useful ways.

A stream is a sequence of data.

A program uses an input stream to read data from a source, one item at a time.

A program uses an output stream to write data to a destination, one item at time

Page 3: Java stream

3

The basic stream classes are defined in the package “java.io”.

A Java program is reasonably well served by its default state when execution begins. Three streams are setup and ready to go. There are two output streams, identified by the objects System.out and System.err, and one input stream, identified by the object System.in. These objects are defined as public data fields in the System class of the java.lang package

The err and out objects are instances of the PrintStream class and the in object is an instance of the InputStream class.

Java defines two types of streams. Byte Stream : It provides a convenient means for handling input and output of byte.

Character Stream : It provides a convenient means for handling input and output of characters. Character stream uses Unicode and therefore can be internationalized.

Page 4: Java stream

4

Page 5: Java stream

5

Byte Stream Classes

Byte stream is defined by using two abstract class at the top of hierarchy, they are InputStream and OutputStream

BufferedInputStream :Used for Buffered Input Stream.

BufferedOutputStream: Used for Buffered Output Stream.

DataInputStream: Contains method for reading java standard datatype

DataOutputStream: An output stream that contain method for writing java standard data type

Page 6: Java stream

6

FileInputStreamInput: stream that reads from a file

FileOutputStreamOutput: stream that write to a file.

InputStream: Abstract class that describe stream input.

OutputStream: Abstract class that describe stream output.

PrintStreamOutput: Stream that contain print() and println() method

Methodsread() : reads byte of data.

write() : Writes byte of data.

Page 7: Java stream

7

Character Stream Classes

Character stream is also defined by using two abstract class at the top of hierarchy, they are Reader and Writer.

Charcter stream classes

BufferedReaderHandles buffered input stream.

BufferedWriterHandles buffered output stream.

FileReaderInput stream that reads from file.

FileWriterOutput stream that writes to file.

InputStreamReaderInput stream that translate byte to character

Page 8: Java stream

8

OutputStreamReaderOutput stream that translate character to byte.

PrintWriterOutput Stream that contain print() and println() method.

ReaderAbstract class that define character stream input

WriterAbstract class that define character stream output

Page 9: Java stream

9

We use the object of BufferedReader class to take inputs from the keyboard.

read() method is used with BufferedReader object to read characters. As this function returns integer type value has we need to use typecasting to convert it into char type.

To read string we have to use readLine() function with BufferedReader class's object.

Page 10: Java stream

10

Scanner constructors

Scanner(File source)           Constructs a new Scanner that produces values scanned from the specified file.

Scanner(InputStream source)           Constructs a new Scanner that produces values scanned from the specified input stream.

Scanner(Readable source)           Constructs a new Scanner that produces values scanned from the specified source.

Scanner(String source)           Constructs a new Scanner that produces values scanned from the specified string.

Page 11: Java stream

11

Scanner will read a line of input from its source

Scanner sc = new Scanner (System.in);int i = sc.nextInt();System.out.println("You entered" + i);

This example reads a single int from System.in and outputs it to System.out. It does not check that the user actually entered an int.

Page 12: Java stream

12

Next Methods

String next() Finds and returns the next complete token from this scanner. 

boolean nextBoolean() Scans the next token of the input into a boolean value and returns that value. 

byte nextByte() Scans the next token of the input as a byte. 

double nextDouble() Scans the next token of the input as a double. 

float nextFloat() Scans the next token of the input as a float. 

int nextInt() Scans the next token of the input as an int. 

String nextLine() Advances this scanner past the current line and returns the input that was skipped. 

long nextLong() Scans the next token of the input as a long. 

short nextShort() Scans the next token of the input as a short.

Page 13: Java stream

13

hasNext methods

boolean hasNext() Returns true if this scanner has another token in its input. 

boolean hasNextBoolean() Returns true if the next token in this scanner's input can be interpreted as a boolean value using a case insensitive pattern created from the string "true|false". 

boolean hasNextByte() Returns true if the next token in this scanner's input can be interpreted as a byte value in the default radix using the nextByte() method. 

boolean hasNextDouble() Returns true if the next token in this scanner's input can be interpreted as a double value using the nextDouble() method. 

boolean hasNextFloat() Returns true if the next token in this scanner's input can be interpreted as a float value using the nextFloat() method. 

Page 14: Java stream

14

boolean hasNextInt() Returns true if the next token in this scanner's input can be interpreted as an int value in the default radix using the nextInt() method. 

boolean hasNextLine() Returns true if there is another line in the input of this scanner. 

boolean hasNextLong() Returns true if the next token in this scanner's input can be interpreted as a long value in the default radix using the nextLong() method. 

boolean hasNextShort() Returns true if the next token in this scanner's input can be interpreted as a short value in the default radix using the nextShort() method. 

Page 15: Java stream

15

RandomAccessFile

The RandomAccessFile class in the Java IO API allows you to move around a file and read from it or write to it. We can replace existing parts of a file too. This is not possible with the FileInputStream or FileOutputStream.

RandomAccessFile file = new RandomAccessFile("c:\\data\\file.txt", "rw");

To read or write at a specific location in a RandomAccessFile you must first position the file pointer at the location to read or write. This is done using the seek() method. The current position of the file pointer can be obtained by calling the getFilePointer() method.

RandomAccessFile file = new RandomAccessFile("c:\\data\\file.txt", "rw");

file.seek(200); long pointer = file.getFilePointer(); file.close();

Page 16: Java stream

16

Reading from a RandomAccessFile

Reading from a RandomAccessFile is done using one of it many  read()  methods.

RandomAccessFile file = new RandomAccessFile("c:\\data\\file.txt", "rw");

int aByte = file.read(); file.close();

The read() method reads the byte located a the position in the file currently pointed to by the file pointer in theRandomAccessFile instance.

Page 17: Java stream

17

Writing to a RandomAccessFile

Writing to a RandomAccessFile can be done using one it its many write() methods.

RandomAccessFile file = new RandomAccessFile("c:\\data\\file.txt", "rw");

file.write("Hello World".getBytes()); file.close();

Just like with the read() method the write() method advances the file pointer after being called. That way you don't have to constantly move the file pointer to write data to a new location in the file.

Page 18: Java stream

Thank You

18


Top Related