Java/Java IO and file Handling

From Meshplex

Jump to: navigation, search
Image:Java_programming.jpg
Introduction to Java
Overview of Java
How to Setup Java on your PC
Java Data Types, Variables,and Arrays
Java Operators
Java Packages, Classes, and Interfaces
Java Program
Modifiers
Java Control Statements
Java Exception Handling
Java Object Oriented Programming
Java Wrappers
Java Strings
Java Math
Java Arrays
Java Random Numbers
Java Date and Time
Java Regular Expressions
Java Collections
Java Generics
Java Thread and Multithreading
Java IO and file Handling
* Java Files & Directories
* Java Byte Oriented IO
* Java Character Oriented IO
* Java Object Serialization
Java Network Programming
Java RMI
Java Image, Audio, Video
Java GUI
Java Applets
Java Internationalization
JDBC
Java and XML

Contents

[edit] IO and File Handling

Java Input Output(IO) is one of the most important topics in Java. Java IO provides many APIs to programmers that he can use to access the files on a filesystem of a computer. Java has many types of IO classes that can be used to read and write to files, memories, sockets. Java's I/O libraries are designed in an abstract way that enables you to read from external data sources and write to external targets, regardless of the kind of thing you're writing to or reading from. You use the same methods to read from a file that you do to read from the console or from a network connection. You use the same methods to write to a file that you do to write to a byte array or a serial port device.


[edit] What is a Stream?

A stream is an ordered sequence of bytes of indeterminate length. Input streams move bytes of data into a Java program from some generally external source. Output streams move bytes of data from Java to some generally external target. An input stream may read from a finite source of bytes such as a file or an unlimited source of bytes such as System.in. Similary in an output may write from a finit source of byte such as a file or an unlimited source of byte such as System.out. Most beginners confuse with the term input stream and out stream. They think the other way round. But if you see input stream as a source of input for your program and output stream as a source of output for your program then concept of input and output stream will be much clear to you.

Java includes a particularly rich set of I/O classes in the core API, mostly in the java.io packages. These packages support several different styles of I/O. One distinction is between byte-oriented I/O, which is handled by input and output streams, and character-I/O, which is handled by readers and writers. These all have their place and are appropriate for different needs and use cases.

All classes are basically divided into two types of categories:

1. Byte Oriented I/O

2. Character Oriented I/O

[edit] Byte Oriented I/O Classes

Programs use byte streams to perform input and output of 8-bit bytes. All byte stream classes are descended from InputStream and OutputStream.
java.io.InputStream (Base Class for InputStream Classes)
java.io.BufferedInputStream
java.io.ByteArrayInputStream
java.io.DataInputStream
java.io.FileInputStream
java.io.FilterInputStream
java.io.ObjectInputStream
java.io.PipedInputStream
java.io.PrintStream
java.io.PushbackInputStream
java.io.SequenceInputStream
java.io.OutputStream (Base Class for OutputStream Classes)
java.io.BufferedOutputStream
java.io.ByteArrayOutputStream
java.io.DataOutputStream
java.io.FileOutputStream
java.io.FilterOutputStream
java.io.ObjectOutputStream
java.io.PipedOutputStream

[edit] Character Oriented I/O Classes

Readers and writers are based on characters, which can have varying widths depending on the character set. For example, ASCII and Latin-1 use 1-byte characters. UTF-32 uses 4-byte characters. UTF-8 uses characters of varying width (between one and four bytes). Since characters are ultimately composed of bytes, readers take their input from streams. However, they convert those bytes into chars according to a specified encoding format before passing them along. Similarly, writers convert chars to bytes according to a specified encoding before writing them onto some underlying stream.
java.io.Reader
java.io.BufferedReader
java.io.CharArrayReader
java.io.FileReader
java.io.FilterReader
java.io.InputStreamReader
java.io.LineNumberReader
java.io.PipedReader
java.io.PushbackReader
java.io.StringReader
java.io.Writer
java.io.BufferedWriter
java.io.CharArrayWriter
java.io.FileWriter
java.io.FilterWriter
java.io.OutputStreamWriter
java.io.PipedWriter
java.io.PrintWriter
java.io.StringWriter

Image:JAVA-IO.jpg