BYTE stream
1 Packagecom.lovo.test;2 3 ImportJava.io.FileInputStream;4 Importjava.io.FileNotFoundException;5 ImportJava.io.FileOutputStream;6 Importjava.io.IOException;7 ImportJava.io.InputStream;8 ImportJava.io.OutputStream;9 Ten Public classTestbytestream { One A Public Static voidMain (string[] args) { - //TODO auto-generated Method Stub - //1, Byte stream the //Declaration Flow -InputStream in =NULL; -OutputStream out =NULL; - Try { + //Create a Stream -in =NewFileInputStream ("D:" + system.getproperty ("file.separator") + "Lileihanmeimei.mp3"); +out =NewFileOutputStream ("E:" + system.getproperty ("file.separator") + "J124.mp3"); A at //Operation Flow - //int b = 0; - //While ((b = In.read ())! =-1) { - //Out.write (b); - // } - in byte[] B =New byte[1024]; - intLength = 0; to while(length = In.read (b))! =-1){ +Out.write (b,0, length); -Out.flush ();//Scour the } * $ Panax Notoginseng}Catch(FileNotFoundException e) { - //TODO auto-generated Catch block the e.printstacktrace (); +}Catch(IOException e) { A //TODO auto-generated Catch block the e.printstacktrace (); +}finally{ - //Close the stream $ if(In! =NULL){ $ Try { - in.close (); -}Catch(IOException e) { the //TODO auto-generated Catch block - e.printstacktrace ();Wuyi } the } - if(Out! =NULL){ Wu Try { - out.close (); About}Catch(IOException e) { $ //TODO auto-generated Catch block - e.printstacktrace (); - } - } A } + the } - $}
Character stream: The same steps as above
1 Packagecom.lovo.test;2 3 Importjava.io.FileNotFoundException;4 ImportJava.io.FileReader;5 ImportJava.io.FileWriter;6 Importjava.io.IOException;7 ImportJava.io.Reader;8 ImportJava.io.Writer;9 Ten ImportJavax.swing.JOptionPane; One A Public classTestcharstream { - - Public Static voidMain (string[] args) { the //TODO auto-generated Method Stub - - //String msg = joptionpane.showinputdialog ("Please enter the next sentence of the day depending on the mountain": "); - // + //Writer w = null; - // + //try { A //w = new FileWriter ("Poem.txt"); at //char[] B = Msg.tochararray (); - //W.write (b); - //W.flush (); - //} catch (IOException e) { - // //TODO auto-generated Catch block - //e.printstacktrace (); in //} finally{ - //if (w! = null) { to //try { + //w.close (); - //} catch (IOException e) { the // //TODO auto-generated Catch block * //e.printstacktrace (); $ // }Panax Notoginseng // } - // } the + AReader r =NULL; the Try { +String result = ""; -R =NewFileReader ("Poem.txt"); $ Char[] C =New Char[10]; $ intLength = 0; - while(length = R.read (c))! =-1){ -String str =NewString (c, 0, length); theResult + =str; - }Wuyi System.out.println (result); the -}Catch(FileNotFoundException e) { Wu //TODO auto-generated Catch block - e.printstacktrace (); About}Catch(IOException e) { $ //TODO auto-generated Catch block - e.printstacktrace (); -}finally{ - if(r! =NULL){ A Try { + r.close (); the}Catch(IOException e) { - //TODO auto-generated Catch block $ e.printstacktrace (); the } the } the } the } -}
Java Data Flow