Java files and io--copy files and conversion streams

Source: Internet
Author: User
Tags file copy printable characters

The difference between a byte stream and a character stream

In all flow operations, bytes are always the most basic. Any byte-based operation is correct. Whether it is a text file or a binary file.

If you confirm that there are only printable characters in the stream, including English and various country text, including Chinese, then you can consider the character stream. Multibyte characters may consume multiple bytes because of different encodings. For example, GBK characters take up 2 bytes, while UTF-8 's characters take up 3 bytes. Therefore, the character stream is converted from 1 or more bytes to Unicode characters in Java, based on the specified encoding, and then manipulated. Character operations generally use Writer,reader, byte operations are generally inputstream,outputstream and various packaging classes, such as Bufferedinputstream and Bufferedoutputstream.

Summary: If you confirm that the stream you are working with is a printable character, then using a character stream will look simpler. If you do not confirm, then the use of byte stream is always not wrong.

Specify a file under a drive letter and copy the file to the specified directory.

 Public classCopyfiledemo {/**     * @paramargs *@throwsIOException*/     Public Static voidMain (string[] args) {//TODO auto-generated Method StubFile target=NewFile ("H:\\123.txt"); String dest= "G:\\test";        CopyFile (target,dest); System.out.println ("File copy succeeded"); }    /**     *      * @paramtarget source files, files to be copied *@paramdest Target file*/     Public Static voidcopyFile (File target,string dest) {String filename=Target.getname (); //System.out.println (filename);File destfile=NewFile (dest+filename); Try {            //input before output, code book cannot be changedInputStream in=NewFileInputStream (target); OutputStream out=NewFileOutputStream (destfile); byte[] bytes=New byte[1024]; intLen=-1;  while((Len=in.read (bytes))!=-1) {out.write (bytes,0, Len);            } out.close ();        In.close (); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }}

Byte character conversion stream , you can convert one byte stream to a character stream, or you can convert a stream of characters to a byte stream

OutputStreamWriter: The output character stream can be converted to the output form of a byte stream

InputStreamReader: Converts the input byte stream into a character stream input form

 Public classChangestreamdemo {/**     * @paramargs*/     Public Static voidMain (string[] args) {//TODO auto-generated Method StubString info=Reader (system.in);    SYSTEM.OUT.PRINTLN (info); }     Public StaticString Reader (InputStream in) {BufferedReader R=NewBufferedReader (NewInputStreamReader (in)); Try {            returnR.readline (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        return NULL; }}

Java files and io--copy files and conversion streams

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.