IO
File
import java.io.*;

public class F
{
	public static void main(String args[])
	{
		File f = new File("./");

		//exists
		if(f.exists())
		{
			System.out.println(f+" exist ...");

			//isDirectory
			if(f.isDirectory())
			{
				//list
				String files [] = f.list();
				for(String e : files)
				{
					File temp = new File(e);
					System.out.println(temp.getName());//getName
					System.out.println(temp.getAbsolutePath());//getAbsolutePath
					System.out.println(temp.getPath());//getPath
					System.out.println(temp.getParent());//getParent
					System.out.println(temp.length());//length
					System.out.println();
				}
			}
		}
	}
}
			
  Byte Based Character Based
  Input Output Input Output
Basic InputStream OutputStream Reader
InputStreamReader
Writer
OutputStreamWriter
Arrays ByteArrayInputStream ByteArrayOutputStream CharArrayReader CharArrayWriter
Files FileInputStream
RandomAccessFile
FileOutputStream
RandomAccessFile
FileReader FileWriter
Pipes PipedInputStream PipedOutputStream PipedReader PipedWriter
Buffering BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter
Filtering FilterInputStream FilterOutputStream FilterReader FilterWriter
Parsing PushbackInputStream
StreamTokenizer
  PushbackReader
LineNumberReader
 
Strings     StringReader StringWriter
Data DataInputStream DataOutputStream  
Data - Formatted   PrintStream   PrintWriter
Objects ObjectInputStream ObjectOutputStream    
Utilities SequenceInputStream
     
Overview in Jenkov's Tutorial
Bytes Stream
Bytes Stream

//FileInputStream, FileOutputStream
import java.io.*;

public class S
{
	public static void main(String args[]) throws IOException
	{
		FileOutputStream out = null;

		try
		{
			out = new FileOutputStream("output2.txt");

			//output string "12"
			out.write("1".getBytes());
			out.write("2".getBytes());

			//output number 12
			out.write(" ".getBytes());
			out.write(3);
			out.write(4);
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
		finally
		{
			if(out != null)
				out.close();
		}
	}
}
			
import java.io.*;

public class S3
{
	public static void main(String args[]) throws IOException
	{
		FileInputStream in = null;

		try
		{
			in = new FileInputStream("output2.txt");

			int n = 0, c;
			while((c = in.read()) != -1)
			{
				//read string number
				if(c >= '0')
				{
					while(c >= '0' && c <= '9')
					{
						n = n*10 + c - '0';//convert byte to number
						c = in.read();
					}
					System.out.println(n);
					n = 0;
				}
				//read binary number
				if( c < 10)
				{
					while(c != -1 && c < 10)
					{
						n = n*10+c;
						c = in.read();
					}
					System.out.println(n);
					n = 0;
				}
			}
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
		finally
		{
			if(in != null)
				in.close();
		}
	}
}
			
//DataInputStream, DataOutputStream
//Wrtite 00102010 by FileOutputStream byte by byte
import java.io.*;

public class S
{
	public static void main(String args[]) throws IOException
	{
		FileOutputStream out = null;

		try
		{
			out = new FileOutputStream("output.txt");

			out.write(0);
			out.write(0);
			out.write(1);
			out.write(0);

			out.write(2);;
			out.write(0);
			out.write(1);
			out.write(0);
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
		finally
		{
			if(out != null)
				out.close();
		}
	}
}
			
//read 256 and 33554688
import java.io.*;

public class S3
{
	public static void main(String args[]) throws IOException
	{
		DataInputStream ds = null;

		try
		{
			ds = new DataInputStream(new FileInputStream("output.txt"));

			int temp;
			while(ds.available() > 0)
				System.out.println(ds.readInt());//read four bytes each time
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
		finally
		{
			if(ds != null)
				ds.close();
		}
	}
}
			
//PipedInputStream, PipedOutputStream
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class P {

    public static void main(String[] args) throws IOException {

        final PipedOutputStream output = new PipedOutputStream();
        final PipedInputStream  input  = new PipedInputStream(output);


        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
			System.out.println("Thread 1 write message to pipe ...");
                    output.write("Hello world, pipe!\n".getBytes());
                } catch (IOException e) {
                }
            }
        });


        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
			System.out.println("Thread 2 receive messge from pipe ...");
		    int data;
                    while((data = input.read()) != -1){
                        System.out.print((char) data);
                    }
                } catch (IOException e) {
                }
            }
        });

        thread1.start();
        thread2.start();

    }
}
			
