Java FileStream

 

OutputStream

         Java applications use an output stream to write data to a destination; it may be a file, an array, peripheral device or socket.

InputStream

         Java applications use an input stream to read data from a source; it may be a file, an array, peripheral device or socket.

 

Below, we’ll see elaborately about these Stream.

 

OutputStream class

         OutputStream class is an abstract class. It is the superclass of all classes representing an output stream of bytes.

         An output stream accepts output bytes and sends them to some sink.

Useful methods of OutputStream

Method

Description

1) public void write(int)throws IOException

is used to write a byte to the current output stream.

2) public void write(byte[])throws IOException

is used to write an array of byte to the current output stream.

3) public void flush()throws IOException

flushes the current output stream.

4) public void close()throws IOException

is used to close the current output stream.

 

 

 

 InputStream class

         InputStream class is an abstract class.

         It is the superclass of all classes representing an input stream of bytes.

Useful methods of InputStream

Method

Description

1) public abstract int read()throws IOException

reads the next byte of data from the input stream. It returns -1 at the end of the file.

2) public int available()throws IOException

returns an estimate of the number of bytes that can be read from the current input stream.

3) public void close()throws IOException

is used to close the current input stream.

 

 


Example:

package com.java.FileStream;

import java.io.FileInputStream;

import java.io.IOException;


public class FileInputStreamExample {

public static void main(String[] args) {

FileInputStream fis = null;

try {

// Open the file

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

int content;

// Read the file until the end

while ((content = fis.read()) != -1) {

// Convert byte to character and print

System.out.print((char) content);

}

} catch (IOException e) {

e.printStackTrace();

} finally {

// Close the file input stream

try {

if (fis != null) fis.close();

} catch (IOException ex) {

ex.printStackTrace();

}

}

}

}

Java FileOutputStream Class

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

         If you have to write primitive values into a file, use FileOutputStream class.

         You can write byte-oriented as well as character-oriented data through FileOutputStream class.

Example:

package com.java.FileStream;

import java.io.FileOutputStream;

import java.io.IOException;


public class FileOutputStreamExample {

public static void main(String[] args) {

FileOutputStream fos = null;

try {

// Open the file in write mode (will overwrite if file exists)

fos = new FileOutputStream("output.txt");


String content = "This is an example of FileOutputStream in Java.";

// Write the string content to the file as bytes

fos.write(content.getBytes());

System.out.println("File written successfully!");

} catch (IOException e) {

e.printStackTrace();

} finally {

// Close the file output stream

try {

if (fos != null) fos.close();

} catch (IOException ex) {

ex.printStackTrace();

}

}

}

}




Post a Comment

0 Comments