Javase Getting Started learning 44: I/O stream of File Transfer Basics (iii)

Source: Internet
Author: User
Tags file copy

use of three-byte throttling 6) FileOutputStream---> Implements a method for writing byte data to a file FileOutputStream inherits the OutputStream abstract class.

Methods in the FileOutputStream class:


Example code 1:

<span style= "FONT-SIZE:18PX;" >import java.io.*;p ublic class fileoutdemo1{public static void Main (string[] args) throws IOException {//If the file does not exist, It is created directly, if present, after deletion creates fileoutputstream out = new FileOutputStream ("E:\\java\\javase\\io\\out.dat"); Out.write (' A ');// Write the ' A ' low eight-bit out.write (' B '),//write the ' B ' low eight-bit int a = 10;//write can only write eight bits, then write an int need to write 4 times, 8 bits per out.write (a >>>); O Ut.write (a >>>); Out.write (a >>> 8); Out.write (a); byte[] GBK = "China". GetBytes ("GBK"); Out.write (GBK) ; Out.close (); Ioutil.printhex ("E:\\java\\javase\\io\\out.dat");}} </span>

Operation Result:


file copy

Example code 2:

<span style= "FONT-SIZE:18PX;"  >import java.io.*;p ublic class ioutil{/* * file copy, byte bulk read */public static void CopyFile (file Srcfile,file destfile) throws Ioexception{if (!srcfile.exists ()) {throw new IllegalArgumentException ("File:" +srcfile+ "does not Exist");} if (!srcfile.isfile ()) {throw new IllegalArgumentException (srcfile+ "is not a file");} FileInputStream in = new FileInputStream (srcfile); FileOutputStream out = new FileOutputStream (destfile); byte[] buf = new Byte[8*1024];int b;        while ((b = In.read (buf,0,buf.length))!=-1) {          out.write (buf,0,b);          Out.flush ();//preferably plus       }       in.close ();       Out.close ();} public static void Main (string[] args) {try{ioutil.copyfile (New file ("E:\\java\\javase\\io\\test.txt"), New file ("e:\\ Java\\javase\\io\\test1.txt "));} catch (IOException e) {e.printstacktrace ();}}} </span>

Operation Result:


7) DataOutputStream class/datainputstream class

For the extension of the "stream" function, you can read int, long, character and other types of data more. This class embodies the decorative patterns in the design pattern.

Common methods in the DataInputStream class:



Common methods in the DataOutputStream class:



Example 1:

Import java.io.*;p ublic class disdemo{public static void Main (string[] args) throws ioexception{    String file = "E:\\ja Va\\javase\\io\\demo\\dos.dat ";    Ioutil.printhex (file);    DataInputStream dis = new DataInputStream (new FileInputStream (file));    int i = Dis.readint ();    System.out.println (i);    i = Dis.readint ();    System.out.println (i);    Long L = Dis.readlong ();    System.out.println (l);    Double d = dis.readdouble ();    System.out.println (d);    String s = Dis.readutf ();    System.out.println (s);            Dis.close ();}}

Operation Result:


Example 2:

Import java.io.*;p ublic class dosdemo{public        static void Main (string[] args) throws IOException {            String file = " E:\\java\\javase\\io\\demo\\dos.dat ";            DataOutputStream dos = new DataOutputStream (new FileOutputStream (file));            Dos.writeint (ten);            Dos.writeint ( -10);            Dos.writelong (10l);            Dos.writedouble (10.5);            Write            Dos.writeutf ("China") with utf-8 code;            Write            dos.writechars ("China") with UTF-16BE code;            Dos.close ();            Ioutil.printhex (file);}        }

Operation Result:


8) Bufferedinputstream class/bufferedoutputstream class

These two stream classes provide the IO with buffer operations, generally open the file for writing, or read operations, will be buffered, this flow pattern

High IO performance putting input into a file from the application is equivalent to pouring a cylinder of water into another cylinder.

Methods of the Bufferedinputstream class:


Methods of the Bufferedoutputstream class:


The FileOutputStream--->write () method is equivalent to "shifting" the water from one drop to the past.

The Dataoutputstream-->writexxx () method will be convenient, equivalent to a scoop of water "transfer" the past.

Bufferedoutputstream--->write method is more convenient, the equivalent of a float a scoop first into the bucket, and then poured from the barrel into another cylinder, performance

It's high.

Instance code:

Import java.io.*;p ublic class ioutil{/* * file copy, byte bulk read */public static void CopyFile (file Srcfile,file destfile) throws IOE Xception{if (!srcfile.exists ()) {throw new IllegalArgumentException ("File:" +srcfile+ "does not Exist");} if (!srcfile.isfile ()) {throw new IllegalArgumentException (srcfile+ "is not a file");} FileInputStream in = new FileInputStream (srcfile);        FileOutputStream out = new FileOutputStream (destfile); byte[] buf = new Byte[8*1024];int b;          while ((b = In.read (buf,0,buf.length))!=-1) {out.write (buf,0,b);        Out.flush ();//Preferably Plus} in.close (); Out.close ();} /* * Make a copy of the file, using buffered byte stream */public static void Copyfilebybuffer (file Srcfile,file destfile) throws Ioexception{if (! Srcfile.exists ()) {throw new IllegalArgumentException ("File:" +srcfile+ "does not Exist");} if (!srcfile.isfile ()) {throw new IllegalArgumentException (srcfile+ "is not a file");} Bufferedinputstream bis = new Bufferedinputstream (new FileInputStream (srcfile)); Bufferedoutputstream BOS = new Bufferedoutputstream (New FileOutputStream (destFile), int C, while ((c = bis.read ())!=-1) {bos.write (c); Bos.flush ();//Flush buffer}bis.close (); Bos.close ();} /* Single byte, no buffer for file copy */public static void Copyfilebybyte (file Srcfile,file destfile) throws Ioexception{if (! Srcfile.exists ()) {throw new IllegalArgumentException ("File:" +srcfile+ "does not Exist");} if (!srcfile.isfile ()) {throw new IllegalArgumentException (srcfile+ "is not a file");} FileInputStream in = new FileInputStream (srcfile); FileOutputStream out = new FileOutputStream (destfile); int C;while ((c = in.read ())!=-1) {out.write (c); Out.flush ();} In.close (); Out.close ();} public static void Main (string[] args) {try{//Three method reads the write time differently Ioutil.copyfilebybuffer ("The New File (" e:\\java\\javase\\io\\ Fileutils.java "), NewFile (" E:\\java\\javase\\io\\aa.txt ")); Ioutil.copyfilebybyte (NE W file ("E:\\java\\javase\\io\\fileutils.java"), New file ("E:\\java\\javase\\io\\bb.txt")); Ioutil.copyfile (new file ("E:\\java\\javase\\io\\fileutils.java"), New File ("E:\\java\\javase\\io\\cc.txt")); catch (IOException E){E.printstacktrace ();}}} 

Operation Result:


Test Copy time:

Import java.io.*;p ublic class ioutil{/* * file copy, byte bulk read */public static void CopyFile (file Srcfile,file destfile) throws IOE Xception{if (!srcfile.exists ()) {throw new IllegalArgumentException ("File:" +srcfile+ "does not Exist");} if (!srcfile.isfile ()) {throw new IllegalArgumentException (srcfile+ "is not a file");} FileInputStream in = new FileInputStream (srcfile);        FileOutputStream out = new FileOutputStream (destfile); byte[] buf = new Byte[8*1024];int b;          while ((b = In.read (buf,0,buf.length))!=-1) {out.write (buf,0,b);        Out.flush ();//Preferably Plus} in.close (); Out.close ();} /* * Make a copy of the file, using buffered byte stream */public static void Copyfilebybuffer (file Srcfile,file destfile) throws Ioexception{if (! Srcfile.exists ()) {throw new IllegalArgumentException ("File:" +srcfile+ "does not Exist");} if (!srcfile.isfile ()) {throw new IllegalArgumentException (srcfile+ "is not a file");} Bufferedinputstream bis = new Bufferedinputstream (new FileInputStream (srcfile)); Bufferedoutputstream BOS = new Bufferedoutputstream (New FileOutputStream (destFile), int C, while ((c = bis.read ())!=-1) {bos.write (c); Bos.flush ();//Flush buffer}bis.close (); Bos.close ();} /* Single byte, no buffer for file copy */public static void Copyfilebybyte (file Srcfile,file destfile) throws Ioexception{if (! Srcfile.exists ()) {throw new IllegalArgumentException ("File:" +srcfile+ "does not Exist");} if (!srcfile.isfile ()) {throw new IllegalArgumentException (srcfile+ "is not a file");} FileInputStream in = new FileInputStream (srcfile); FileOutputStream out = new FileOutputStream (destfile), int C, while ((c = in.read ())!=-1) {out.write (c); Out.flush ();} In.close (); Out.close ();} public static void Main (string[] args) {Try{long start=system.currenttimemillis ();//ioutil.copyfilebybyte (New File ("E : \\Java\\JavaSE\\IO\\1.mp3 "), New File (//" E:\\java\\javase\\io\\2.mp3 "));//test for 95042 MS, slowest// Ioutil.copyfilebybuffer (New file ("E:\\java\\javase\\io\\1.mp3"), new file (//"E:\\java\\javase\\io\\3.mp3");// Tested for 4799 milliseconds, medium speed Ioutil.copyfile (new file ("E:\\java\\javase\\io\\1.mp3"), New file ("E:\\java\\javase\\io\\4.mp3")) ;//test for 60 milliseconds, the fastest LOng End=start=system.currenttimemillis (); System.out.println (End-start);} catch (IOException e) {e.printstacktrace ();}}}

Operation Result:



Javase Getting Started learning 44: I/O stream of File Transfer Basics (iii)

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.