Input Output
Input Output
io (Input/ Output)
Java I/O (Input and Output) is used to process the input and produce the output.
System.out.println("simple message");
System.err.println("error message");
int i=System.in.read();
System.out.println((char)i);
OutputStream
Java application uses an output stream to write data to a destination; it may be a file, an array, peripheral
device or socket.
InputStream
Java application uses an input stream to read data from a source; it may be a file, an array, peripheral
device or socket.
OutputStream class
OutputStream class is an abstract class. It is the superclass of all classes representing an output stream of
bytes. An output stream output bytes and sends them to some sink.
methods of OutputStream
1) public void write(int)throws IOExceptionis 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.
methods of InputStream
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.
Writer is an abstract class for writing to character streams. The methods that a subclass must implement
are write(char[], int, int), flush(), and close().
Example Program
import java.io.*;
public class WriterExample {
public static void main(String[] args) {
try {
Writer w = new FileWriter("output.txt");
String content = "I love my country";
w.write(content);
w.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
} }
Java Program to copy one file to another
import java.io.*;
import java.util.*;
public class CopyFromFileaToFileb {
public static void copyContent(File a, File b)
throws Exception
{
FileInputStream in = new FileInputStream(a);
FileOutputStream out = new FileOutputStream(b);
try {
int n;
// read() function to read the
// byte of data
while ((n = in.read()) != -1) {
// write() function to write
// the byte of data
out.write(n);
}
}
finally {
if (in != null) {
// close() function to close the
// stream
in.close();
}
// close() function to close
// the stream
if (out != null) {
out.close();
}
}
System.out.println("File Copied");
}
public static void main(String[] args) throws Exception
{
Scanner sc = new Scanner(System.in);
// get the source file name
System.out.println("Enter the source filename from where you have to read/copy :");
String a = sc.nextLine();
// source file
File x = new File(a);
// get the destination file name
System.out.println("Enter the destination filename where you have to write/paste :");
String b = sc.nextLine();
// destination file
File y = new File(b);
// method called to copy the
// contents from x to y
copyContent(x, y);
} }
Read a line of text in Java
Supported Methods
nextX() methods return the type of value
nextInt(), nextByte(), nextShort(), next(), nextLine(), nextDouble(), nextFloat(), nextBoolean() etc.
Example Program-1
import java.util.*;
public class ScannerExample {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = in.nextLine();
System.out.println("Name is: " + name);
in.close();
}
}
Example Program-2
import java.util.*;
public class ScannerClassExample1 {
public static void main(String args[]){
String s = "Hello, This is JavaTpoint.";
//Create scanner Object and pass string in it
Scanner scan = new Scanner(s);
//Check if the scanner has a token
System.out.println("Boolean Result: " + scan.hasNext());
//Print the string
System.out.println("String: " +scan.nextLine());
scan.close();
System.out.println("--------Enter Your Details-------- ");
Scanner in = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = in.next();
System.out.println("Name: " + name);
System.out.print("Enter your age: ");
int i = in.nextInt();
System.out.println("Age: " + i);
System.out.print("Enter your salary: ");
double d = in.nextDouble();
System.out.println("Salary: " + d);
in.close();
}
}
2. Using public class BufferedReader extends Reader
Example Program-1:
import java.io.*;
public class BufferedReaderExample {
public static void main(String args[])throws Exception{
FileReader fr=new FileReader("D:\\test.txt");
BufferedReader br=new BufferedReader(fr);
int i;
while((i=br.read())!=-1){
System.out.print((char)i);
}
br.close();
fr.close();
}
}
Example Program-2:
import java.io.*;
public class BufferedReaderExample{
public static void main(String args[])throws Exception{
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);
String name="";
while(!name.equals("stop")){
System.out.println("Enter data: ");
name=br.readLine();
System.out.println("data is: "+name);
}
br.close();
r.close();
}
}