Java Programming Mehdi Ebady Manaa 3rd class – Department of Network College of IT- University of Babylon Section Two Reading data from InputStream using files Table 1: Fundamental Stream Classes 1. Data Within Streams Java technology supports two types of streams: character and byte. Input and output of character data is handled (implemented) by subclass readers and writers. Input and output of byte data is handled (implemented) by subclass inputStream and OutputStream 3.1. Byte Streams The following sections describe the fundamental byte stream 3.1.1 The InputStream Methods The following three methods provide access to the data from the input stream: The three basic read methods are: int read() // return an int from input stream, which contains either a byte read from the stream or a -1 It means The java.io.InputStream.read() method reads the next byte of the data from the the input stream and returns int in the range of 0 to 255. If no byte is available because the end of the stream has been reached, the returned value is -1. Page 1 Monday, May 30, 2016 Java Programming Mehdi Ebady Manaa 3rd class – Department of Network College of IT- University of Babylon int read(byte[] buffer) // read the stream into a byte array and return the number of bytes read int read(byte[] buffer, int offset, int length) // read the stream into a byte array and return the number of bytes read , the two int arguments in the third method indicate a sub range in the target array that needs to be filled Other methods include: void close() //it closes the stream long skip(long n) //this method discards the specified number of bytes from the stream. 3.1.2 The OutputStream Methods These methods write to the output of stream: The three basic write methods are: void write(int c)// void write(byte[] buffer) void write(byte[] buffer, int offset, int length) • Other methods include: void close() void flush()// output stream uses it to force write when the write accumulate before committing Java InputStream Example Java InputStream's are used for reading byte based data, one byte at a time. Here is a Java InputStream example: InputStream inputstream = new FileInputStream("c:/text.txt"); int data = inputstream.read(); while(data != -1) { //do something with data... // doSomethingWithData(data); data = inputstream.read(); } inputstream.close(); Page 2 Monday, May 30, 2016 Java Programming Mehdi Ebady Manaa 3rd class – Department of Network College of IT- University of Babylon import java.io.FileInputStream; import java.io.InputStream; public class InputStreamDemo { public static void main(String[] args) throws Exception { InputStream x = null; int i; char c; try{ // new input stream created x = new FileInputStream("C:\\test.txt"); System.out.println("Characters printed:"); // reads till the end of the stream while((i=x.read())!=-1) { // converts integer to character c=(char)i; // prints character System.out.print(c); } }catch(Exception e){ // if any I/O error occurs e.printStackTrace(); }finally{ // releases system resources associated with this stream if(x!=null) x.close(); } }} Example 2: create buffer of byte to read from input file stream import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; public class BufferedReaderDemo { public static void main(String[] args) throws Exception { InputStream is = null; InputStreamReader isr = null; BufferedReader br = null; try{ // open input stream test.txt for reading purpose. is = new FileInputStream("c:/test.txt"); // create new input stream reader isr = new InputStreamReader(is); // create new buffered reader br = new BufferedReader(isr); // creates buffer char[] cbuf = new char[is.available()]; //is.available to check the next invoking of byte // reads characters to buffer. br.read(cbuf); // for each character in the buffer for (char c:cbuf) { // if char is empty if(c == (char)0) { c='&'; } // prints characters Page 3 Monday, May 30, 2016 Java Programming Mehdi Ebady Manaa 3rd class – Department of Network College of IT- University of Babylon System.out.print(c); }catch(Exception e){ e.printStackTrace(); }finally{// releases resources associated with the streams if(is!=null) is.close(); if(isr!=null) isr.close(); if(br!=null) br.close(); } } } Example2 import import import import public java.io.FileInputStream; java.io.IOException; java.io.InputStream; java.util.Arrays; class InputStreamExample { private static final String FILE_PATH="c://cc.txt"; public static void main(String[] args){ InputStream fileInputStream = null; byte[] bytes = new byte[20]; try { FileInputStream = new FileInputStream(FILE_PATH); System.out.println("Available bytes of file:"+fileInputStream.available()); int bytesread = fileInputStream.read(bytes, 0, 15); System.out.println("Bytes read :"+bytesread); System.out.println(Arrays.toString(bytes)); } catch (IOException e) { e.printStackTrace(); }finally{ try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); }}}} Of course you can use all the above methods to bridge the byte stream to a character streams. So let’s see how you can read a text file,line by line package com.javacodegeeks.core.io.inputstream; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class InputStreamExample { private static final String FILE_PATH="c:/cc.txt"; public static void main(String[] args){ InputStream fileInputStream = null; Page 4 Monday, May 30, 2016 Java Programming Mehdi Ebady Manaa 3rd class – Department of Network College of IT- University of Babylon BufferedReader bufferedReader = null; try { fileInputStream = new FileInputStream(FILE_PATH); bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream)); String line=""; while( (line = bufferedReader.readLine()) != null ){ System.out.println(line); } } catch (IOException e) { e.printStackTrace(); }finally{ try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); }}}} Example: read and write in input and output stream import java.io.*; public class OutputStreamDemo { public static void main(String[] args) { byte[] b = {'h', 'e', 'l', 'l', 'o'}; try { // create a new output stream OutputStream os = new FileOutputStream("c:/test1.txt"); // craete a new input stream InputStream is = new FileInputStream("c:/test1.txt"); // write something os.write(b); // read what we wrote for (int i = 0; i < b.length; i++) { System.out.print("" + (char) is.read()); } } catch (Exception ex) { ex.printStackTrace(); } } } Page 5 Monday, May 30, 2016