//ByteArrayInputStream, BytesArrayOutputStream
import java.io.*;

public class B
{
	//convert four bytes to integer
	public static int bytes2int(byte [] array)
	{
		int n = 0;
		for(int i = 0; i < 4; i++)
		{
			n <<= 8;
			n |= array[i] & 0xff;
		}

		return n;
	}

	public static void main(String args[])
	{
		ByteArrayInputStream ba = new ByteArrayInputStream("Hello World!".getBytes());

		byte [] array = new byte[4];

		//read bytes
		System.out.println("Read the first four bytes ...");
		ba.read(array, 0, 4);

		for(int i = 0; i < array.length; i++)
			System.out.println(array[i]+" "+(char)array[i]);

		//bytes2int
		System.out.println("Number: "+bytes2int(array));

		//read the rest of bytes
		System.out.println("Read the rest of bytes ...");
		int b;
		while((b = ba.read()) != -1)
		{
			System.out.println(b+" "+(char)b);
		}
	}
}
			
//PushbackInputStream
import java.io.*;

public class P
{
	public static void main(String args[]) throws IOException
	{
		PushbackInputStream ps = new PushbackInputStream(new ByteArrayInputStream("Hello World!".getBytes()), 100);//set up pushback buffer to be 100

		int n;

		n = ps.read();//read a byte
		System.out.println("Read: "+(char)n);

		byte [] array;
		array = "Pushback ".getBytes();

		//push several bytes back to stream
		ps.unread(array);

		//output stream
		while((n = ps.read()) != -1)
		{
			System.out.println((char)n);
		}
	}
}
			
//BufferedInputStream, BUfferedOutputStream
import java.io.*;

public class B
{
	public static void main(String args[]) throws IOException
	{
		InputStream in = new BufferedInputStream(new FileInputStream("temp.txt"), 8*1024);//set up buffer size to 8k

		byte [] array = new byte[10];
		in.read(array, 0, 4);

		System.out.println(new String(array));

		in.mark(2);

		System.out.println((char)in.read());
		System.out.println((char)in.read());

		in.reset();

		System.out.println((char)in.read());
		System.out.println((char)in.read());
		System.out.println((char)in.read());
		System.out.println((char)in.read());
	}
}
			
//SequenceInputStream
import java.io.*;

public class S
{
	public static void main(String args[]) throws IOException
	{
		InputStream in = new SequenceInputStream(new ByteArrayInputStream("Hello ".getBytes()), new ByteArrayInputStream("World!".getBytes()));

		int n;

		while((n = in.read()) != -1)
		{
			System.out.println((char)n);
		}
	}
}
			
StringBufferInputStream
//StringBufferInputStream
import java.io.*;

public class S
{
	public static void main(String args[])
	{
		try(InputStream in = new StringBufferInputStream("Hello World!"))
		{
			int n;

			while((n = in.read()) != -1)
			{
				System.out.println();
			}
		}
	}
}
			
// Format output, Formatter
import java.util.*;

