20-java学习-IO流-FileOutputStream、FileInputStream、BufferedOutputStream、BufferedInputStream&案例代码

20-java学习-IO流-FileOutputStream、FileInputStream、BufferedOutputStream、BufferedInputStream&案例代码

1.IO流概述及其分类

A:IO流概述
	IO流用来处理设备之间的数据传输
	Java对数据的操作是通过流的方式
	Java用于操作流的对象都在IO包中
B:IO流分类
	a:按照数据流向
		输入流	读入数据
		输出流	写出数据
	b:按照数据类型
		字节流 可以读写任何类型的文件 比如音频 视频  文本文件
		字符流 只能读写文本文件
		什么情况下使用哪种流呢?
		如果数据所在的文件通过windows自带的记事本打开并能读懂里面的内容,就用字符流。其他用字节流。
		如果你什么都不知道,就用字节流

2.IO流基类概述和FileOutputStream的构造方法

A:IO流基类概述
	a:字节流的抽象基类:
		InputStream ,OutputStream。
	b:字符流的抽象基类:
		Reader , Writer。
	注:由这四个类派生出来的子类名称都是以其父类名作为子类名的后缀。
	如:InputStream的子类FileInputStream。
	如:Reader的子类FileReader。
B:FileOutputStream的构造方法

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

- 需求:	往一个文本文件中写一串数据 Hello,IO
- 分析:
- a: 我们现在操作的是文本文件,所有按照我们的想法,我们优先现在字符流,但是字节流是优先于字符流. 所以先使用字节流
- b: 因为我们要写数据,所以我们应该使用字节流中输出流	OutputStream
- 我们发现OutputStream是一个抽象类,我们不能对其进行直接实例化,而我们需要使用子类对其进行实例化.那么选择哪个子类呢?
- 我们现在操作的是文件所以我们选择的是FileOutputStream

3.FileOutputStream写出数据

A: 构造方法
	FileOutputStream(File file)
	FileOutputStream(String name)
B:案例演示
	FileOutputStream写出数据
	
	注意事项:
		创建字节输出流对象了做了几件事情?
		a:调用系统资源创建a.txt文件
	  	b:创建了一个fos对象
	  	c:把fos对象指向这个文件
		为什么一定要close()?
		
		a: 通知系统释放关于管理a.txt文件的资源
		b: 让Io流对象变成垃圾,等待垃圾回收器对其回收

4.FileOutputStream的三个write()方法

A:FileOutputStream的三个write()方法
	public void write(int b):写一个字节  超过一个字节 砍掉前面的字节
	public void write(byte[] b):写一个字节数组
	public void write(byte[] b,int off,int len):写一个字节数组的一部分
B:案例演示:	FileOutputStream的三个write()方法

public class MyTest2 {
    public static void main(String[] args) throws IOException {
        //1.创建文件输出流对象,并且关联a.txt
        FileOutputStream out = new FileOutputStream("a.txt");
        //2.往文件中写入数据
        out.write(99); //一次写入一个字节
        out.write(100);
        out.write(101);
        out.write(102);
       // out.write();//一次写入一个字节数组
        String str="我爱你中国";
        byte[] bytes = str.getBytes();
        out.write(bytes);

      // out.write();//一次写入字节数组的一部分
        String str2="抗击疫情,万众一心";
        byte[] bytes1 = str2.getBytes();
        //从字节数组的0索引处开始,写入12个字节到文件中
        out.write(bytes1,0,12);
        //最后,流使用完毕后,一定要记得关闭,要释放资源。

        out.close();//关闭流。
    }
}

5.FileOutputStream写出数据实现换行和追加写入

A:案例演示:	FileOutputStream写出数据如何实现数据的换行

public class MyTest {
    public static void main(String[] args) throws IOException {
        //输出流所关联的文件,如果不存在,会自动创建
        FileOutputStream out = new FileOutputStream("b.txt");
        //往文件中写数据
        out.write("沅水通波接武岗,".getBytes());
        //写入换行符
      /*  windows下的换行符只用是 \r\n
            Linux		\n
             Mac		\r
             */
        out.write("\r\n".getBytes());
        out.write("送君不觉有离伤。".getBytes());
        //写入换行符
        out.write("\r\n".getBytes());
        out.write("青山一道同云雨,".getBytes());
        //写入换行符
        out.write("\r\n".getBytes());
        out.write("明月何曾是两乡。".getBytes());
        //写入换行符
        out.write("\r\n".getBytes());

        //释放资源
        out.close();

    }
}


 windows下的换行符只用是 \r\n
