/* Byte stream: inputstreamoutputstream*/import java.io.*;class filestream{public static void Main (string[] args) throws Ioexception{outputfile ();//inputfile_1 (); inputfile_2 ();} /* */public static void OutputFile () throws IOException//write {FileOutputStream fos=new fileoutputstream ("d:\\") without using arrays for buffering Myfile\\mycode\\4.txt "); Fos.write (" Abndks hello ". getBytes ()); Fos.close ();} public static void Inputfile () throws ioexception//Read {FileInputStream fis=new fileinputstream ("D:\\myfile\\mycode\\4. TXT ") int ch=0;while ((Ch=fis.read ())!=-1) {System.out.println ((char) ch);} Fis.close ();} /* Read */public static void Inputfile_1 () throws ioexception//Read {FileInputStream fis=new fileinputstream ("D:\") using the array buffer method \myfile\\mycode\\4.txt "), int num=0;byte[] b=new byte[1024];while ((Num=fis.read (b))!=-1) {System.out.println (new String (B,0,num));} Fis.close ();} /* Gets the number of bytes using the available specific to the byte stream object. */public static void Inputfile_2 () throws ioexception//Read {FileInputStream fis=new fileinputstream ("d:\\myfile\\mycode\ \4.txt "); int num=fis.available (); byte[] B=new byte[num];int X=fis.read (b); System.out.println ("x:" +x); SYSTEM.OUT.PRINTLN ("num:" +num); System.out.println (New String (b)); Fis.close ();}}
To copy a picture:
/* Copy Picture: Idea: 1, read stream object and Picture Association in bytes. 2. Create a picture file with bytes written to the stream object to store the captured picture data. 3, through the loop read and write, the completion of data storage. 4, close the resource. */import java.io.*;class copypic {public static void main (string[] args) {FileInputStream fis=null; FileOutputStream fos=null;try{system.out.println ("1111"); Fis=new fileinputstream ("d:\\20.jpg"); System.out.println ("2222"); Fos=new fileoutputstream ("c:\\test\\new.jpg"); System.out.println ("3333"); byte[] B=new byte[1024];int len=0;while ((Len=fis.read (b))!=-1) {fos.write (B,0,len);}} catch (IOException e) {throw new RuntimeException ("Failed to copy File");} Finally{try{if (Fis!=null) Fis.close ();} catch (IOException e) {throw new RuntimeException ("Read shutdown failed");} Try{if (Fos!=null) Fos.close ();} catch (IOException e) {throw new RuntimeException ("Write close Failed");}}}
Copy the MP3 file, using the byte stream buffer
/* Use buffer */import by copying MP3 media files Java.io.*;class CopyMp3 {public static void main (string[] args) {long start= System.currenttimemillis (); Copy_1 (); Long End=system.currenttimemillis (); System.out.println ((End-start) + "MS");} public static void Copy_1 () {FileInputStream fis=null; FileOutputStream Fos=null; Bufferedinputstream Bis=null; Bufferedoutputstream bos=null;try{fis=new FileInputStream ("D:\\cloudmusic\\lonely.mp3"); Fos=new FileOutputStream ( "D:\\myfile\\mycode\\new.mp3"); bis=new bufferedinputstream (FIS); bos=new bufferedoutputstream (FOS); int By=0;while ( (By=bis.read ())!=-1) {bos.write (by);}} catch (IOException e) {throw new RuntimeException ("Copy Failed");} Finally{try{if (Bis!=null) Bis.close ();} catch (IOException e) {throw new RuntimeException ("read stream shutdown failed");} Try{if (Bos!=null) Bos.close ();} catch (IOException e) {throw new RuntimeException ("Write stream shutdown Failed");}}}
Java bytes input and output stream