4/17/19

JAVA I/O - Input and Output handling (File Handling)


JAVA I/O - Input and Output handling (File Handing)

Input and output (I/O) is used to process the input and produce the output based on the input. java uses the concept of stream to make I/O operations fast. java.io. package contains all the classes required for input and output operations.

Streaming nothing but streaming access which bulk data access. like you tube streaming.

Output Stream:
Java Application -> 00110011 -> Destination(File,Application)

Input Stream:
(File,Application) -> 1001010 -> Java Application

I/O classes:
- FileInputStream
- FileOuputStream
- BufferInputReader
- BufferOutptuReader
- FileWritter
- FileReader
.ect.

File Streaming handles Strings (numeric + alpha)


File Writing Java Code..

package com.najathi.www;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileWritee {

 public static void main(String[] args) {

  try {
   
   FileOutputStream fout = new FileOutputStream("file.txt");
   
   String s = "Hai i'm Najathi";
   
   byte b[] = s.getBytes();
   fout.write(b);
   
   fout.close();
   System.out.println("Success..");
   
   
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
 }

}



File Reading Java Code...


package com.najathi.www;  
 import java.io.FileInputStream;  
 import java.io.FileNotFoundException;  
 import java.io.IOException;  
 public class FileReadd {  
      public static void main(String[] args) {  
           try {  
                FileInputStream fin = new FileInputStream("file.txt");  
                int i;  
                while((i=fin.read())!= -1){  
                     System.out.print((char)i);  
                }  
                fin.close();  
           } catch (FileNotFoundException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
           } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
           }  
      }  
 }  

No comments:

Post a Comment

About

Hi, I'm Najathi.
I've started entrepreneurial company, Twin Brothers.Inc.
One is self-funded & the other is venture backed. I also send a weekly. where I share relevent, curated links.

Every Week I Publish a short post on writing, publishing, or content of IT Related

Contact Form

Name

Email *

Message *