Tuesday, September 8, 2015

Java IO

JAVA STREAM CLASS REPRESENTATION



Input -- Reading content
Output -- writing the content

Java stream class are of two type

1. Byte Stream - for I/O on bytes
2. Character Stream - for I/O operation on character


Character Stream Class Byte Stream Class
Reader Input stream
Buffered Reader Buffered input stream
Buffered Writer Buffered output stream
File reader File input stream
File writer  File output stream

What is Stream?

Input stream/ output stream is the way of reading and writing raw data respectively. In stream, data are read byte by byte. For example if you want to read an image file or a binary file use Byte Stream Class
Where character stream is used for reading all text. It takes care of character decoding to give Unicode character.

What is Buffer and Un-buffered ?
Unbuffered I/O - For unbuffered access read and write operation is handled by Operating System level. Unbuffered way of reading and writing files involves direct disk access. Which is inefficient in terms of performance.
Buffered I/O - To overcome the inefficiency the way read and write operation occurs for unbuffered  way buffered stream is used. In buffered stream read operation is performed on memory. The memory is nothing but what we call buffer. native API is called when buffer is empty. Write operation is also performed on memory i.e. buffer, when the buffer is full native API is called
Unbuffered stream can be converted to buffer stream in following ways
For character class
inputStream = new BufferedReader(new FileReader("xanadu.txt"));
outputStream = new BufferedWriter(new FileWriter("characteroutput.txt"));

//Read File Using Buffered input stream

package example1; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class FileReader { public static void main(String[] args) { reader r = new reader(); r.fileReader(); } } class reader { public void fileReader() { // this constructor accept a input stream try { BufferedInputStream bis = new BufferedInputStream( new FileInputStream("C:/Test/sudas.txt")); while (bis.available() > 0) { int read = bis.read(); System.out.print((char) read); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

// Write File Using BufferedOutputStream

Class BufferOutput { public void bufferout() { try { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:/Test/File1.txt")); String sample="This is a sample text"; byte[] inbyte = sample.getBytes(); bos.write(inbyte); bos.flush(); bos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

//Read File Using Buffered Reader using READLINE method

package example1; import java.io.BufferedReader; import java.io.IOException; public class FileReader { public static void main(String[] args) { reader r = new reader(); r.fileReader(); } } class reader { // "C:/Test/sudas.txt" public void fileReader() { try { BufferedReader reader = new BufferedReader(new java.io.FileReader( "C:/Test/sudas.txt")); while (reader.readLine() != null) { String read = reader.readLine(); System.out.println(read); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

// Use of Buffer Reader using READ method

      public static void readCookies(String filePath) throws IOException {  
           BufferedReader br = new BufferedReader(new FileReader(new File(filePath)));  
           int line;  
           while((line=br.read())!=-1)  
           {  
           System.out.println((char)line);       
           }  
      }  

//Write file using BufferedWriter

public void writer() { String path = "C:/Test/CreateFile.txt"; // create a file File file = new File(path); try { if (file.exists()) { file.deleteOnExit(); System.out.println("file exists deleting file"); } else { file.createNewFile(); } BufferedWriter writer = new BufferedWriter(new FileWriter(path)); writer.write("This is for Test Purpose"); writer.flush(); writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

// EXAMPLE TO CREATE FILE, IF EXISTS DELETE FILE

public void writer() { String path = "C:/Test/CreateFile.txt"; // create a file File file = new File(path); try { if (file.exists()) { file.deleteOnExit(); System.out.println("file exists deleting file"); } else { file.createNewFile(); } // BufferedWriter writer = new BufferedWriter(new FileWriter(path)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

// Utility to copy the content of one file to the other

class CopyContent { public void copy() { String sourceFile = "C:/Test/AdobeARM.log"; String destinationFile = "C:/Test/CreateFile.txt"; try { // create a file File file = new File(destinationFile); if (file.exists()) { file.deleteOnExit(); System.out.println("file exists deleting file"); } else { file.createNewFile(); System.out.println("File created"); } // Read the source file content BufferedReader breader = new BufferedReader(new java.io.FileReader( new File(sourceFile))); BufferedWriter writer = new BufferedWriter(new FileWriter( destinationFile)); while (breader.readLine() != null) { char[] readLine = breader.readLine().toCharArray(); // write writer.write(readLine); writer.write('\n'); } writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

This is an example to get String from Fileinputstream using StringBuilder


 class LogScanner {  
      public void scan() {  
           // fileinputstream to string  
           StringBuilder sb = new StringBuilder();  
           try {  
                FileInputStream fis = new FileInputStream(new File("/Users/S_Das/Documents/Java/sudas.log"));  
                int input;  
                while((input = fis.read())!=-1){  
                sb.append((char)input);  
                }  
                System.out.println(sb.toString());  
                fis.close();  
           } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
           }  
      }  
 }  

No comments:

Post a Comment