Java study notes -Java stream file operations

 day03

Input and output streams: Read Write
 
 Node Stream:
   a clear origin and destination
   tend to be byte-
 called low-flow node stream, byte stream
 
  processing flow:
  there is no clear source and destination
  are often low-level flow or other high-level streams operation, can not exist independently of
  the processing flow called advanced stream

* FileOutputStream fos = new FileOutputStream ( "a.txt", true); // create an object, create a txt file named "a" in the current directory

 // FileOutputStream default will overwrite the old content if we are not to be covered, add a true parameter when declaring an object

* String message = "date recasting of Broken Sword, the return of its potential.";

*  fos.write(message.getBytes());

* Fos.close () // remember to turn off the flow, otherwise it may cause serious consequences

Example: stream of bytes to copy a file

* FileInputStream fis1 = new FileInputStream ( "File Name File Type."); // read
* Long MSl = System.currentTimeMillis ();
* byte [] = new new byte of buf1 [File Size (B)];
* FIS1. the Read (buf1);
* = FIS2 new new FileOutputStream FileOutputStream ( "copy of the file name file type."); // write  
* fis2.write (buf1);
* Long ms2 = System.currentTimeMillis ();
* System.out .println (ms2-ms1); // with time (in milliseconds)
* fis2.close ();

 BufferedInputStream/BufferedOutputStream:

Advanced Stream buffer. Internal maintains a buffer zone, when we need to write data, the data will be stored in the buffer, when the buffer is full, one-time data will be written. Advanced Stream need to use low-flow as an auxiliary.

* FileOutputStream fos = new FileOutputStream ( "b.txt"); // lower stream (byte stream)

* BufferedOutputStream bos = new BufferedOutputStream (fos); // Advanced Stream (both are written stream)

* String message = "Demacian force";                
 
* byte [] = buf message.getBytes (); // string into bytes, receiving array
* bos.write (buf); // write                  

* bos.close (); // close the stream can be advanced

Copy the file stream Senior:

  FileInputStream fos = new FileInputStream ( "filename file type.");
  BufferedInputStream BOS = new new BufferedInputStream (fos);
  
  byte [] b = new new byte [10240]; // array can not call replication
  
  FileOutputStream fis = new FileOutputStream ( "copy the file type of the file.");
  BufferedOutputStream The new new BufferedOutputStream The BIS = (FIS);
  
  Long A1 = System.currentTimeMillis ();
  int I = -1;
  the while ((I = BOS. read (b)) = -1) {// (i = bos.read ()) = -1 this comment associated with the array!!
   bis.write (B);                    
   //bis.write(i);
  }
  BIS. Close ();
  bos.close ();
  Long A2 = System.currentTimeMillis ();
  System.out.println (BS - A); // time of calculation (in milliseconds)

Guess you like

Origin www.cnblogs.com/cgwjava/p/11390807.html