The 1.Java I/O framework uses the adorner mode, which can be divided into byte stream and character stream according to the reading and writing methods, both of which encapsulate different classes (ByteArray, String, File, pipe, etc.) depending on the data source, FilterInputStream, Filteroutputstream is the two class used to provide an adorner class interface to control a particular input stream and output stream. The byte stream and the character stream can be converted to and from each other through InputStreamReader and OutputStreamWriter.
2. Common cases
Import Java.io.bufferedreader;import java.io.file;import java.io.filereader;/* * Enter a file in the form of a character buffer */public class bufferedinputfile {static int count = 0;//buffer reads a specified file public static string read (String fileName) {stringbuffer sb = new Strin Gbuffer (); try {bufferedreader in = new BufferedReader (new FileReader (new File)); String S;while ((s = in.readline ()) = null) {Count = count + 1;sb.append (s + "\ n");} In.close ();} catch (Exception e) {e.printstacktrace ();} return sb.tostring ();} public static void Main (string[] args) {System.out.print (Read ("e:/j2ee/new Folder/1.txt")); System.out.println ("" + Count);}}
Import Java.io.ioexception;import Java.io.stringreader;public class Memoryinput {public static void main (string[] args) {StringReader in = new StringReader (Bufferedinputfile.read ("e:/j2ee/new Folder/2.txt"); int C;try {while ((c = In.read ())! =- 1) {System.out.println ((char) c);} In.close ();} catch (IOException e) {e.printstacktrace ();}}}
Import Java.io.bytearrayinputstream;import Java.io.datainputstream;import Java.io.eofexception;public class formattedmemoryinput {public static void main (string[] args) {DataInputStream in = new DataInputStream (New bytearrayinput Stream (Bufferedinputfile.read ("e:/j2ee/new Folder/2.txt"). GetBytes ()); Try{while (True) {System.out.println ((char) In.readbyte ());}} catch (Eofexception e) {System.out.println ("stream end"); E.printstacktrace ();} catch (Exception e) {e.printstacktrace ();}}}
Import Java.io.bufferedreader;import java.io.filewriter;import java.io.ioexception;import java.io.StringReader;/* * Output content to file */public class Basicfileoutput {public static void main (string[] args) throws IOException {BufferedReader in = new BufferedReader (New StringReader (Bufferedinputfile.read ("e:/j2ee/new Folder/2.txt")); FileWriter out = new FileWriter ("e:/j2ee/new Folder/2_copy.txt"); String S;while ((s = in.readline ()) = null) {Out.write (s + "\ r \ n");//Add a carriage return at the end of each line}in.close (); Out.close ();}}
Import Java.io.bufferedinputstream;import Java.io.bufferedoutputstream;import Java.io.datainputstream;import Java.io.dataoutputstream;import Java.io.fileinputstream;import Java.io.fileoutputstream;import java.io.ioexception;/* * Use DataInputStream to write data * use DataOutputStream to recover data */public class Storeandrecoverdata {public static void Main (string[] args) throws IOException {DataOutputStream out = new DataOutputStream (New Bufferedoutputstream ( New FileOutputStream ("e:/j2ee/new Folder/3.txt")), Out.writechars ("CC");//writes the string in character order Out.writeutf ("write in UTF");// Write Out.writedouble (3.141659) using UTF-8 encoding; Out.writeboolean (true); Out.writeint (1212); Out.close ();D atainputstream in = New DataInputStream New Bufferedinputstream (New FileInputStream ("e:/j2ee/new Folder/3.txt")); System.out.println (In.readchar ()); In.skipbytes (2); System.out.println (In.readutf ()); System.out.println (In.readboolean ()); System.out.println (In.readint ());}}
3. Compression
The Java.util.zip package encapsulates classes that read and write compressed format data, most of which inherit from related classes in the Java.io package.
CheckSum: An interface that represents data checksums, which provides two types of validation: Adler32, CRC32
CheckedOutputStream(OutputStream out, Checksum cksum) 可以创建一个使用制定校验和的OutputStream
CheckedInputStream (inputstream in , Checksum cksum) can create a inputstream that uses the set checksum
Compression class: Zipoutputstream: Compress data into zip format Gzipoutputstream compress data into gzip format
Decompression class: Zipinputstram: Data for extracting zip format Gzipinputstram: Extract data in gzip file format
Example 1: Simple compression with gzip
/* * Simple compression using gzip */public class Gzipcompress {public static void main (string[] args) throws IOException {Bufferedread Er in = new BufferedReader (New FileReader ("e:/j2ee/new Folder/2.txt")); Bufferedoutputstream out = new Bufferedoutputstream (New FileOutputStream ("e:/j2ee/new Folder/2.gz")) ); int C;while ((c = In.read ())! =-1) out.write (c); In.close (); Out.close ();//gzipinputstream is a byte stream, To use ReadLine, you must convert through InputStreamReader bufferedreader gin = new BufferedReader (New InputStreamReader (New FileInputStream ("e:/j2ee/new Folder/2.gz"))); String S;while ((s = gin.readline ()) = null) {SYSTEM.OUT.PRINTLN (s);}}}
Example 2:
/* Multi-file compression with zip */public class Zipcompress {public static void main (string[] args) throws IOException {Fileoutputstr EAM fos = new FileOutputStream ("e:/j2ee/new Folder/1_2_3.zip"); Checkedoutputstream cos = new Checkedoutputstream (FOS, New Adler32 ()); Zipoutputstream Zos = new Zipoutputstream (COS); Bufferedoutputstream out = new Bufferedoutputstream (ZOS); Zos.setcomment ("1.txt 2.txt 3.txt Compressed file"); string [] FileNames = {"e:/j2ee/new Folder/1.txt", "e:/j2ee/new Folder/2.txt", "e:/j2ee/new Folder/3.txt"};for (String name:filenames {BufferedReader in = new BufferedReader (new FileReader (name)); Zos.putnextentry (new ZipEntry (name)); int C;while (c = In.read ())! =-1) {out.write (c);} In.close (); Out.flush (); Each loop should empty the buffer (write one time file), otherwise it will cause all content to be written to the last file}out.close ();}}
Java I/O parsing and java.util.zip compression