黑马程序员——JAVA基础-IO流(一)

----------android培训java培训java学习型技术博客、期待与您交流!------------


在此,分享一下自己学习JAVA的学习心得。有不对的地方请帮忙改正,也希望对想java的同学有帮助!


JAVA基础

        —IO流



  IO流体系图:



IO流概述:


1)IO流用来处理设备之间的数据传输
        上传文件和下载文件


   2)Java对数据的操作是通过流的方式


   3)Java用于操作流的对象都在IO包中


IO流分类:


1)按照数据流向


       输入流  读入数据
       输出流  写出数据


   2)按照数据类型


       字节流   InputStream ,OutputStream。


       字符流   Reader , Writer。


注:由这四个类派生出来的子类名称都是以其父类名作为子类名的后缀。


什么情况下使用哪种流:


   1.字节流:读写二进制文件:图片、视频文件等;也可以读写文本文件;
   2.字符流:读写文本文件的;



字节流:


1)、FileOutputStream和FileInputStream


FileOutputStream的构造函数:

1.public FileOutputStream(String name):创建一个向具有指定名称的文件中写入数据的输出文件流。
   2.public FileOutputStream(File file):创建一个向指定 File 对象表示的文件中写入数据的文件输出流。

3.public FileOutputStream(File file,boolean append):创建一个向指定 File 对象表示的文件中写入数据的文件输出流。append - 如果为 true,则将字节写入文件末尾处,而不是写入文件开始处 
   4.public FileOutputStream(String name,boolean append):


FileOutputStream的写入数据的方法:


   1.write(byte[] b):将 b.length 个字节从指定 byte 数组写入此文件输出流中。
   2.public void write(byte[] b,int off, int len):指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。 
   3.public void write(int b):将指定字节写入此文件输出流。


FileInputStream的构造方法:

注意:建立输入流,文件必须存在,否则FileNotFoundException异常;
    1.public FileInputStream(String name) throws FileNotFoundException:通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定。
    2.public FileInputStream(File file) throws FileNotFoundException:通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定。
    
    FileInputStream读取数据:
    1.int read():从此输入流中读取一个数据字节。返回:下一个数据字节;如果已到达文件末尾,则返回 -1。
  

Java练习代码:

练习:一次读取一个字节,一次写入一个字节

public class Demo {
	public static void main(String[] args) {
		
		try {
			//输入流:文件必须存在;
			FileInputStream in = new FileInputStream("a.txt");
			//准备一个输出流:文件可以不存在
			FileOutputStream out = new FileOutputStream("b.txt");
			//开始复制:
			//输入流:一次读取一个字节
			//输出流:一次写入一个字节
			int n = 0;
			while((n = in.read())!= -1){
				out.write(n);
			}
			//释放资源
			in.close();
			out.close();
			System.out.println("复制完毕!");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

   练习2:一次读取并写入一个字节数组

public class Demo {
	public static void main(String[] args) {
		//1.输入流
		try {
			FileInputStream in = new FileInputStream("a.txt");
			//2.输出流
			FileOutputStream out = new FileOutputStream("b.txt");
			//3.复制:一次读取一个字节数组,一次写入一个字节数组
			byte[] byteArray = new byte[1024];//创建一个缓冲区
			int len = 0;
			while((len = in.read(byteArray)) != -1){
				//写入
				out.write(byteArray, 0, len);
			}
			//4.释放资源;
			in.close();
			out.close();
			System.out.println("复制完毕");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

      字节缓冲流:


  1)字节缓冲输出流
BufferedOutputStream

     构造方法:
         1.public BufferedOutputStream(OutputStream out):使用一个OutputStream来构造;
          实际上还是由FileOutputStream提供输出流,BufferedOutputStream提供了缓冲区;


      注:1)带缓冲区的输出流,write()之后,一定要flush()或close(),才能将缓冲区的内如写入文件;

          2)colse()方法,先执行自己的flush()方法,再执行OutputStream的close()方法。


     2)字节缓冲输入流
BufferedInputStream

      构造方法:
         1.public BufferedInputStream(InputStream in):对于BufferedInputStream和BufferedOutputStream的          使用同FileInputStream和FileOutputStream的使用是一样的。没有新增其它的方法;但是:效率要高很            多;所以,对于字节的输入、输出流,推荐使用:BufferedInputStream和BufferedOutputStream

Java练习代码:

练习:四种IO流的复制方式及效率比较

<span style="font-size:18px;">public class Demo {
	public static void main(String[] args) throws IOException {
		long start = System.currentTimeMillis();

		// method1();//60376毫秒//</span><span style="font-size:18px; font-family: 宋体;">FileInputStream和FileOutputStream:</span><span style="font-size:18px;">一次读取一个字节;method1()
		// method2();//122毫秒//</span><span style="font-size:18px; font-family: 宋体;">FileInputStream和FileOutputStream:</span><span style="font-size:18px;">一次读取一个字节数组;method2()
		// method3();//955毫秒//</span><span style="font-size:18px; font-family: 宋体;">BufferedInputStream和BufferedOutputStream:</span><span style="font-size:18px;">一次读取一个字节:method3()
		method4();// 90毫秒//</span><span style="font-size:18px; font-family: 宋体;">BufferedInputStream和BufferedOutputStream:</span><span style="font-size:18px;">一次读取一个字节数组:method4();
		long end = System.currentTimeMillis();
		System.out.println("此次复制文件共耗时:" + (end - start) + " 毫秒");
	}

	// FileInputStream和FileOutputStream:一次读取一个字节;method1()
	public static void method1() throws IOException {
		FileInputStream in = new FileInputStream("哥有老婆.mp4");
		FileOutputStream out = new FileOutputStream("哥有老婆2.mp4");
		int n = 0;
		while ((n = in.read()) != -1) {
			out.write(n);
		}
		//释放资源
		in.close();
		out.close();
	}

	// 2.FileInputStream和FileOutputStream:一次读取一个字节数组;method2()
	public static void method2() throws IOException {
		FileInputStream in = new FileInputStream("哥有老婆.mp4");
		FileOutputStream out = new FileOutputStream("哥有老婆3.mp4");
		byte[] byteArray = new byte[1024];
		int n = 0;
		while ((n = in.read(byteArray)) != -1) {
			out.write(byteArray, 0, n);
		}
		//释放资源
		in.close();
		out.close();
	}

	// 3.BufferedInputStream和BufferedOutputStream:一次读取一个字节:method3()
	public static void method3() throws IOException {
		BufferedInputStream in = new BufferedInputStream(
				new FileInputStream("哥有老婆.mp4"));
		BufferedOutputStream out = new BufferedOutputStream(
				new FileOutputStream("哥有老婆4.mp4"));
		
		int n = 0;
		while ((n = in.read()) != -1) {
			out.write(n);
		}
		//释放资源
		in.close();
		out.close();
	}

	// 4.4.3.BufferedInputStream和BufferedOutputStream:一次读取一个字节数组:method4();
	public static void method4() throws IOException {
		BufferedInputStream in = new BufferedInputStream(new FileInputStream(
				"哥有老婆.mp4"));
		BufferedOutputStream out = new BufferedOutputStream(
				new FileOutputStream("哥有老婆5.mp4"));
		byte[] byteArray = new byte[1024];
		int n = 0;
		while ((n = in.read(byteArray)) != -1) {
			out.write(byteArray, 0, n);
		}
		in.close();//释放资源
		out.close();</span><span style="font-family: 宋体;"><span style="font-size:14px;">//释放资源</span></span><span style="font-size:18px;">
	}
}</span><strong style="font-size:18px;">
</strong>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值