- Linux		\n
- Mac		\r
- eclipse中的记事本软件以及editplus这样的第三种软件都做了平台的兼容
B:案例演示:	FileOutputStream写出数据如何实现数据的追加写入

FileOutputStream fos = new FileOutputStream("fos.txt" , true) ;  // 完成追加写入
	
	// 写数据
	for(int x = 0 ; x < 10 ; x++){
		fos.write(("hello" + x).getBytes()) ;
		fos.write("\r\n".getBytes()) ;
	}
	
	// 释放资源
	fos.close(); 

64.FileOutputStream写出数据加入异常处理

A:案例演示:FileOutputStream写出数据加入异常处理
public class MyTest3 {
    public static void main(String[] args) {
        FileOutputStream out = null;
        //流的异常处理
        try {
            //模拟异常
           // System.out.println(1/0);
            out = new FileOutputStream("e.txt");
            out.write("你好哦".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(out!=null){
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

7.FileInputStream读取数据一次一个字节

A:案例演示:	int read():一次读取一个字节
                如果没有数据返回的就是-1
public class MyTest2 {
    public static void main(String[] args) throws IOException {
        FileInputStream in= new FileInputStream("e.txt");
        //读取文件中年的数据
        int by = in.read();//一次读取一个字节 ,返回的是你读取到的字节,如果读取不到返回-1
        System.out.println(by);
        by = in.read();//一次读取一个字节 ,返回的是你读取到的字节
        System.out.println(by);
        by = in.read();//一次读取一个字节 ,返回的是你读取到的字节
        System.out.println(by);
        by = in.read();//一次读取一个字节 ,返回的是你读取到的字节
        System.out.println(by);
        by = in.read();//一次读取一个字节 ,返回的是你读取到的字节
        System.out.println(by);
        by = in.read();//一次读取一个字节 ,返回的是你读取到的字节
        System.out.println(by);
        by = in.read();//一次读取一个字节 ,返回的是你读取到的字节
        //如果读取不到返回 -1 表示文件中的数据读取完了
        System.out.println(by);

        //释放资源
        in.close();
    }
}

8.字节流复制文本文件

A:案例演示:	字节流一次读写一个字节复制文本文件

 分析:
- a: 创建字节输入流对象和字节输出流对象
- b: 频繁的读写操作
- c: 释放资源
public class MyTest {
    public static void main(String[] args) throws IOException {
        //采用一次读取一个字节,写出一个字节来复制文本文件。
        //创建输入流
        FileInputStream in = new FileInputStream("MyTest.java");
        //创建输出流
        FileOutputStream out = new FileOutputStream("C:\\Users\\ShenMouMou\\Desktop\\MyTestCopy.java");
        //读取一个字节写出一个
        int by=0;//定义一个变量,用来记录每次读取到的字节
        while ((by=in.read())!=-1){
            out.write(by);//写一个字节
        }

        //释放资源
        in.close();
        out.close();
        System.out.println("复制完成");
    }
}

9.字节流复制MP3

A:案例演示:	字节流一次读写一个字节复制MP3

/**
	 * 创建字节输入流对象和字节输出流对象
	 */
	FileInputStream fis = new FileInputStream("D:\\a.mp3") ;
	FileOutputStream fos = new FileOutputStream("E:\\a.mp3") ;
	
	// 频繁的读写操作
	int by = 0 ;		// 用户记录读取到的字节
	while((by = fis.read()) != -1){
		fos.write(by) ;
	}
	
	// 释放资源
	fos.close() ;
	fis.close() ;
                
	一次读写一个字节效率低

10.FileInputStream读取数据一次一个字节数组

A:案例演示:	int read(byte[] b):一次读取一个字节数组
		返回的int类型的值表示的意思是读取到的字节的个数,如果没有数据了就返回-1

	byte[] bytes = new byte[1024] ;
	int len = 0 ;	// 作用: 记录读取到的有效的字节个数
	while((len = fis.read(bytes)) != -1){
		System.out.print(new String(bytes , 0 , len));
	}
	
	// 释放资源
	fis.close() ;

11.字节流复制文本文件2

A:案例演示:	字节流一次读写一个字节数组复制文本文件

// 创建字节输入流对象和字节输出流对象
	FileInputStream fis = new FileInputStream("D:\\xiaoshuo.txt") ;
	FileOutputStream fos = new FileOutputStream("E:\\xiaoshuo.txt");
	
	// 一次读取一个字节数组复制文件
	byte[] bytes = new byte[1024] ;
	int len = 0 ;// 作用: 记录读取到的有效的字节个数
	while((len = fis.read(bytes)) != -1){
		
		// 写数据
		fos.write(bytes , 0 , len) ;
	}
	
	// 释放资源
	fos.close() ;
	fis.close() ;

12.BufferedOutputStream写出数据

A:缓冲思想
	字节流一次读写一个数组的速度明显比一次读写一个字节的速度快很多,
	这是加入了数组这样的缓冲区效果,java本身在设计的时候,
	也考虑到了这样的设计思想,所以提供了字节缓冲区流
B:BufferedOutputStream的构造方法
	查看API
	BufferedOutputStream(OutputStream out)
    
    
C:案例演示
	BufferedOutputStream写出数据
public class MyTest {
    public static void main(String[] args) throws IOException {
        //copyFile1();
        //copyFile2();
       //copyFile3();
        copyFile4();
    }

 	private static void copyFile4() throws IOException {
        BufferedInputStream bfr = new BufferedInputStream(new FileInputStream("C:\\Users\\ShenMouMou\\Music\\我的图片和音乐\\音乐\\辛晓琪 - 领悟.mp3"));
       /* BufferedOutputStream(OutputStream out)
        创建一个新的缓冲输出流,以将数据写入指定的底层输出流。*/
        BufferedOutputStream bfw = new BufferedOutputStream(new FileOutputStream("C:\\Users\\ShenMouMou\\Desktop\\领悟.mp3"));


        int len = 0;
        //加入缓冲区
        byte[] bytes = new byte[1024 * 8];
        long start = System.currentTimeMillis();
        while ((len = bfr.read(bytes)) != -1) {
            bfw.write(bytes,0,len);
        }

        bfr.close();
        bfw.close();
        long end = System.currentTimeMillis();
        System.out.println("复制完成耗时" + (end - start) + "毫秒");
    }

    private static void copyFile3() throws IOException {
        FileInputStream in = new FileInputStream("C:\\Users\\ShenMouMou\\Music\\我的图片和音乐\\音乐\\辛晓琪 - 领悟.mp3");
        FileOutputStream out = new FileOutputStream("C:\\Users\\ShenMouMou\\Desktop\\领悟.mp3");
        int len = 0;
        //加入缓冲区。
        byte[] bytes = new byte[1024 * 8];

        long start = System.currentTimeMillis();
        while ((len = in.read(bytes)) != -1) {
            out.write(bytes,0,len);
        }
        in.close();
        out.close();
        long end = System.currentTimeMillis();
        System.out.println("复制完成耗时" + (end - start) + "毫秒");
    }

    private static void copyFile2() throws IOException {
        FileInputStream in = new FileInputStream("C:\\Users\\ShenMouMou\\Music\\我的图片和音乐\\音乐\\辛晓琪 - 领悟.mp3");
        FileOutputStream out = new FileOutputStream("C:\\Users\\ShenMouMou\\Desktop\\领悟.mp3");
        int by = 0;
        long start = System.currentTimeMillis();
        while ((by = in.read())!=-1) {
            out.write(by);
        }

        in.close();
        out.close();
        long end = System.currentTimeMillis();
        System.out.println("复制完成耗时" + (end - start) + "毫秒");

    }

    private static void copyFile1() throws IOException {
        //一对高效的字节输入输出流
        //  new BufferedInputStream()
        //  new BufferedOutputStream()
        /*BufferedInputStream(InputStream in)
        创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。*/
        BufferedInputStream bfr = new BufferedInputStream(new FileInputStream("C:\\Users\\ShenMouMou\\Music\\我的图片和音乐\\音乐\\辛晓琪 - 领悟.mp3"));
       /* BufferedOutputStream(OutputStream out)
        创建一个新的缓冲输出流,以将数据写入指定的底层输出流。*/
        BufferedOutputStream bfw = new BufferedOutputStream(new FileOutputStream("C:\\Users\\ShenMouMou\\Desktop\\领悟.mp3"));

        //一次读写一个字节来复制
        int by=0;
        long start = System.currentTimeMillis();
        while ((by = bfr.read())!=-1){
            bfw.write(by);
        }

        bfr.close();
        bfw.close();
        long end = System.currentTimeMillis();
        System.out.println("复制完成耗时" + (end - start) + "毫秒");
    }
}

13.BufferedInputStream读取数据

A:BufferedInputStream的构造方法
	查看API
	BufferedInputStream(InputStream in)

14.字节流四种方式复制MP3并测试效率

	基本字节流一次读写一个字节
	基本字节流一次读写一个字节数组
	高效字节流一次读写一个字节
	高效字节流一次读写一个字节数组
发布了49 篇原创文章 · 获赞 9 · 访问量 1447

猜你喜欢

转载自blog.csdn.net/weixin_42401546/article/details/104793839