Java IO using demo

Source: Internet
Author: User

 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.