1. Basic Concepts
By data type: Java has two kinds of byte stream and character streams:
BYTE throttling: Inputstream/outputstream
Character streams: Reader/writer
When a byte stream is read, a byte is returned when it is read.
The character stream reads one or more bytes using a byte (the Chinese corresponding byte is two, the UTF-8 Code table is 3), first go to the specified encoding table, will find the characters returned.
2. Common sub-classes
The byte stream ends with Inputstream/outputstream/stream, Inputstream/outputstream the front represents the function of the stream, they all inherit the Inputstream/outputstream,
For example, Fileinputstream/fileinputstream represents the byte stream for manipulating the contents of the file, PrintStream represents the byte stream used to format the print;
Character streams all end with Reader/writer, reader/writer the previous function of the stream, they all inherit Reader/writer,
For example, Filereader/filewriter represents a stream of characters that manipulate the contents of a file, PrintWriter represents a stream of characters used to format the print.
2. Use Occasion
The byte stream can handle all types of data, such as pictures, MP3, etc.
A character stream can only handle characters data.
The following is an instance of using a stream of bytes and character streams:
Fileio.java file:
Package Com.swsx.io;
Import Java.io.File;
/**
* Interface for file input/output operation
* @author Administrator
*/public
interface FileIO {
String Class_path = System.getproperty ("Java.class.path") + File.separator;
/**
* line breaks
/String line_separator = System.getproperty ("Line.separator");
/**
* Create a document
* @param filename
* @return
/boolean CreateFile (String filename);
/**
* Delete a file
* @param filename
* @return
/boolean DeleteFile (String filename);
/**
* Writes content to the file
* @param filename
* @param content
/void WriteFile (string filename, string content);
/**
* Write content to the file
* @param fileName *
@param content
* @param append whether to append/
void WriteFile (string fileName, String content, Boolean append);
/** *
Read the contents of the document
* @param filename
* @return
/String ReadFile (String fileName);
}
Abstractfileio.java file:
Package Com.swsx.io;
Import Java.io.File;
Import java.io.IOException; /** * Implementation FileIO abstract class * @author Administrator */Public abstract class Abstractfileio implements FileIO {public Abst Ractfileio () {//TODO auto-generated constructor stub} @Override public Boolean CreateFile (String fileName) {// TODO auto-generated Method Stub if (fileName!= null &&!filename.equals ("")) {File File = new file (class_
PATH + fileName);
try {return file.createnewfile ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
return false; @Override public boolean DeleteFile (String filename) {//TODO auto-generated method stub if (filename!= null &A
mp;&!filename.equals ("")) {File File = new file (Class_path + fileName);
return File.delete ();
return false; @Override public void WriteFile (string fileName, string content) {//TODO auto-generated Method stub WriteFile (f Ilename, COntent, false);
@Override public abstract void WriteFile (string fileName, String content, Boolean append);
@Override Public abstract String ReadFile (string fileName);
}
Filestreamio.java file:
Package Com.swsx.io;
Import Java.io.ByteArrayOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.io.PrintStream; /** * File Input output operation byte throttle implementation * @author Administrator */public class Filestreamio extends Abstractfileio {public filestre
Amio () {super (); TODO auto-generated Constructor stub} @Override public void WriteFile (string fileName, String content, Boolean app End) {if (fileName!= null && content!= null &&!filename.equals ("")) {byte[] b = content.getbytes
();
OutputStream output = null;
try {output = new FileOutputStream (Class_path + fileName, append);
Output.write (b);
catch (FileNotFoundException e) {//TODO auto-generated catch block E.printstacktrace (); catch (IOException e) {//TODO auto-generated catch block e.printsTacktrace ();
}finally{if (output!= null) {try {output.close ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); }}}//Method One: Save all file contents (possibly overflow)//@Override//Public String ReadFile (string fileName) {///if (f) with a predetermined length byte array
Ilename!= null &&!filename.equals ("")) {//File File = new file (Class_path + fileName);
InputStream input = null; Byte[] B = new byte[1024];//file contents read here//try {//input = new FileInputStream (file);//int temp = 0;//
int count = 0;
while (temp = Input.read ())!=-1) {//b[count++] = (byte) temp;//file is too large, it may overflow//}//Return new String (b); catch (FileNotFoundException e) {///TODO auto-generated catch block//E.printstacktrace ();//} catch (IOException e)
{////TODO auto-generated catch block//E.printstacktrace ();//}finally{//if (input!= null) {//try {
Input.close (); } catch (IOException e) {///TODO auto-generated catch block//E.printstacktrace ();//}/////}////
return null; //Method Two: Use Bytearrayoutputstream to generate a byte array that stores the contents of the file (you can avoid content overflow) @Override public string ReadFile (string fileName) {if f
Ilename!= null &&!filename.equals ("")) {File File = new file (Class_path + fileName);
InputStream input = null;
Just a buffer file full content read in Bytearrayoutputstream byte[] b = new BYTE[16];
Bytearrayoutputstream out = new Bytearrayoutputstream ();
try {input = new FileInputStream (file);
int len = 0; while (len = Input.read (b))!=-1) {out.write (b, 0, Len);//bytearrayoutputstream does not overflow} return new String. Tobytearray ());//convert content in Bytearrayoutputstream to byte array} catch (FileNotFoundException e) {//TODO auto-generated catch
Block E.printstacktrace ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); }finally{if (input!= null) {try {Input.close ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
}}} return null; /** * Format Write file * @param fileName * @param append * @param format * @param args/public static void Print File (String FileName, Boolean append, string format, Object...args) {if (filename!= null && format!= NULL &
&!filename.equals ("")) {PrintStream pt = null;
try {pt =new printstream (New FileOutputStream (Class_path + fileName, append));
pt.printf (format, args);
catch (FileNotFoundException e) {//TODO auto-generated catch block E.printstacktrace ();
}finally{if (pt!= null) {pt.close ();
}
}
}
}
}
Filecharacterio.java file:
Package Com.swsx.io;
Import Java.io.File;
Import java.io.FileNotFoundException;
Import Java.io.FileReader;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.io.Reader;
Import Java.io.Writer; /** * File Input output operation character Stream implementation * @author Administrator */public class Filecharacterio extends Abstractfileio {public Filech
Aracterio () {super (); TODO auto-generated Constructor stub} @Override public void WriteFile (string fileName, String content, Boolean app End) {//TODO auto-generated Method stub if (fileName!= null && content!= null &&!filename.equals ("
{Writer out = null;
try {out = new FileWriter (Class_path + fileName, append);
Out.write (content);
catch (FileNotFoundException e) {//TODO auto-generated catch block E.printstacktrace ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
}finally{if (out!= null) {try {out.close (); catch (IOEXception e) {//TODO auto-generated catch block E.printstacktrace (); }}}//Method One: Save all file contents (possibly overflow)//@Override//public string ReadFile (string fileName) {///// TODO auto-generated Method Stub//if (fileName!= null &&!filename.equals ("")) {//File File = new file (CLASS
_path + fileName);
Reader in = null; Char[] C = new char[1024];//file contents read here//try {//in = new FileReader (file);//int temp = 0;//int Coun
t = 0; while (temp = In.read ())!=-1) {//c[count++] = (char) temp;//file may overflow////////////////Return to new String
(c); catch (FileNotFoundException e) {///TODO auto-generated catch block//E.printstacktrace ();//} catch (IOException e) {////TODO auto-generated catch blocks//E.printstacktrace ();//}finally{//if (in!= null) {//try {//
In.close (); catch (IOException e) {////TODO auto-generated catch block//E.PRINTSTAcktrace ();
}/////return null; //Method Two: Store the contents of the file using StringBuffer or StringBuilder (you can avoid content overflow) @Override public string ReadFile (string fileName) {//To Do auto-generated method stub if (fileName!= null &&!filename.equals ("")) {File File = new file (Class_path +
FileName);
Reader in = null;
Just a buffer file full content read to StringBuffer or StringBuilder char[] C = new CHAR[16];
StringBuffer or StringBuilder stores the contents of the file StringBuffer Strbuffer = new StringBuffer ();
try {in = new FileReader (file);
int len = 0;
while (len = In.read (c))!=-1) {strbuffer.append (c, 0, Len);//Does not overflow} return strbuffer.tostring ();
catch (FileNotFoundException e) {//TODO auto-generated catch block E.printstacktrace ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
}finally{if (in!= null) {try {in.close (); catch (IOException e) {//TODO auto-generated catch BloCK E.printstacktrace ();
}}} return null;
}
}
Testfileio.java file:
Package Com.swsx.io;
public class Testfileio {public
static void Main (string[] agrs) {
FileIO FileIO = new Filecharacterio ();
String fileName = "Test1.txt";
if (Fileio.createfile (FileName)) {
System.out.println ("Success");
}
String content = "Hello!" + fileio.line_separator + "Hello" + fileio.line_separator;
Fileio.writefile (filename,content);
Filestreamio.printfile (Filename,true, "name:%s Age:%d%s", "John", 3, fileio.line_separator);
System.out.println (Fileio.readfile (FileName));
Fileio.deletefile (FileName);
}
Output:
//success
//Hello!
Hello
//Name: John Age: 3