Java I/O Interview Questions

Top FREQUENTLY ASKED JAVA I/O INTERVIEW QUESTIONS

What is Java I/O ?

It is Input and Output to process input stream and produce output stream.In java file handling is done by I/O API.

What is Stream ?

It is sequence of data.In java it is composed of bytes.

Give example of all three stream in java ?

  • System.in : it is an input stream.
  • 
    int i=System.in.read();//returns ASCII code of 1st character  
    System.out.println((char)i);
    
    
  • System.err : Its an error stream.
  • 
    System.err.println("error");
    
    
  • System.out : Its an output stream.
  • 
    System.out.println("Output Message");
    
    

What is InputStream ?

It is used to read data from source like file.It is an abstract class and super class of all classes that works on input stream of bytes.

What are the subclasses of InputStream ?

  • FileInputStream
  • FilterInputStream
  • ByteArrayInputStream
  • PipedInputStream
  • ObjectInputStream

What is FileInputStream and Give one example ?

It is an input stream that is used to read byte from a file.

package javaio.example;

import java.io.FileInputStream;

public class FileInputStreamExample {
	public static void main(String args[]) {
		try {
			FileInputStream fileInputStream = new FileInputStream(
					"C:\\javaio\\example.txt");
			int i = 0;
			while ((i = fileInputStream.read()) != -1) {
				System.out.print((char) i);
			}
			fileInputStream.close();
		} catch (Exception e) {
			System.out.println(e);
		}
	}
}

We have example.txt at mentioned place and there we have content Learning Java IO .FileInputStream reads data from file as byte and convert it to character.Output is as follows.

Learning Java IO

What is ByteArrayInputStream and Give one example ?

It is used to read byte array as input stream.It contains an internal buffer.In ByteArrayInputStream data is read from byte array.As data increases size of buffer also increases.

package javaio.example;

import java.io.ByteArrayInputStream;

public class ByteArrayInputStreamExample {
	public static void main(String args[]) {
		byte[] buf = { 1, 2, 3, 4 };
		int temp = 0;
		ByteArrayInputStream byt = new ByteArrayInputStream(buf);
		while ((temp = byt.read()) != -1) {
			System.out.println(temp);
		}
	}
}

Output is as follows.

1
2
3
4

What are the subclasses of OutputStream ?

  • FileOutputStream
  • FilterOutputStream
  • ByteArrayOutputStream
  • PipedOutputStream
  • ObjectOutputStream

What is FileOutputStream and Give one example ?

It is an output stream that is used to write data to file.

package javaio.example;

import java.io.FileOutputStream;

public class FileOutputStreamExample {
	public static void main(String args[]) {
		String st = "Learning Java IO";
		System.out.println(st);
		try {
			FileOutputStream fileOutputStream = new FileOutputStream("C:\\javaio\\example.txt");
			byte b[] = st.getBytes();
			fileOutputStream.write(b);
			fileOutputStream.close();
			System.out.println("Complete");
		} catch (Exception e) {
			System.out.println(e);
		}
	}
}

If we see output in example.txt we will get

Learning Java IO

What is FileInputStream and Give one example ?

It is an input stream that is used to read byte from a file.

package javaio.example;

import java.io.FileInputStream;

public class FileInputStreamExample {
	public static void main(String args[]) {
		try {
			FileInputStream fileInputStream = new FileInputStream(
					"C:\\javaio\\example.txt");
			int i = 0;
			while ((i = fileInputStream.read()) != -1) {
				System.out.print((char) i);
			}
			fileInputStream.close();
		} catch (Exception e) {
			System.out.println(e);
		}
	}
}

We have example.txt at mentioned place and there we have content Learning Java IO .FileInputStream reads data from file as byte and convert it to character.Output is as follows.

Learning Java IO



Visit Others Links

Interview Questions Top Elasticsearch Interview Question
Top Kubernetes Interview Question
Top HTTP Interview Question
Top PouchDB Interview Question
Top Pivotal Cloud Foundry (pcf) Interview Question
Top Apache Camel Interview Question
Top Docker Interview Question
Top Gradle Interview Question
Top Generics Interview Questions In Java
Java 9 Interview Questions
Top Maven Interview Question
Top RabbitMQ Interview Question
Top Spring Batch Interview Question
Top Spring Boot Interview Question
Top Tika Interview Question
Top Kotlin Interview Question
Top OpenShift Interview Question
Top Polymer Interview Question
Top WebGL Interview Question
Top Continuous Integration Interview Question
Top Spring Cloud Interview Question
Top JOGL Interview Question
Top JFreeChart Interview Question
Java MultiThreading Interview Question
Top Spring ROO Interview Question
Top Seaborn Interview Question
Top TestRail Interview Question
Top LolCode Interview Question
Top Spring Transaction Management Interview Question
Top DynamoDB Interview Question
Top PySpark Interview Question
Top SaltStack Interview Question
Top Spring AOP Interview Question
© 2019 PreparationForInterview.com