021.3 IO stream - byte stream - FileInputStream read byte

Contents: file reading method, reading method example, value of buf in read(buf) method, byte stream buffer object - improve reading speed///

File reading method: fis.read(), fis.read(buf), see the example for details

Example: file read - read the file and display it

public class FileInputStreamDemo
{
    public static void main(String[] args)
    {
        //In order to ensure that the file must exist before reading. Encapsulate the string path as File object
        File file = new File("F:"+File.separator+"eclipse_javaCode"+File.separator+"day22"
        +File.separator+"src"+File.separator+"demo"+File.separator+"GetMoney.java");
        if(!file.exists()){
            throw new RuntimeException("file does not exist!");
        }
        
        //Create file byte read stream object
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            //First read method, single read
//            int ch = 0;
//            while(ch!=-1){
//                ch = fis.read();
//                System.out.println(ch);  //The first method to read the file, successful return acsii Code, failure return-1
//            }
            //Second read method, batch read
            byte[] buf = new byte[1024];
            int len = 0;
            while((len = fis.read(buf)) != -1){
                System.out.println(new String(buf,0,len));
            }
        } catch (IOException e) {
        }
    }
}

 

#################################################################
Generally, byte buffers of corresponding size are not created directly
Buffer size is usually a multiple of bytes, a multiple of 1024

public static void main(String[] args) throws IOException
{
    File file = new File("F:\\eclipse_javaCode\\day21\\src\\day21\\shangxin.txt");
    System.out.println(file.length());    //Get File Bytes
    FileInputStream fis = new FileInputStream(file);
    System.out.println(fis.available());     //Gets the number of bytes of the file associated with it
    
    byte[] buf = new byte[fis.available()];   //Generally, it will not create buffer of corresponding size at once, such as HD movie.
    //The general creation is a multiple of 1024
    fis.read(buf);
    String s = new String(buf);
    System.out.println(s);
    
    fis.close();
}

 

Byte stream - copy text

public static void main(String[] args) throws IOException
{
    FileOutputStream fos = new FileOutputStream("xxx_copy.txt");
    FileInputStream fis = new FileInputStream("FileInputStreamDemo.java");
    
    byte[] buf = new byte[1024];
    int len = 0;
    while((len = fis.read(buf))!= -1){
        fos.write(buf,0,len);
        fos.flush();
    }
    
    fis.close();
    fos.close();
}

 

Buffer object for byte stream
#####Byte stream copy text - use buffer objects for efficiency

private static void copyByBuffer() throws IOException
{
    FileInputStream fis = new FileInputStream("aaaa.txt");
    BufferedInputStream bufis = new BufferedInputStream(fis);
    
    FileOutputStream fos = new FileOutputStream("aaaa_copy.txt");
    BufferedOutputStream bufos = new BufferedOutputStream(fos);
    
    byte[] buf = new byte[1024];
    
    int by = 0;
    while((by = bufis.read(buf))!=-1){
        bufos.write(buf,0,by);
        bufos.flush();
    }
    
    fos.close();
    fis.close();

}

Tags: Java

Posted on Mon, 30 Mar 2020 09:15:50 -0700 by quiksilver