java stream

of 18/18
JAVA I/O Streams Prepared by Miss. Arati A. Gadgil

Post on 13-Apr-2017

274 views

Category:

Education

0 download

Embed Size (px)

TRANSCRIPT

  • JAVA I/O StreamsPrepared byMiss. Arati A. Gadgil

  • *I/O Streams

    AnI/O Streamrepresents 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 aninput streamto read data from a source, one item at a time.

    A program uses anoutput streamto write data to a destination, one item at time

  • *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.

  • *

  • *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

  • *

    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 containprint()andprintln()method

    Methodsread(): reads byte of data.

    write(): Writes byte of data.

  • *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

  • *

    OutputStreamReaderOutput stream that translate character to byte.

    PrintWriterOutput Stream that containprint()andprintln()method.

    ReaderAbstract class that define character stream input

    WriterAbstract class that define character stream output

  • *

    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 intochartype.

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

  • *Scanner constructors

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

    Scanner(InputStreamsource) 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(Stringsource) Constructs a new Scanner that produces values scanned from the specified string.

  • *

    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.

  • *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.

  • *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.

  • *

    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.

  • *RandomAccessFile

    TheRandomAccessFileclass 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 theFileInputStreamor FileOutputStream.

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

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

    RandomAccessFile file = new RandomAccessFile("c:\\data\\file.txt", "rw"); file.seek(200); long pointer = file.getFilePointer(); file.close();

  • *Reading from a RandomAccessFile

    Reading from aRandomAccessFileis done using one of it many read() methods.

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

    Theread()method reads the byte located a the position in the file currently pointed to by the file pointer in theRandomAccessFileinstance.

  • *Writing to a RandomAccessFile

    Writing to aRandomAccessFilecan be done using one it its manywrite()methods.

    RandomAccessFile file = new RandomAccessFile("c:\\data\\file.txt", "rw"); file.write("Hello World".getBytes()); file.close();

    Just like with theread()method thewrite()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.

  • Thank You*