One: Print flow
/*system.out.println () redirect output *//*public static void Main (string[] args) {System.out.println ("MM");//output directly to console file file= New File ("D:" +file.separator+ "Mm.txt"), try {system.setout (new PrintStream (New FileOutputStream (File));} catch ( FileNotFoundException e) {e.printstacktrace ();} System.out.println ("These things can be seen in the file!") ");} *//*system.err Redirection This example also prompts us to save the error message using this method *//*public static void Main (string[] args) {file File=new file ("D:" + file.separator+ "Mm.txt"); System.out.println ("These are output in the console!") "); try {system.seterr (new PrintStream (new FileOutputStream (file));} catch (FileNotFoundException e) {e.printstacktrace ();} System.out.println ("These things can be seen in the file!") ");} A small example of *//*bufferedreader Note: BufferedReader can only accept buffers of character streams, because each text needs to occupy two bytes, So we need to system.in this byte input into a character input stream, using: bufferedreader buf = new BufferedReader (new InputStreamReader (system.in)); */public static void Main (string[] args) {BufferedReader bufferedreader=new BufferedReader (New InputStreamReader (system.in)); String Str=null; System.out.println ("Please enter content:"); try {Str=bufferedreAder.readline ();} catch (IOException e) {e.printstacktrace ();} System.out.println ("The content you entered is:" +STR);}
Two: Merge flow:
Package Com.js.ai.modules.pointwall.testxfz;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.sequenceinputstream;/* Merge two text files into another text file */public Class Sequenceinputstreamdemo {public static void main (string[] args) throws IOException {file File1=new file ("D:" +file.s eparator+ "Hello1.txt"); File File2=new file ("D:" +file.separator+ "Hello2.txt"); File File3=new file ("D:" +file.separator+ "Hello.txt"); InputStream inputstream1=new FileInputStream (file1); I Nputstream inputstream2=new FileInputStream (file2); OutputStream outputstream=new FileOutputStream (FILE3);// Merge stream Sequenceinputstream sequenceinputstream=new sequenceinputstream (inputStream1, inputStream2); int Temp=0;while (( Temp=sequenceinputstream.read ())! = ( -1)) {outputstream.write (temp);} Inputstream1.close (); Inputstream2.close (); Outputstream.close (); Sequenceinputstream.close ();}}
Three: File compression:
/* Compress single File */public class ZipOutputStreamDemo1 {public static void main (string[] args) throws IOException {file File=new file ("D:" +file.separator+ "Hello.txt"); File Zipfile=new file ("D:" +file.separator+ "Hello.zip"); InputStream inputstream=new fileinputstream (file); Zipoutputstream zipoutputstream=new Zipoutputstream (New FileOutputStream (ZipFile)); Zipoutputstream.putnextentry ( New ZipEntry (File.getname ())); Zipoutputstream.setcomment ("Hello");//Set comment int temp=0;while ((Temp=inputstream.read ( )! = ( -1)) {zipoutputstream.write (temp);} Inputstream.close (); Zipoutputstream.close ();}}
/* Compress multiple Files */public class ZipOutputStreamDemo1 {public static void main (string[] args) throws IOException {file File=new file ("D:" +file.separator+ "temp");//folder to be compressed file Zipfile=new file ("D:" +file.separator+ "Zipfile.zip"); InputStream Inputstream=null; Zipoutputstream zipoutputstream=new Zipoutputstream (New FileOutputStream (ZipFile)); Zipoutputstream.setcomment (" Hello ");//Set Comment if (File.isdirectory ()) {file[] files =file.listfiles (); for (int i=0;i<files.length;i++) { Inputstream=new FileInputStream (Files[i]) zipoutputstream.putnextentry (New ZipEntry (File.getname () + File.separator+files[i].getname ())); int Temp=0;while ((Temp=inputstream.read ())! = ( -1)) {Zipoutputstream.write ( temp);} Inputstream.close ();}} Zipoutputstream.close ();}}
Four: File decompression
/* Extract Individual files */
public class ZipOutputStreamDemo1 {public static void main (string[] args) throws IOException {file File=new file ("D:" +file . separator+ "Hello.zip"); File Outfile=new file ("D:" +file.separator+ "OutZipFile.txt"); ZipFile zipfile=new zipfile (file); ZipEntry entry=zipfile.getentry ("Hello.txt"); InputStream inputstream=zipfile.getinputstream (entry); OutputStream Outputstream=new FileOutputStream (outFile); int Temp=0;while ((Temp=inputstream.read ())! = ( -1)) {Outputstream.write ( temp);} Inputstream.close (); Outputstream.close ();}}
/* Unzip a compressed file with multiple files in the case */public class ZipOutputStreamDemo1 {public static void main (string[] args) throws IOException {file File=new File ("D:" +file.separator+ "Zipfile.zip"); File Outfile=null; ZipFile zipfile=new zipfile (file); Zipinputstream zipinputstream=new Zipinputstream (new FileInputStream (file)); ZipEntry entry=null;inputstream inputstream=null;outputstream Outputstream=null;while (entry= Zipinputstream.getnextentry ())!=null) {System.out.println ("Unzip file:" +entry.getname ()); Outfile=new file ("D:" + File.separator+entry.getname ()); if (!outfile.getparentfile (). exists ()) {Outfile.getparentfile (). Mkdirs (); if (!outfile.exists ()) {outfile.createnewfile ();} Inputstream=zipfile.getinputstream (entry); outputstream=new FileOutputStream (outFile); int Temp=0;while (temp= Inputstream.read ())! = ( -1)) {outputstream.write (temp);} Inputstream.close (); Outputstream.close ();}}}
V. Fallback flow
public class Pushbackinputstreamdemo {public static void main (string[] args) throws Exception {String str= "Hello,rollenho LT "; Pushbackinputstream Push=null; Bytearrayinputstream bat=null;bat=new Bytearrayinputstream (str.getbytes ());p ush=new PushbackInputStream (BAT); int Temp=0;while ((Temp=push.read ())! = ( -1)) {if (temp== ', ') {push.unread (temp); Temp=push.read (); System.out.print ("(Fallback:" + (char) temp+ ")"); else {System.out.print ((char) temp);}}}}
Java IO finishing (middle)