Example & Tutorial understanding programming in easy ways.

What is FileInputStream and FileOutputStream in java explain?.

FileOutputStream class:

A FileOutputStream is an output stream for writing data to a file.

If you have to write primitive values then use FileOutputStream.Instead, for character-oriented data, prefer FileWriter.But you can write byte-oriented as well as character-oriented data.

Example of FileOutputStream class:

Simple program of writing data into the file :

import java.io.*;

class javaTest{

public static void main(String args[]){

try{

FileOutputstream fs=new FileOutputStream("myjava.txt");

String s="i like file handlig in java";

byte b[]=s.getBytes();

fs.write(b);

fs.close();

System.out.println("welcome");

}catch(Exception e){system.out.println(e);}

} }

FileInputStream class:

A FileInputStream obtains input bytes from a file.It is used for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.

It should be used to read byte-oriented data.For example, to read image etc.


Example of FileInputStream class:

Simple program of reading data from the file :

import java.io.*;

class Readtest{

public static void main(String args[]){

try{

FileInputStream fis=new FileInputStream("myjava.txt");

int i;

while((i=fr.read())!=-1)

System.out.println((char)i);

fis.close();

}catch(Exception e){system.out.println(e);}

} }

 

Read More →
Search
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!