2019-05-30 Java learning diary day20 IO streams

I 流

concept:

  IO stream for processing data transfer between devices

  Java operations on the data stream by way of

  Java classes for IO operations in stream packet

  Press divided into two flow streams: input streams, output streams

  Stream by stream is divided into two types of operations:

    Byte stream: byte stream can operate any data, any data because the computer is stored in bytes

    Character stream: a stream of characters can only be operated pure character data, more convenient

IO streams common parent:

  Abstract parent class byte stream

    InputStream

    OutputStream

  Abstract method character classes:

    Reader

    Writer

IO program writing

  Before use, import classes package IO

  When in use, perform IO exception handling

  After use, the release of resources

 

InputStream

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class demo1_FileInputStraem {

    public static void main(String[] args) throws IOException {
        //demo1();
        FileInputStream fis =new FileInputStream("xxx.txt");
        int a;
        while ((a=fis.read()) !=-1) {
            System.out.println(a);        
        }
        fis.close();
    }

    public static void demo1() throws FileNotFoundException, IOException {
        FIS the FileInputStream = new new the FileInputStream ( "xxx.txt"); // Create a stream object 
        int X = fis.read ();   // read a byte from the hard disk 
        System.out.println (X);
         int Y = FIS .read ();
        System.out.println(y);
        int z=fis.read();
        System.out.println(z);
        int c=fis.read();
        System.out.println(c);
        fis.close ();    // off flow to release resources 
    }

}
Case

 

read () method reads a byte is why a return int, instead of a byte

  Because the input stream of bytes can operate any type of file, such as audio, pictures, files which are in the bottom and has a storage form, if each read returns byte, there may be encountered when reading the middle 11111111

  11111111 then this is the type of byte-1, our program is not encountered -1 will stop reading the following data is less than school, so accept the use of type int when read, if 11111111 will be supplemented in front on

  Gather 24 0 4 bytes, then the byte type int -1 becomes such a type 255 can ensure that the entire data read, and is of type int -1 accepted marker

 

OutputStream

When an object is created without this folder will be created to help me out

If you have this file will be empty the first file

Gaga true parameter chase

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class demo2_OutputStream {

    public static void main(String[] args) throws IOException {
         Fos a FileOutputStream = new new a FileOutputStream ( "yyy.txt" );
          // create output byte stream object, if not automatically create a 
         
         fos.write ( 97);   // lay write int is a number, but to a file It is a byte, automatically removing the first three 8 
         fos.write (98 );
         fos.write(99);
         System.out.println(fos);
         fos.close();
         
         

    }
}
Case

 

Copy pictures

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class demo3_Copy {

    public  static  void main (String [] args) throws IOException {
         // the demo1 (); 
        the FileInputStream FIL1 = new new the FileInputStream ( "Suddenly .mp3");   // Create input stream objects, the associated binary .jpg 
        
        a FileOutputStream FIL2 = new new FileOutputStream ( "copy.mp3"); // create an output stream object is associated copy.jpg
        
        int B;
         the while (! (B = fil1.read ()) = -. 1) {   // continue to read each byte 
            fil2.write (B);     // will write each byte
            
        }
        fil1.close (); // off the flow release resources 
        fil2.close ();
    }

    public static void demo1() throws FileNotFoundException, IOException {
        FIL1 the FileInputStream = new new the FileInputStream ( "1.JPG");   // Create input stream objects, the associated binary .jpg 
        
        a FileOutputStream FIL2 = new new a FileOutputStream ( "copy.jpg"); // create output stream object associated copy.jpg
        
        int B;
         the while (! (B = fil1.read ()) = -. 1) {   // continue to read each byte 
            fil2.write (B);     // will write each byte
            
        }
        fil1.close (); // off the flow release resources 
        fil2.close ();
    }

}
The first copy of the

 

Copy video illustration

Copy byte array -available () method

 int read (byte [] b): reads one byte array

 write(byte [ ] b):一次写出一个字节数组

 available()获取读的文件所有的字节个数

第二种拷贝
public
static void main(String[] args) throws IOException { //demo1(); //demo2(); //有可能会导致内存溢出 FileInputStream fil1=new FileInputStream("忽然之间.mp3"); FileOutputStream fil2=new FileOutputStream("copy.mp3"); //int len=fil1.available(); //System.out.println(len); byte [] arr=new byte[fil1.available()];//创建与文件一样大小的字节数组 fil1.read(arr); //将文件上的字节读取到内存中 fil2.write(arr);//将字节数组中的字节数据写到文件上 fil1.close(); fil2.close(); } }

 

定义小数组

  *write(byte [ ] b)

  *write(byte [ ] b, int off, int len )写出有效的字节个数

public class demo_arrayCopy {

    public static void main(String[] args) throws IOException {
        //demo1();
        FileInputStream fis =new FileInputStream("xxx.txt");
        FileOutputStream fos =new FileOutputStream("aaa.txt");
        byte[] arr= new byte[2];
        int len;
        while ((len=fis.read(arr)) !=-1) {
            fos.write(arr,0,len);
            
        }
        fis.close();
        fos.close();
    }
}
第三种拷贝

 

Guess you like

Origin www.cnblogs.com/JungTan0113/p/10953044.html