JAVA---IO stream

Source: Internet
Author: User
Tags readline

Share under Java, use IO to view files, write files, copy files.

1. Copy the picture without throwing an exception

 PackageCom.newdream.class4;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException; Public classtest_filecopy {//no exception thrown      Public Static voidMain (string[] args)throwsIOException {//Locate the target fileFile InFile =NewFile ("F:/3-150402141930.jpg"); File DestFile=NewFile ("E:/1.jpg"); //input and output channels for data creationFileInputStream FileInputStream =NewFileInputStream (InFile); FileOutputStream FileOutputStream=NewFileOutputStream (DestFile);//each time a new fileoutputstream is created, by default the pointer to FileOutputStream is the location of the file's start point.              Each time it is written out, the pointer will appear to move accordingly. //set up buffer data, read and write on the side            byte[] buf =New byte[1024]; intLength = 0 ;  while(length = Fileinputstream.read (BUF))!=-1) {Fileoutputstream.write (buf,0,length);//write lots of data, so you have to append it.             }              //Close the resources principle: first Open after the close, after open first off. Fileoutputstream.close ();          Fileinputstream.close (); }  }

2. View Files

1  PackageCom.newdream.class4;2 3 ImportJava.io.BufferedReader;4 ImportJava.io.FileReader;5 6  Public classCharioreader {7     //read the contents of the file8      Public Static voidMain (string[] args) {9         //reading character FilesTen         //1. Access OneFileReader fr=NULL;//2. Create a character stream input buffer ABufferedReader br=NULL; -         Try{         -              //Put the contents of the file in the buffer theHrsNewFileReader ("E:\newdream.txt"); -                       //3, read characters from Buffer -              //reading data from a buffer -Br=NewBufferedReader (FR);  +              //using while to read out data -               while(Br.read ()!=-1){ +String date =br.readline (); A System.out.println (date); at              }              -}Catch(Exception e) { - System.out.println (e.tostring ()); -}finally{ -             Try{ -Br.close ();//5. Close the buffer and close the channel in}Catch(Exception e) { - System.out.println (e.tostring ()); to             }             +         } -     } the  *}

3. Writing files

1  PackageCom.newdream.class4;2 3 ImportJava.io.BufferedReader;4 ImportJava.io.BufferedWriter;5 ImportJava.io.File;6 ImportJava.io.FileWriter;7 ImportJava.io.StringReader;8 9  Public classChariowriter {Ten      One     //writes text to the specified file A      -  -      Public StaticString data= "I am back again qaq! "; the      Public Static voidMain (string[] args) { -String Line;//used to control the end of the write file -BufferedWriter bw=NULL; -BufferedReader br=NULL; +         Try{ -FileWriter fr=NewFileWriter ("E:\newdream.txt"); +bw=NewBufferedWriter (FR); A             //reads a stream of characters and then streams the character to the read stream atBr=NewBufferedReader (NewStringReader (data)); -             //readLine () null means no content, end -              while((Line=br.readline ())! =NULL){ - Bw.write (line); -                 //Write a line, refresh it, write the next line - Bw.flush (); in             } -System.out.println ("Write succeeded! "); to}Catch(Exception e) { + System.out.println (e.tostring ()); -}finally{ the             Try{ *                 //Close the buffer: turn it off first, then turn it off first $ br.close ();Panax Notoginseng bw.close (); -}Catch(Exception e) { the System.out.println (e.tostring ()); +             } A         } the     } +}

4. Copy Movie

1  PackageCom.newdream.class4;2 3 ImportJava.io.File;4 ImportJava.io.FileInputStream;5 ImportJava.io.FileOutputStream;6 Importjava.io.IOException;7 8  Public classTest_filecopymovie {9      Public Static voidMain (string[] args)throwsIOException {Ten         //Locate the target file OneFile InFile =NewFile ("f:\\ thunder download \ \ With June love 100 times. mp4");  AFile DestFile =NewFile ("d:\\100 marriage. mp4");  -         //input and output channels for data creation -FileInputStream FileInputStream =NewFileInputStream (InFile);  theFileOutputStream FileOutputStream =NewFileOutputStream (DestFile); -            -         //each time a new fileoutputstream is created, by default the pointer to FileOutputStream is the location of the file's start point.  Each time it is written out, the pointer will appear to move accordingly.  -         //set up buffer data, read and write on the side +         byte[] buf =New byte[1024];  -         intLength = 0 ;  +          while(length = Fileinputstream.read (BUF))!=-1) { AFileoutputstream.write (buf,0,length); at         }   -         //Close the resources principle: first Open after the close, after open first off.  - Fileoutputstream.close ();  - Fileinputstream.close ();  -     } -  in}


--Same as copy file above
This article is for the author's opinion only, and the author is invited to give more advice. Welcome reprint, but without the consent of the author must retain this paragraph of the statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
Http://www.cnblogs.com/yushengaqingzhijiao/p/8343319.html

JAVA---IO stream

Related Article

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.