Java Byte Streams

Previous
Next

Byte Streams:

  • Processing the data byte by byte.
  • Classes since JDK1.0
  • The 2 main abstract classes are
    • InputStream class
    • OutputStream class
  • Extensions are
    • FileInputStream – to perform read operations
    • FileOutputStream – to perform write operations

Opening the File:

  • We can open file in different modes
  • FileInputStream class is used to open the file in read mode.
  • We construct the File object from FileInputStream
class FileInputStream
{
	public FileInputStream(String path) throws FileNotFoundException
	{
		If the file is present, it opens and returns pointer to file.
		Raises exception if the file is not present.
	}
}

Closing the File:

  • It is recommended to release the resource which is connected to the program.
  • We use finally block to release the resource.
  • FileInputStream class is providing close() method to release the resource.
class FileInputStream
{
	public void close() throws IOException
	{
		Closes this file input stream and releases any system resources associated with the stream.
	}
}

Open and Close the File:

import java.io.*;
class Resource 
{
	static FileInputStream file = null ;
	public static void main(String[] args) 
	{
		try
		{
			file = new FileInputStream("e:/test.txt");
			System.out.println("File opened...");
		}
		catch (FileNotFoundException e1)
		{
			System.out.println("File doesn't exists...");
		}
		finally
		{
			if(file != null)
			{
				try
				{
					file.close();
					System.out.println("File closed...");
				}
				catch (IOException e2)
				{
					System.out.println("IO error");
				}
			}
		}
	}
}

Throwing all exceptions instead of handling:

import java.io.*;
class Resource 
{
	static FileInputStream file = null ;
	public static void main(String[] args) throws Exception
	{
		try
		{
			file = new FileInputStream("e:/test.txt");
			System.out.println("File opened...");
		}
		finally
		{
			if(file != null)
				file.close();
		}
	}
}

Reading a File character by character:

class FileInputStream
{
	public int read()
	{
		on success, it returns ASCII value of character that has read.
		on failure, it returns -1 when it reaches "End of File".
	}
}

import java.io.*;
class ReadInfo 
{
	public static void main(String[] args) throws Exception
	{
		FileInputStream src=null;
		try
		{
			src = new FileInputStream("d:/ReadInfo.java");
			System.out.println("File opened...");

			int x ;
			while((x = src.read()) != -1)
			{
				System.out.print((char)x);
			}
		}
		finally
		{
			if(src != null)
			{
				src.close();
				System.out.println("File closed..");
			}
		}
	}
}

Writing into File:

FileOutputStream class is providing 2 constructors to open file in write mode or append mode.

class FileOutputStream
{
	public FileOutputStream(String path)
	{
		-> Open file in write mode.
		-> If file is present, it opens and removes existing data.
		-> If file is not present, it creates the new file....
	}
	public FileOutputStream(String path, boolean append)
	{
		-> Open file in append mode.
		-> If file is present, it opens and place the cursor at the end of existing data to add new contents.
		-> If file is not present, it creates the new file....
	}
}

Copy the contents of one File to another File:

import java.io.*;
class CopyFile 
{
	public static void main(String[] args) throws Exception
	{
		FileInputStream src=null;
		FileOutputStream dest=null;
		try
		{
			src = new FileInputStream("d:/CopyFile.java");
			dest = new FileOutputStream("d:/Output.txt", true); // append mode

			int x ;
			while((x = src.read()) != -1)
			{
				dest.write(x);
			}
			System.out.println("Data copied....");
		}
		finally
		{
			if(src != null)
				src.close();
			if(dest != null)
				dest.close();
		}
	}
}
Previous
Next

Add Comment

Courses Enquiry Form