public class F
{
	public static void main(String args[])
	{
		Formatter output = null;

		// Open output file
		try
		{
			output = new Formatter("output.txt");

			// Format output
			output.format("%20s%20s%n", "First Name:", "Lin");
			output.format("%20s%20s%n", "Last Name:", "Chen");
			output.format("%20s%20s%n", "Age: ", "38");
			output.format("%20s%20s%n", "University:", "ECSU");
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
		finally
		{
			if(output != null)
				output.close();
		}

	}
}
			
// Format input, Scanner
import java.util.*;
import java.io.*;

public class S
{
	public static void main(String args[])
	{
		Scanner input = null;

		try
		{
			input = new Scanner(new FileInputStream("output.txt"));

			while(input.hasNext())
			{
				String temp = input.nextLine();
				System.out.printf("%20s%20s%n", temp.substring(0, 20), temp.substring(20, 40));
			}
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
		finally
		{
			if(input != null)
				input.close();
		}
	}
}
			
AccessRandomFile
//AccessRandomFile
import java.io.IOException;
import java.io.RandomAccessFile;

public class R {
    static final String FILEPATH = "temp.txt";
    public static void main(String[] args) {
        try {
        	RandomAccessFile file = new RandomAccessFile(FILEPATH, "rw");

		//toEnd
		toEnd(file);

		//readFromFile
            	System.out.println(new String(readFromFile(file, 10, 4)));

		//writeToFile
            	writeToFile(file, "JavaCodeGeeks Rocks!", 2);

		//close file
		file.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void toEnd(RandomAccessFile f) throws IOException
    {
	    System.out.println("Length: "+f.length());//check file length
	    f.seek(f.length()-2);//move the pointer to the second last character
	    System.out.println((char)f.read());
    }

    private static byte[] readFromFile(RandomAccessFile file, int position, int size)
            throws IOException {
        file.seek(position);
        byte[] bytes = new byte[size];
        file.read(bytes);
        return bytes;
    }

    private static void writeToFile(RandomAccessFile file, String data, int position)
            throws IOException {
        file.seek(position);
        file.write(data.getBytes());
    }
}

			
Serializable
import java.io.*;

public class Part implements Serializable
{
	private String name;

	public Part()
	{
		name = "unknown";
	}

	public String toString()
	{
		return name;
	}
}
			
import java.io.*;

public class Car implements Serializable
{
	private String maker;
	private Part p;

	public Car(String m)
	{
		maker = m;
		p = new Part();
	}

	public String getMaker()
	{
		return maker;
	}

	public void setMaker(String m)
	{
		maker = m;
	}

	public String toString()
	{
		return "Car: "+maker+" "+p.toString();
	}
}
			
//ObjectInputStream, ObjectOutputStream
import java.io.*;

public class S
{
	public static void main(String args[]) throws IOException
	{
		ObjectOutputStream output = null;

		try
		{
			output = new ObjectOutputStream(new FileOutputStream("data.dat"));
			output.writeObject(new Car("Buick"));
			output.writeObject(new Car("Honda"));
			output.writeObject("Hello World!");
			output.writeObject(123);
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
		finally
		{
			if(output != null)
				output.close();
		}

	}
}
			
import java.io.*;

public class S2
{
	public static void main(String args[]) throws IOException
	{
		ObjectInputStream input = null;

		try
		{
			input = new ObjectInputStream(new FileInputStream("data.dat"));
			Car c = (Car)input.readObject();
			System.out.println(c);
			Car c2 = (Car)input.readObject();
			System.out.println(c2);
			String s = (String)input.readObject();
			System.out.println(s);
			int n = (int)input.readObject();
			System.out.println(n);
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
		finally
		{
			if(input != null)
				input.close();
		}

	}
}
			
Character Stream

//FileReader, FileWriter
import java.io.*;

public class S
{
	public static void main(String args[]) throws IOException
	{
		FileWriter out = null;

		try
		{
			out = new FileWriter("output.txt");

			for(int i = 0; i < 10; i++)
				out.write(Integer.toString(i));
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
		finally
		{
			if(out != null)
				out.close();
		}
	}
}
			
import java.io.*;

public class S2
{
	public static void main(String args[]) throws IOException
	{
		FileReader in = null;

		try
		{
			in = new FileReader("output.txt");

			int c;
			while((c = in.read()) != -1)
				System.out.println((char)c);
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
		finally
		{
			if(in != null)
				in.close();
		}
	}
}
			
//BufferedReader
import java.util.*;
import java.io.*;

public class S
{
	public static void main(String args[]) throws IOException
	{
		BufferedReader br = null;

		try
		{
			br = new BufferedReader(new InputStreamReader(new FileInputStream("output.txt")));
			String line;
			while((line = br.readLine()) != null)
			{
				//System.out.println(line);
				StringTokenizer st = new StringTokenizer(line);
				System.out.printf("%20s%20s%n", st.nextToken()+" "+st.nextToken(), st.nextToken());
			}
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
		finally
		{
			if(br != null)
				br.close();
		}
	}
}
			
//LineNumberReader
import java.io.*;

public class L
{
	public static void main(String args[])
	{
		try(LineNumberReader l = new LineNumberReader(new FileReader("L.java")))
		{
			int n;

			while((n = l.read()) != -1)
			{
				System.out.println("Line: "+l.getLineNumber()+" "+(char)n);
			}
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
	}
}
			
Standard Stream
import java.util.Scanner;
import java.io.*;

public class S
{
	public static void main(String args[]) throws IOException
	{
		Scanner s = new Scanner(System.in);

		int temp;
		temp = s.nextInt();

		try
		{
			temp = 100/temp;
			System.out.println(temp);
		}
		catch (Exception e)//if temp is 0
		{
			System.err.println(e);
		}

		PrintStream pOut = new PrintStream(new FileOutputStream("output.txt"));

		System.setOut(pOut);

		System.out.println("Hello World!");
	}
}
			
Character Stream
			
Reference