Public classReadbytestream { Public Static voidMain (string[] args) {//Stream byte Flow//Reader,writer Character Stream//input reads external things,//Output Outputs Try{//read byte stream,mytext.txt is exitsFileInputStream inputstream=NewFileInputStream ("Mytext.txt"); byte[]in=New byte[60]; Inputstream.read (in);//read bytes into the array//string Str=new string (in); default is GBKString str=NewString (In, "UTF-8");//it's garbled .System.out.println (str); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
Public classWritebytestream { Public Static voidMain (string[] args) {Try{FileOutputStream FileOutputStream=NewFileOutputStream ("Mytext1.txt"); String Str= "Hello, Jack ..."; byte[]out=str.getbytes (); Try{fileoutputstream.write (out); } Catch(IOException e) {e.printstacktrace (); } } Catch(FileNotFoundException e) {e.printstacktrace (); } }}
Public classCopybybytestream { Public Static voidMain (string[] args) {Try{FileInputStream InputStream=NewFileInputStream ("Water.jpg"); FileOutputStream OutputStream=NewFileOutputStream ("Newwater.jpg"); byte[]in=New byte[50];//now just read the 50 bytes and can't read the full picture//Inputstream.read (in); //cannot read the full picture//Outputstream.write (in); //Inputstream.read (in); returns the int type if 1 means no data is returned while(Inputstream.read (in)!=-1)//indicates that there is data to write to{outputstream.write (in); } inputstream.close (); Outputstream.close (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } }}
Public classFilereaderandfilewriter { Public Static voidMain (string[] args) {Try { //file Write ReadFileReader filereader=NewFileReader ("Java.txt"); BufferedReader BufferedReader=NewBufferedReader (FileReader); //character stream wrapped as buffer streamFileWriter filewriter=NewFileWriter ("Java_new_1"); BufferedWriter BufferedWriter=NewBufferedWriter (FileWriter); String Len; while((Len=bufferedreader.readline ())! =NULL) {bufferedwriter.write (len+ "\ n"); } bufferedwriter.flush ();//Note that writer has to flush ()Bufferedwriter.close (); Bufferedreader.close (); Filereader.close (); Filewriter.close (); System.out.println ("Done"); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } }}
Public classBufferreader { Public Static voidMain (string[] args) {Try{FileInputStream FileInputStream=NewFileInputStream ("Java.txt"); InputStreamReader ISR=NewInputStreamReader (FileInputStream);//byte flow into a character streamBufferedReader Reader=NewBufferedReader (ISR); FileOutputStream FileOutputStream=NewFileOutputStream ("Java_new_buffered.txt"); OutputStreamWriter OSW=NewOutputStreamWriter (FileOutputStream); BufferedWriter writer=NewBufferedWriter (OSW); String Len; while((Len=reader.readline ())! =NULL) {writer.write (len); } writer.flush (); Writer.close (); Osw.close (); Fileinputstream.close (); Fileoutputstream.close (); System.out.println ("Done"); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
Public classReadbufferbystream { Public Static voidMain (string[] args) {Try{FileInputStream FileInputStream=NewFileInputStream ("vidio.wmv"); Bufferedinputstream Bufferedinputstream=NewBufferedinputstream (FileInputStream); FileOutputStream FileOutputStream=NewFileOutputStream ("e://textvidio.wmv"); Bufferedoutputstream Bufferedoutputstream=NewBufferedoutputstream (FileOutputStream); byte[]by=New byte[100]; while(Bufferedinputstream.read (by)!=-1) {bufferedoutputstream.write (by); } bufferedinputstream.close (); Bufferedoutputstream.close (); Fileinputstream.close (); Fileoutputstream.close (); System.out.println ("Vidio Create Sucess ..."); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } }}
Java IO using demo