java IO byte stream flow ---

1. IO streams Summary and Classification

. 1) Overview IO stream
IO streams to process the data transmission between the devices
Java operations on the data stream by way of
Java objects for the operation of the flow in the IO package
2) IO traffic classification
a: The data flow we stand in the memory flow stream perspective
the input data stream into an
output stream of data is written

b: the data type of
the byte stream read and write any type of file such as a text file an audio video
stream of characters can read and write text files

2. IO stream the base class Overview

a: abstract base class byte stream:
the InputStream (parent remaining byte input), OutputStream (left byte output parent).
b: an abstract base class for character streams:
Reader, Writer.
Note: these four sub-classes derived from the class name is the class name as a suffix to its parent subclass name.
Such as: InputStream subclass FileInputStream.
Such as: the subclass FileReader Reader.

Category 3. Io flows

(1): it is divided according to the flow of
the input stream
output stream
(2): the data are divided according to the type of operation
byte stream
input stream of bytes read InputStream
byte written to the output stream OutputStream
character stream
Character Reader reads the input stream
of characters to write to the output stream Writer

4. FileOutputStream

1) FileOutputStream constructor

 /*  FileOutputStream(File file)
        创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
        FileOutputStream(String name)
        创建一个向具有指定名称的文件中写入数据的输出文件流。*/
 File file = new File("a.txt");
        FileOutputStream out = new FileOutputStream(file);
        FileOutputStream out1 = new FileOutputStream("e.txt");

2) FileOutputStream write data
3) FileOutputStream three write () method

public class test1 {
    public static void main(String[] args) throws IOException {
        FileOutputStream out = new FileOutputStream("a.txt");
        //一次写一个字节
        out.write(97);
        out.write(98);
        out.write(99);
        //写一个字节数组
        byte[] bytes={100, 101, 102, 103, 104, 105};
        out.write(bytes);
        //写一段字节数组
        out.write(bytes,0,3);

        String str="好好学习,天天向上";
        byte[] bytes1 = str.getBytes();
        out.write(bytes1,0,3);
        out.write(bytes1);
    }
}
/*  创建字节输出流对象了做了几件事情 ?
        a : 调用系统资源创建a.txt文件
        b:创建了一个out对象
        c:把out对象指向这个文件
        为什么一定要close() ?
        a : 通知系统释放关于管理a.txt文件的资源
        b:让Io流对象变成垃圾, 等待垃圾回收器对其回收*/

4) FileOutputStream write data additional writing implement and linefeed

 /*  windows下的换行符只用是 \r\n
        Linux		\n
        Mac		\r*/
        //true 代表追加写入,不会重新覆盖文件中之前的数据
        public class test2 {
    public static void main(String[] args) throws IOException {
        FileOutputStream out = new FileOutputStream(new File("a.txt"), true);
        out.write("大漠孤烟直".getBytes());
        out.write("\r\n".getBytes());
        out.write("长河落日圆".getBytes());
        out.write("\r\n".getBytes());
        out.close();
    }
}

5) FileOutputStream to write data into the exception handling

public class MyTest3 {
    public static void main(String[] args) {
        //流的移除处理
        FileOutputStream out=null;
        try {
            out = new FileOutputStream("a.txt");
            out.write(200);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(out!=null){
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

5. FileInputStream

1) Read one byte of data
2) to read the data one byte array

public class test4 {
    public static void main(String[] args) throws IOException {
        //输入流:读取文件中的数据
        FileInputStream in = new FileInputStream("a.txt");
        //一.一次读取一个字节   如果没有数据返回的就是-1
        int b = in.read();
        System.out.println(b);
        //二.一次读取一个字节数组
        /*1.创建一个空的字节数组,充当缓冲区
        * 2.返回的是读取到的有效字节的个数*/
        byte[] bytes = new byte[1024];
        int len = in.read(bytes);
        for (byte aByte : bytes) {
            System.out.println(aByte);
        }
        System.out.println(len);
        String s = new String(bytes, 0, len);
        System.out.println(s);
        //三.一次读取一部分字节
        int len1 = in.read(bytes, 0, 20);
        in.close();
    }
}

3) copy the text file byte stream

public class test5 {                                                                                            
    public static void main(String[] args) throws IOException {                                                 
        FileInputStream in = new FileInputStream("C:\\Users\\10237\\Desktop\\123.txt");//读取                     
        FileOutputStream out = new FileOutputStream("C:\\Users\\10237\\Desktop\\121.txt");//写到这里                
        int len=0;                                                                                              
        while ((len=in.read())!=-1){                                                                            
            out.write(len);                                                                                     
            out.flush();                                                                                        
        }                                                                                                       
       in.close();                                                                                              
        out.close();                                                                                            
    }                                                                                                           
                                                                                                                
}                                                                                                               

4) Copy byte stream MP3

public class test6 {
    public static void main(String[] args) throws IOException {
        //复制音乐
        File file = new File("E:\\IdeaProjects\\MyTest\\.idea\\新上海滩 - 上海滩.mp3");
        FileInputStream in = new FileInputStream(file);
        FileOutputStream out = new FileOutputStream("C:\\Users\\10237\\Desktop"+file.getName());
        int len=0;
        long start = System.currentTimeMillis();
        while ((len=in.read())!=-1){
            out.write(len);
            out.flush();
        }
        long end = System.currentTimeMillis();
        in.close();
        out.close();
        System.out.println(end-start+"毫秒");
    }

}

6. BufferedOutputStream write data

public class MyTest6 {
    public static void main(String[] args) throws IOException {
        /*高效的输入输出流
        BufferedInputStream
        BufferedOutputStream*/
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\IdeaProjects\\MyTest\\.idea\\新上海滩 - 上海滩.mp3"), 1024);
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\Users\\10237\\Desktop\\a.mp3"));
        int len=0;
        long start = System.currentTimeMillis();
        byte[] bytes = new byte[1024*8];
        while ((len=bis.read())!=-1){
            bos.write(bytes,0,len);
            bos.flush();
        }
        long end = System.currentTimeMillis();
        bis.close();
        bos.close();
        System.out.println("复制完成");
    }
}
Published 39 original articles · won praise 1 · views 563

Guess you like

Origin blog.csdn.net/love_to_share/article/details/103118341