java IO流---字节流

1. IO流概述及其分类

1)IO流概述
IO流用来处理设备之间的数据传输
Java对数据的操作是通过流的方式
Java用于操作流的对象都在IO包中
2)IO流分类
a:按照数据流向 我们是站在内存的角度来看流的流向
输入流 读入数据
输出流 写出数据

b:按照数据类型
字节流 可以读写任何类型的文件 比如音频 视频 文本文件
字符流 只能读写文本文件

2. IO流基类概述

a:字节流的抽象基类:
InputStream(字节输入留的父类) ,OutputStream(字节输出留的父类)。
b:字符流的抽象基类:
Reader , Writer。
注:由这四个类派生出来的子类名称都是以其父类名作为子类名的后缀。
如:InputStream的子类FileInputStream。
如:Reader的子类FileReader。

3. Io流的分类

(1): 按照流向进行划分
输入流
输出流
(2): 按照操作的数据类型进行划分
字节流
字节输入流 InputStream 读
字节输出流 OutputStream 写
字符流
字符输入流 Reader 读
字符输出流 Writer 写

4. FileOutputStream

1)FileOutputStream的构造方法

 /*  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写出数据
3)FileOutputStream的三个write()方法

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写出数据实现换行和追加写入

 /*  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写出数据加入异常处理

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)读取数据一次一个字节
2)读取数据一次一个字节数组

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)字节流复制文本文件

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)字节流复制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写出数据

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("复制完成");
    }
}
发布了39 篇原创文章 · 获赞 1 · 访问量 563

猜你喜欢

转载自blog.csdn.net/love_to_share/article/details/103118341
今日推荐