API-IO流

API-IO

学习内容

  1. 异常

  2. File

  3. 递归

  4. 字节流

  5. 转换流

  6. 字符流

  7. 其他流


一、异常

异常:异常就是Java程序在运行过程中出现的错误。

异常由来:问题也是现实生活中一个具体事务,也可以通过java的类的形式进行描述,并封装成对象。其实就是Java对不正常情况进行描述后的对象体现。

我们见过的异常,角标越界异常,空指针异常

/*
 * 举例:今天天气很好,班长出去旅游。骑着自行车,去山里面呼吸新鲜空气。
 * 		问题1:山路塌陷了,班长及时停住了,但是过不去了。严重的问题。
 * 		问题2:班长出门推自行车,发现气没了,把气吹起来。出发前就应该检查的问题。
 * 		问题3:班长骑着车在山路上惬意的行驶着,山路两边是有小石子的,中间是平坦的水泥路。
 * 			一直在平坦的水泥路上行驶是没有任何问题的,但是呢,他偏偏喜欢骑到小石子上,结果爆胎了。旅游的过程中出现的问题。
 * 			no zuo no die。
 * 
 * 程序的异常:Throwable
 * 		严重问题:Error 我们不处理。这种问题一般都是很严重的,比如说内存溢出。
 * 		问题:Exception
 * 			编译期问题:不是RuntimeException的异常 必须进行处理的,因为你不处理,编译就不能通过。
 * 			运行期问题:RuntimeException	这种问题我们也不处理,因为是你的问题,而且这个问题出现肯定是我们的代码不够严谨,需要修正代码的。
 * 
 * 如何程序出现了问题,我们没有做任何处理,最终jvm会做出默认的处理。
 * 把异常的名称,原因及出现的问题等信息输出在控制台。
 * 同时会结束程序。
 */
public class ExceptionDemo {
	public static void main(String[] args) {
		//第一阶段
		int a = 10;
		// int b = 2;
		int b = 0;
		System.out.println(a / b);
		
		//第二阶段
		System.out.println("over");
	}
}

 

异常分类

Throwable

  • |--Error 严重问题,我们不处理。

  • |--Exception

  • |--RuntimeException 运行期异常,我们需要修正代码

  • |--非RuntimeException 编译期异常,必须处理的,否则程序编译不通过

 

异常举例

除数为0

数组访问越界

 

JVM的默认处理方案

把异常的名称,错误原因及异常出现的位置等信息输出在了控制台

程序停止执行

 

异常处理方案

异常处理方案

•    trycatchfinally

•    throws

/*
 * 我们自己如何处理异常呢?
 * A:try...catch...finally
 * B:throws 抛出
 * 
 * try...catch...finally的处理格式:
 * 		try {
 * 			可能出现问题的代码;
 * 		}catch(异常名 变量) {
 * 			针对问题的处理;
 * 		}finally {
 * 			释放资源;
 * 		}
 * 
 * 变形格式:
 * 		try {
 * 			可能出现问题的代码;
 * 		}catch(异常名 变量) {
 * 			针对问题的处理;
 * 		}
 * 
 * 注意:
 * 		A:try里面的代码越少越好
 * 		B:catch里面必须有内容,哪怕是给出一个简单的提示
 */
public class ExceptionDemo {
	public static void main(String[] args) {
		// 第一阶段
		int a = 10;
		// int b = 2;
		int b = 0;

		try {
			System.out.println(a / b);
		} catch (ArithmeticException ae) {
			System.out.println("除数不能为0");
		}

		// 第二阶段
		System.out.println("over");
	}
}

 

trycatch处理方式

一个异常的情况

多个异常的情况

•    平级情况

•    存在上下级的情况

/*
 * A:一个异常
 * B:二个异常的处理
 * 		a:每一个写一个try...catch
 * 		b:写一个try,多个catch
 * 			try{
 * 				...
 * 			}catch(异常类名 变量名) {
 * 				...
 * 			}
 * 			catch(异常类名 变量名) {
 * 				...
 * 			}
 * 			...
 * 
 * 			注意事项:
 * 				1:能明确的尽量明确,不要用大的来处理。
 * 				2:平级关系的异常谁前谁后无所谓,如果出现了子父关系,父必须在后面。
 * 
 * 注意:
 * 		一旦try里面出了问题,就会在这里把问题给抛出去,然后和catch里面的问题进行匹配,
 * 		一旦有匹配的,就执行catch里面的处理,然后结束了try...catch
 * 		继续执行后面的语句。
 */
public class ExceptionDemo2 {
	public static void main(String[] args) {
		// method1();

		// method2();

		// method3();

		method4();
	}

	public static void method4() {
		int a = 10;
		int b = 0;
		int[] arr = { 1, 2, 3 };

		// 爷爷在最后
		try {
			System.out.println(a / b);
			System.out.println(arr[3]);
			System.out.println("这里出现了一个异常,你不太清楚是谁,该怎么办呢?");
		} catch (ArithmeticException e) {
			System.out.println("除数不能为0");
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("你访问了不该的访问的索引");
		} catch (Exception e) {
			System.out.println("出问题了");
		}

		// 爷爷在前面是不可以的
		// try {
		// System.out.println(a / b);
		// System.out.println(arr[3]);
		// System.out.println("这里出现了一个异常,你不太清楚是谁,该怎么办呢?");
		// } catch (Exception e) {
		// System.out.println("出问题了");
		// } catch (ArithmeticException e) {
		// System.out.println("除数不能为0");
		// } catch (ArrayIndexOutOfBoundsException e) {
		// System.out.println("你访问了不该的访问的索引");
		// }

		System.out.println("over");
	}

	// 两个异常的处理
	public static void method3() {
		int a = 10;
		int b = 0;
		int[] arr = { 1, 2, 3 };

		try {
			System.out.println(arr[3]);
			System.out.println(a / b);
			// System.out.println(arr[3]);
		} catch (ArithmeticException e) {
			System.out.println("除数不能为0");
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("你访问了不该的访问的索引");
		}

		System.out.println("over");
	}

	// 两个异常
	public static void method2() {
		int a = 10;
		int b = 0;
		try {
			System.out.println(a / b);
		} catch (ArithmeticException e) {
			System.out.println("除数不能为0");
		}

		int[] arr = { 1, 2, 3 };
		try {
			System.out.println(arr[3]);
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("你访问了不该的访问的索引");
		}

		System.out.println("over");
	}

	// 一个异常
	public static void method1() {
		// 第一阶段
		int a = 10;
		// int b = 2;
		int b = 0;

		try {
			System.out.println(a / b);
		} catch (ArithmeticException ae) {
			System.out.println("除数不能为0");
		}

		// 第二阶段
		System.out.println("over");
	}
}

•    JDK7的新特性及注意事项

/*
 * JDK7出现了一个新的异常处理方案:
 * 		try{
 * 
 * 		}catch(异常名1 | 异常名2 | ...  变量 ) {
 * 			...
 * 		}
 * 
 * 		注意:这个方法虽然简洁,但是也不够好。
 * 			A:处理方式是一致的。(实际开发中,好多时候可能就是针对同类型的问题,给出同一个处理)
 *			B:多个异常间必须是平级关系。
 */
public class ExceptionDemo3 {
	public static void main(String[] args) {
		method();
	}

	public static void method() {
		int a = 10;
		int b = 0;
		int[] arr = { 1, 2, 3 };

		// try {
		// System.out.println(a / b);
		// System.out.println(arr[3]);
		// System.out.println("这里出现了一个异常,你不太清楚是谁,该怎么办呢?");
		// } catch (ArithmeticException e) {
		// System.out.println("除数不能为0");
		// } catch (ArrayIndexOutOfBoundsException e) {
		// System.out.println("你访问了不该的访问的索引");
		// } catch (Exception e) {
		// System.out.println("出问题了");
		// }

		// JDK7的处理方案
		try {
			System.out.println(a / b);
			System.out.println(arr[3]);
		} catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {
			System.out.println("出问题了");
		}

		System.out.println("over");
	}

}

 

编译时异常和运行时异常的区别

Java中的异常被分为两大类:编译时异常和运行时异常。所有的RuntimeException类及其子类的实例被称为运行时异常,其他的异常就是编译时异常

编译时异常

•    Java程序必须显示处理,否则程序就会发生错误,无法通过编译

运行时异常

•    无需显示处理,也可以和编译时异常一样处理

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
 * 编译时异常和运行时异常的区别
 * 编译期异常:Java程序必须显示处理,否则程序就会发生错误,无法通过编译
 * 运行期异常:无需显示处理,也可以和编译时异常一样处理
 */
public class ExceptionDemo {
	public static void main(String[] args) {
		// int a = 10;
		// int b = 0;
		// if (b != 0) {
		// System.out.println(a / b);
		// }

		String s = "2014-11-20";
		// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		// Date d = sdf.parse(s);
		try {
			Date d = sdf.parse(s);
			System.out.println(d);
		} catch (ParseException e) {
			// e.printStackTrace();
			System.out.println("解析日期出问题了");
		}
	}
}

 

Throwable中的方法

getMessage()

•     获取异常信息,返回字符串。

toString()

•     获取异常类名和异常信息,返回字符串。

printStackTrace()

•     获取异常类名和异常信息,以及异常出现在程序中的位置。返回值void

printStackTrace(PrintStream s)

•     通常用该方法将异常内容保存在日志文件中,以便查阅。

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
 * 在try里面发现问题后,jvm会帮我们生成一个异常对象,然后把这个对象抛出,和catch里面的类进行匹配。
 * 如果该对象是某个类型的,就会执行该catch里面的处理信息。
 * 
 * 异常中要了解的几个方法:
 * public String getMessage():异常的消息字符串		
 * public String toString():返回异常的简单信息描述
 * 		此对象的类的 name(全路径名)
 * 		": "(冒号和一个空格) 
 * 		调用此对象 getLocalizedMessage()方法的结果 (默认返回的是getMessage()的内容)
 * printStackTrace() 获取异常类名和异常信息,以及异常出现在程序中的位置。返回值void。把信息输出在控制台。
 */
public class ExceptionDemo {
	public static void main(String[] args) {
		String s = "2014-11-20";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date d = sdf.parse(s); // 创建了一个ParseException对象,然后抛出去,和catch里面进行匹配
			System.out.println(d);
		} catch (ParseException e) { // ParseException e = new ParseException();
			// ParseException
			// e.printStackTrace();

			// getMessage()
			// System.out.println(e.getMessage());
			// Unparseable date: "2014-11-20"

			// toString()
			// System.out.println(e.toString());
			// java.text.ParseException: Unparseable date: "2014-11-20"
			
			e.printStackTrace();
			//跳转到某个指定的页面(index.html)
		}
		
		System.out.println("over");
	}
}

 

throws

定义功能方法时,需要把出现的问题暴露出来让调用者去处理。那么就通过throws在方法上标识。

举例分别演示编译时异常和运行时异常的抛出

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
 * 有些时候,我们是可以对异常进行处理的,但是又有些时候,我们根本就没有权限去处理某个异常。
 * 或者说,我处理不了,我就不处理了。
 * 为了解决出错问题,Java针对这种情况,就提供了另一种处理方案:抛出。
 * 
 * 格式:
 * 		throws 异常类名
 * 		注意:这个格式必须跟在方法的括号后面。
 * 
 * 注意:
 * 		尽量不要在main方法上抛出异常。
 * 		但是我讲课为了方便我就这样做了。
 * 
 * 小结:
 * 		编译期异常抛出,将来调用者必须处理。
 * 		运行期异常抛出,将来调用可以不用处理。
 */
public class ExceptionDemo {
	public static void main(String[] args) {
		System.out.println("今天天气很好");
		try {
			method();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		System.out.println("但是就是不该有雾霾");

		method2();
	}

	// 运行期异常的抛出
	public static void method2() throws ArithmeticException {
		int a = 10;
		int b = 0;
		System.out.println(a / b);
	}

	// 编译期异常的抛出
	// 在方法声明上抛出,是为了告诉调用者,你注意了,我有问题。
	public static void method() throws ParseException {
		String s = "2014-11-20";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d = sdf.parse(s);
		System.out.println(d);
	}
}

 

throwsthrow的区别

throws

•     用在方法声明后面,跟的是异常类名

•     可以跟多个异常类名,用逗号隔开

•     表示抛出异常,由该方法的调用者来处理

•     throws表示出现异常的一种可能性,并不一定会发生这些异常

throw

•     用在方法体内,跟的是异常对象名

•     只能抛出一个异常对象名

•     表示抛出异常,由方法体内的语句处理

•      throw则是抛出了异常,执行throw则一定抛出了某种异常 

/*
 * throw:如果出现了异常情况,我们可以把该异常抛出,这个时候的抛出的应该是异常的对象。
 * 
 * throws和throw的区别(面试题)
	throws
		用在方法声明后面,跟的是异常类名
		可以跟多个异常类名,用逗号隔开
		表示抛出异常,由该方法的调用者来处理
		throws表示出现异常的一种可能性,并不一定会发生这些异常
	throw
		用在方法体内,跟的是异常对象名
		只能抛出一个异常对象名
		表示抛出异常,由方法体内的语句处理
		throw则是抛出了异常,执行throw则一定抛出了某种异常
 */
public class ExceptionDemo {
	public static void main(String[] args) {
		// method();
		
		try {
			method2();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void method() {
		int a = 10;
		int b = 0;
		if (b == 0) {
			throw new ArithmeticException();
		} else {
			System.out.println(a / b);
		}
	}

	public static void method2() throws Exception {
		int a = 10;
		int b = 0;
		if (b == 0) {
			throw new Exception();
		} else {
			System.out.println(a / b);
		}
	}
}

 

我们到底该如何处理异常呢

原则:如果该功能内部可以将问题处理,try,如果处理不了,交由调用者处理,这是用throws

区别:

•     后续程序需要继续运行就try

•     后续程序不需要继续运行就throws

举例:

•     感冒了就自己吃点药就好了,try

•     吃了好几天药都没好结果得了H7N9,那就的得throws到医院

•     如果医院没有特效药就变成Error

 

finally的特点作用及面试题

finally的特点

•    finally控制的语句体一定会执行

•    特殊情况:在执行到finally之前jvm退出了(比如System.exit(0))

finally的作用

•    用于释放资源,在IO流操作和数据库操作中会见到

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
 * finally:被finally控制的语句体一定会执行
 * 注意:如果在执行到finally之前jvm退出了,就不能执行了。
 * 
 * A:格式
 * 		try...catch...finally...
 * B:用于释放资源,在IO流操作和数据库操作中会见到
 */
public class FinallyDemo {
	public static void main(String[] args) {
		String s = "2014-11-20";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		Date d = null;
		try {
			// System.out.println(10 / 0);
			d = sdf.parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
			System.exit(0);
		} finally {
			System.out.println("这里的代码是可以执行的");
		}

		System.out.println(d);
	}
}

finally相关的面试题

•    final,finallyfinalize的区别

•    如果catch里面有return语句,请问finally的代码还会执行吗?如果会,请问是在return前还是return后。

/*
 * 面试题:
 * 1:final,finally和finalize的区别
 * final:最终的意思,可以修饰类,成员变量,成员方法
 * 		修饰类,类不能被继承
 * 		修饰变量,变量是常量
 * 		修饰方法,方法不能被重写
 * finally:是异常处理的一部分,用于释放资源。
 * 		一般来说,代码肯定会执行,特殊情况:在执行到finally之前jvm退出了
 * finalize:是Object类的一个方法,用于垃圾回收
 * 
 * 2:如果catch里面有return语句,请问finally里面的代码还会执行吗?
 *   如果会,请问是在return前,还是return后。
 * 	   会。前。
 * 
 * 	 准确的说,应该是在中间。
 * 
 * 3:try...catch...finally的格式变形
 * 		A:try...catch...finally
 * 		B:try...catch
 * 		C:try...catch...catch...
 * 		D:try...catch...catch...finally
 * 		E:try...finally
 * 			这种做法的目前是为了释放资源。
 */
public class FinallyDemo2 {
	public static void main(String[] args) {
		System.out.println(getInt());
	}

	public static int getInt() {
		int a = 10;
		try {
			System.out.println(a / 0);
			a = 20;
		} catch (ArithmeticException e) {
			a = 30;
			return a;
			/*
			 * return a在程序执行到这一步的时候,这里不是return a而是return 30;这个返回路径就形成了。
			 * 但是呢,它发现后面还有finally,所以继续执行finally的内容,a=40
			 * 再次回到以前的返回路径,继续走return 30;
			 */
		} finally {
			a = 40;
			return a;//如果这样结果就是40了。
		}
		// return a;
	}
}

 

自定义异常

考试成绩必须在0-100之间

很明显java没有对应的异常,需要我们自己来做一个异常

自定义异常

•     继承自Exception

•      继承自RuntimeException

/*
 * java不可能对所有的情况都考虑到,所以,在实际的开发中,我们可能需要自己定义异常。
 * 而我们自己随意的写一个类,是不能作为异常类来看的,要想你的类是一个异常类,就必须继承自Exception或者RuntimeException
 * 
 * 两种方式:
 * A:继承Exception
 * B:继承RuntimeException
 */
public class MyException extends Exception {
	public MyException() {
	}

	public MyException(String message) {
		super(message);
	}
}

// public class MyException extends RuntimeException {
//
// }

 

异常注意事项

l   子类重写父类方法时,子类的方法必须抛出相同的异常或父类异常的子类。(父亲坏了,儿子不能比父亲更坏)

l   如果父类抛出了多个异常,子类重写父类时,只能抛出相同的异常或者是他的子集,子类不能抛出父类没有的异常

l   如果被重写的方法没有异常抛出,那么子类的方法绝对不可以抛出异常,如果子类方法内有异常发生,那么子类只能try,不能throws

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
 * 异常注意事项:
 * A:子类重写父类方法时,子类的方法必须抛出相同的异常或父类异常的子类。(父亲坏了,儿子不能比父亲更坏)
 * B:如果父类抛出了多个异常,子类重写父类时,只能抛出相同的异常或者是他的子集,子类不能抛出父类没有的异常
 * C:如果被重写的方法没有异常抛出,那么子类的方法绝对不可以抛出异常,如果子类方法内有异常发生,那么子类只能try,不能throws
 */
public class ExceptionDemo {

}

class Fu {
	public void show() throws Exception {
	}

	public void method() {
	}
}

class Zi extends Fu {
	@Override
	public void show() throws ArithmeticException {

	}

	@Override
	public void method() {
		// String s = "2014-11-20";
		// SimpleDateFormat sdf = new SimpleDateFormat();
		// Date d = sdf.parse(s);
		// System.out.println(d);
	}
}

二、File类

File类的概述

•    文件和目录路径名的抽象表示形式

构造方法

•    publicFile(String pathname)

•    publicFile(String parent,String child)

•    publicFile(File parent,String child)

import java.io.File;

/*
 * 我们要想实现IO的操作,就必须知道硬盘上文件的表现形式。
 * 而Java就提供了一个类File供我们使用。
 * 
 * File:文件和目录(文件夹)路径名的抽象表示形式
 * 构造方法:
 * 		File(String pathname):根据一个路径得到File对象
 * 		File(String parent, String child):根据一个目录和一个子文件/目录得到File对象
 * 		File(File parent, String child):根据一个父File对象和一个子文件/目录得到File对象
 */
public class FileDemo {
	public static void main(String[] args) {
		// File(String pathname):根据一个路径得到File对象
		// 把e:\\demo\\a.txt封装成一个File对象
		File file = new File("E:\\demo\\a.txt");

		// File(String parent, String child):根据一个目录和一个子文件/目录得到File对象
		File file2 = new File("E:\\demo", "a.txt");

		// File(File parent, String child):根据一个父File对象和一个子文件/目录得到File对象
		File file3 = new File("e:\\demo");
		File file4 = new File(file3, "a.txt");

		// 以上三种方式其实效果一样
	}
}

 

File类的成员方法

创建功能

•    public boolean createNewFile()

•    public boolean mkdir()

•    public boolean mkdirs()

import java.io.File;
import java.io.IOException;

/*
 *创建功能:
 *public boolean createNewFile():创建文件 如果存在这样的文件,就不创建了
 *public boolean mkdir():创建文件夹 如果存在这样的文件夹,就不创建了
 *public boolean mkdirs():创建文件夹,如果父文件夹不存在,会帮你创建出来
 *
 *骑白马的不一定是王子,可能是班长。
 *注意:你到底要创建文件还是文件夹,你最清楚,方法不要调错了。
 */
public class FileDemo {
	public static void main(String[] args) throws IOException {
		// 需求:我要在e盘目录下创建一个文件夹demo
		File file = new File("e:\\demo");
		System.out.println("mkdir:" + file.mkdir());

		// 需求:我要在e盘目录demo下创建一个文件a.txt
		File file2 = new File("e:\\demo\\a.txt");
		System.out.println("createNewFile:" + file2.createNewFile());

		// 需求:我要在e盘目录test下创建一个文件b.txt
		// Exception in thread "main" java.io.IOException: 系统找不到指定的路径。
		// 注意:要想在某个目录下创建内容,该目录首先必须存在。
		// File file3 = new File("e:\\test\\b.txt");
		// System.out.println("createNewFile:" + file3.createNewFile());

		// 需求:我要在e盘目录test下创建aaa目录
		// File file4 = new File("e:\\test\\aaa");
		// System.out.println("mkdir:" + file4.mkdir());

		// File file5 = new File("e:\\test");
		// File file6 = new File("e:\\test\\aaa");
		// System.out.println("mkdir:" + file5.mkdir());
		// System.out.println("mkdir:" + file6.mkdir());

		// 其实我们有更简单的方法
		File file7 = new File("e:\\aaa\\bbb\\ccc\\ddd");
		System.out.println("mkdirs:" + file7.mkdirs());

		// 看下面的这个东西:
		File file8 = new File("e:\\liuyi\\a.txt");
		System.out.println("mkdirs:" + file8.mkdirs());
	}
}

删除功能

•    public boolean delete()

import java.io.File;
import java.io.IOException;

/*
 * 删除功能:public boolean delete()
 * 
 * 注意:
 * 		A:如果你创建文件或者文件夹忘了写盘符路径,那么,默认在项目路径下。
 * 		B:Java中的删除不走回收站。
 * 		C:要删除一个文件夹,请注意该文件夹内不能包含文件或者文件夹
 */
public class FileDemo {
	public static void main(String[] args) throws IOException {
		// 创建文件
		// File file = new File("e:\\a.txt");
		// System.out.println("createNewFile:" + file.createNewFile());

		// 我不小心写成这个样子了
		File file = new File("a.txt");
		System.out.println("createNewFile:" + file.createNewFile());

		// 继续玩几个
		File file2 = new File("aaa\\bbb\\ccc");
		System.out.println("mkdirs:" + file2.mkdirs());

		// 删除功能:我要删除a.txt这个文件
		File file3 = new File("a.txt");
		System.out.println("delete:" + file3.delete());

		// 删除功能:我要删除ccc这个文件夹
		File file4 = new File("aaa\\bbb\\ccc");
		System.out.println("delete:" + file4.delete());

		// 删除功能:我要删除aaa文件夹
		// File file5 = new File("aaa");
		// System.out.println("delete:" + file5.delete());

		File file6 = new File("aaa\\bbb");
		File file7 = new File("aaa");
		System.out.println("delete:" + file6.delete());
		System.out.println("delete:" + file7.delete());
	}
}

重命名功能

•    public boolean renameTo(File dest)

import java.io.File;

/*
 * 重命名功能:public boolean renameTo(File dest)
 * 		如果路径名相同,就是改名。
 * 		如果路径名不同,就是改名并剪切。
 * 
 * 路径以盘符开始:绝对路径	c:\\a.txt
 * 路径不以盘符开始:相对路径	a.txt
 */
public class FileDemo {
	public static void main(String[] args) {
		// 创建一个文件对象
		// File file = new File("林青霞.jpg");
		// // 需求:我要修改这个文件的名称为"东方不败.jpg"
		// File newFile = new File("东方不败.jpg");
		// System.out.println("renameTo:" + file.renameTo(newFile));

		File file2 = new File("东方不败.jpg");
		File newFile2 = new File("e:\\林青霞.jpg");
		System.out.println("renameTo:" + file2.renameTo(newFile2));
	}
}

 

File类的成员方法

判断功能

•    public boolean isDirectory()

•    public boolean isFile()

•    public boolean exists()

•    public boolean canRead()

•    public boolean canWrite()

•    public boolean isHidden()

import java.io.File;

/*
 * 判断功能:
 * public boolean isDirectory():判断是否是目录
 * public boolean isFile():判断是否是文件
 * public boolean exists():判断是否存在
 * public boolean canRead():判断是否可读
 * public boolean canWrite():判断是否可写
 * public boolean isHidden():判断是否隐藏
 */
public class FileDemo {
	public static void main(String[] args) {
		// 创建文件对象
		File file = new File("a.txt");

		System.out.println("isDirectory:" + file.isDirectory());// false
		System.out.println("isFile:" + file.isFile());// true
		System.out.println("exists:" + file.exists());// true
		System.out.println("canRead:" + file.canRead());// true
		System.out.println("canWrite:" + file.canWrite());// true
		System.out.println("isHidden:" + file.isHidden());// false
	}
}

 

File类的成员方法

基本获取功能

•    public String getAbsolutePath()

•    public String getPath()

•    public String getName()

•    public long length()

•    public long lastModified()

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
 * 获取功能:
 * public String getAbsolutePath():获取绝对路径
 * public String getPath():获取相对路径
 * public String getName():获取名称
 * public long length():获取长度。字节数
 * public long lastModified():获取最后一次的修改时间,毫秒值
 */
public class FileDemo {
	public static void main(String[] args) {
		// 创建文件对象
		File file = new File("demo\\test.txt");

		System.out.println("getAbsolutePath:" + file.getAbsolutePath());
		System.out.println("getPath:" + file.getPath());
		System.out.println("getName:" + file.getName());
		System.out.println("length:" + file.length());
		System.out.println("lastModified:" + file.lastModified());

		// 1416471971031
		Date d = new Date(1416471971031L);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String s = sdf.format(d);
		System.out.println(s);
	}
}

 

高级获取功能

•    public String[] list()

•    public File[] listFiles()

import java.io.File;

/*
 * 获取功能:
 * public String[] list():获取指定目录下的所有文件或者文件夹的名称数组
 * public File[] listFiles():获取指定目录下的所有文件或者文件夹的File数组
 */
public class FileDemo {
	public static void main(String[] args) {
		// 指定一个目录
		File file = new File("e:\\");

		// public String[] list():获取指定目录下的所有文件或者文件夹的名称数组
		String[] strArray = file.list();
		for (String s : strArray) {
			System.out.println(s);
		}
		System.out.println("------------");

		// public File[] listFiles():获取指定目录下的所有文件或者文件夹的File数组
		File[] fileArray = file.listFiles();
		for (File f : fileArray) {
			System.out.println(f.getName());
		}
	}
}

File类练习

判断E盘目录下是否有后缀名为.jpg的文件,如果有,就输出此文件名称

import java.io.File;

/*
 * 判断E盘目录下是否有后缀名为.jpg的文件,如果有,就输出此文件名称
 * 
 * 分析:
 * 		A:封装e判断目录
 * 		B:获取该目录下所有文件或者文件夹的File数组
 * 		C:遍历该File数组,得到每一个File对象,然后判断
 * 		D:是否是文件
 * 			是:继续判断是否以.jpg结尾
 * 				是:就输出该文件名称
 * 				否:不搭理它
 * 			否:不搭理它
 */
public class FileDemo {
	public static void main(String[] args) {
		// 封装e判断目录
		File file = new File("e:\\");

		// 获取该目录下所有文件或者文件夹的File数组
		File[] fileArray = file.listFiles();

		// 遍历该File数组,得到每一个File对象,然后判断
		for (File f : fileArray) {
			// 是否是文件
			if (f.isFile()) {
				// 继续判断是否以.jpg结尾
				if (f.getName().endsWith(".jpg")) {
					// 就输出该文件名称
					System.out.println(f.getName());
				}
			}
		}
	}
}

 

文件名称过滤器的实现思想及代码

•     public String[] list(FilenameFilter filter)

•     public File[] listFiles(FilenameFilter filter)

•     查看源码看执行流程

import java.io.File;
import java.io.FilenameFilter;

/*
 * 判断E盘目录下是否有后缀名为.jpg的文件,如果有,就输出此文件名称
 * A:先获取所有的,然后遍历的时候,依次判断,如果满足条件就输出。
 * B:获取的时候就已经是满足条件的了,然后输出即可。
 * 
 * 要想实现这个效果,就必须学习一个接口:文件名称过滤器
 * public String[] list(FilenameFilter filter)
 * public File[] listFiles(FilenameFilter filter)
 */
public class FileDemo2 {
	public static void main(String[] args) {
		// 封装e判断目录
		File file = new File("e:\\");

		// 获取该目录下所有文件或者文件夹的String数组
		// public String[] list(FilenameFilter filter)
		String[] strArray = file.list(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				// return false;
				// return true;
				// 通过这个测试,我们就知道了,到底把这个文件或者文件夹的名称加不加到数组中,取决于这里的返回值是true还是false
				// 所以,这个的true或者false应该是我们通过某种判断得到的
				// System.out.println(dir + "---" + name);
				// File file = new File(dir, name);
				// // System.out.println(file);
				// boolean flag = file.isFile();
				// boolean flag2 = name.endsWith(".jpg");
				// return flag && flag2;
				return new File(dir, name).isFile() && name.endsWith(".jpg");
			}
		});

		// 遍历
		for (String s : strArray) {
			System.out.println(s);
		}
	}
}

|  案例

import java.io.File;

/*
 * 需求:把E:\评书\三国演义下面的视频名称修改为
 * 		00?_介绍.avi
 * 
 * 思路:
 * 		A:封装目录
 * 		B:获取该目录下所有的文件的File数组
 * 		C:遍历该File数组,得到每一个File对象
 * 		D:拼接一个新的名称,然后重命名即可。
 */
public class FileDemo {
	public static void main(String[] args) {
		// 封装目录
		File srcFolder = new File("E:\\评书\\三国演义");

		// 获取该目录下所有的文件的File数组
		File[] fileArray = srcFolder.listFiles();

		// 遍历该File数组,得到每一个File对象
		for (File file : fileArray) {
			// System.out.println(file);
			// E:\评书\三国演义\三国演义_001_[评书网-今天很高兴,明天就IO了]_桃园三结义.avi
			// 改后:E:\评书\三国演义\001_桃园三结义.avi
			String name = file.getName(); // 三国演义_001_[评书网-今天很高兴,明天就IO了]_桃园三结义.avi

			int index = name.indexOf("_");
			String numberString = name.substring(index + 1, index + 4);
			// System.out.println(numberString);

			// int startIndex = name.lastIndexOf('_');
			// int endIndex = name.lastIndexOf('.');
			// String nameString = name.substring(startIndex + 1, endIndex);
			// System.out.println(nameString);
			int endIndex = name.lastIndexOf('_');
			String nameString = name.substring(endIndex);

			String newName = numberString.concat(nameString); // 001_桃园三结义.avi
			// System.out.println(newName);

			File newFile = new File(srcFolder, newName); // E:\\评书\\三国演义\\001_桃园三结义.avi

			// 重命名即可
			file.renameTo(newFile);
		}
	}
}

三、递归的思想

递归

•    方法定义中调用方法本身的现象

递归注意实现

•    要有出口,否则就是死递归

•    次数不能太多,否则就内存溢出

•    构造方法不能递归使用

/*
 * 递归:方法定义中调用方法本身的现象
 * 
 * 方法的嵌套调用,这不是递归。
 * Math.max(Math.max(a,b),c);
 * 
 * public void show(int n) {
 * 		if(n <= 0) {
 * 			System.exit(0);
 * 		}
 * 		System.out.println(n);
 * 		show(--n);
 * }
 * 
 * 注意事项:
 * 		A:递归一定要有出口,否则就是死递归
 * 		B:递归的次数不能太多,否则就内存溢出
 * 		C:构造方法不能递归使用
 * 
 * 举例:
 * 		A:从前有座山,山里有座庙,庙里有个老和尚和小和尚,老和尚在给小和尚讲故事,故事是:
 * 			从前有座山,山里有座庙,庙里有个老和尚和小和尚,老和尚在给小和尚讲故事,故事是:
 * 				从前有座山,山里有座庙,庙里有个老和尚和小和尚,老和尚在给小和尚讲故事,故事是:
 * 					从前有座山,山里有座庙,庙里有个老和尚和小和尚,老和尚在给小和尚讲故事,故事是:
 * 						...
 * 					庙挂了,或者山崩了
 * 		B:学编程  -- 高薪就业 -- 挣钱 -- 娶媳妇 -- 生娃娃 -- 放羊 -- 挣学费
 * 			 学编程  -- 高薪就业 -- 挣钱 -- 娶媳妇 -- 生娃娃 -- 放羊 -- 挣学费
 * 				学编程  -- 高薪就业 -- 挣钱 -- 娶媳妇 -- 生娃娃 -- 放羊 -- 挣学费
 * 					学编程  -- 高薪就业 -- 挣钱 -- 娶媳妇 -- 生娃娃 -- 放羊 -- 挣学费
 * 						...
 * 					娶不到媳妇或者生不了娃娃	
 */
public class DiGuiDemo {
	// public DiGuiDemo() {
	// DiGuiDemo();
	// }
}

 

递归解决问题的思想及内存图

递归解决问题的思想

•    找到出口

•    找到规律

求阶乘案例

•    普通for循环

•    递归实现

•    画内存图

•    断点查看执行流程

/*
 * 需求:请用代码实现求5的阶乘。
 * 下面的知识要知道:
 * 		5! = 1*2*3*4*5
 * 		5! = 5*4!
 * 
 * 有几种方案实现呢?
 * 		A:循环实现
 * 		B:递归实现
 * 			a:做递归要写一个方法
 * 			b:出口条件
 * 			c:规律
 */
public class DiGuiDemo {
	public static void main(String[] args) {
		int jc = 1;
		for (int x = 2; x <= 5; x++) {
			jc *= x;
		}
		System.out.println("5的阶乘是:" + jc);
		
		System.out.println("5的阶乘是:"+jieCheng(5));
	}
	
	/*
	 * 做递归要写一个方法:
	 * 		返回值类型:int
	 * 		参数列表:int n
	 * 出口条件:
	 * 		if(n == 1) {return 1;}
	 * 规律:
	 * 		if(n != 1) {return n*方法名(n-1);}
	 */
	public static int jieCheng(int n){
		if(n==1){
			return 1;
		}else {
			return n*jieCheng(n-1);
		}
	}
}

 

递归练习

兔子问题(斐波那契数列)

/*
 * 有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问第二十个月的兔子对数为多少?
 * 分析:我们要想办法找规律
 * 			兔子对数
 * 第一个月: 	1
 * 第二个月:	1
 * 第三个月:	2
 * 第四个月:	3	
 * 第五个月:	5
 * 第六个月:	8
 * ...
 * 
 * 由此可见兔子对象的数据是:
 * 		1,1,2,3,5,8...
 * 规则:
 * 		A:从第三项开始,每一项是前两项之和
 * 		B:而且说明前两项是已知的
 * 
 * 如何实现这个程序呢?
 * 		A:数组实现
 * 		B:变量的变化实现
 * 		C:递归实现
 * 
 * 假如相邻的两个月的兔子对数是a,b
 * 第一个相邻的数据:a=1,b=1
 * 第二个相邻的数据:a=1,b=2
 * 第三个相邻的数据:a=2,b=3
 * 第四个相邻的数据:a=3,b=5
 * 看到了:下一次的a是以前的b,下一次是以前的a+b	
 */
public class DiGuiDemo2 {
	public static void main(String[] args) {
		// 定义一个数组
		int[] arr = new int[20];
		arr[0] = 1;
		arr[1] = 1;
		// arr[2] = arr[0] + arr[1];
		// arr[3] = arr[1] + arr[2];
		// ...
		for (int x = 2; x < arr.length; x++) {
			arr[x] = arr[x - 2] + arr[x - 1];
		}
		System.out.println(arr[19]);// 6765
		System.out.println("----------------");

		int a = 1;
		int b = 1;
		for (int x = 0; x < 18; x++) {
			// 临时变量存储上一次的a
			int temp = a;
			a = b;
			b = temp + b;
		}
		System.out.println(b);
		System.out.println("----------------");

		System.out.println(fib(20));
	}

	/*
	 * 方法: 返回值类型:int 参数列表:int n 出口条件: 第一个月是1,第二个月是1 规律: 从第三个月开始,每一个月是前两个月之和
	 */
	public static int fib(int n) {
		if (n == 1 || n == 2) {
			return 1;
		} else {
			return fib(n - 1) + fib(n - 2);
		}
	}
}

递归遍历目录下指定后缀名结尾的文件名称

import java.io.File;

/*
 * 需求:请大家把E:\JavaSE目录下所有的java结尾的文件的绝对路径给输出在控制台。
 * 
 * 分析:
 * 		A:封装目录
 * 		B:获取该目录下所有的文件或者文件夹的File数组
 * 		C:遍历该File数组,得到每一个File对象
 * 		D:判断该File对象是否是文件夹
 * 			是:回到B
 * 			否:继续判断是否以.java结尾
 * 				是:就输出该文件的绝对路径
 * 				否:不搭理它
 */
public class FilePathDemo {
	public static void main(String[] args) {
		// 封装目录
		File srcFolder = new File("E:\\JavaSE");

		// 递归功能实现
		getAllJavaFilePaths(srcFolder);
	}

	private static void getAllJavaFilePaths(File srcFolder) {
		// 获取该目录下所有的文件或者文件夹的File数组
		File[] fileArray = srcFolder.listFiles();

		// 遍历该File数组,得到每一个File对象
		for (File file : fileArray) {
			// 判断该File对象是否是文件夹
			if (file.isDirectory()) {
				getAllJavaFilePaths(file);
			} else {
				// 继续判断是否以.java结尾
				if (file.getName().endsWith(".java")) {
					// 就输出该文件的绝对路径
					System.out.println(file.getAbsolutePath());
				}
			}
		}
	}
}

递归删除带内容的目录

import java.io.File;

/*
 * 需求:递归删除带内容的目录
 * 
 * 目录我已经给定:demo
 * 
 * 分析:
 * 		A:封装目录
 * 		B:获取该目录下的所有文件或者文件夹的File数组
 * 		C:遍历该File数组,得到每一个File对象
 * 		D:判断该File对象是否是文件夹
 * 			是:回到B
 * 			否:就删除
 */
public class FileDeleteDemo {
	public static void main(String[] args) {
		// 封装目录
		File srcFolder = new File("demo");
		// 递归实现
		deleteFolder(srcFolder);
	}

	private static void deleteFolder(File srcFolder) {
		// 获取该目录下的所有文件或者文件夹的File数组
		File[] fileArray = srcFolder.listFiles();

		if (fileArray != null) {
			// 遍历该File数组,得到每一个File对象
			for (File file : fileArray) {
				// 判断该File对象是否是文件夹
				if (file.isDirectory()) {
					deleteFolder(file);
				} else {
					System.out.println(file.getName() + "---" + file.delete());
				}
			}

			System.out
					.println(srcFolder.getName() + "---" + srcFolder.delete());
		}
	}
}

四、IO流

IO流用来处理设备之间的数据传输

•     上传文件和下载文件

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

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

IO流分类

按照数据流向

•    输入流     读入数据

•    输出流     写出数据

按照数据类型

•    字节流

•    字符流

•    什么情况下使用哪种流呢?

•   如果数据所在的文件通过windows自带的记事本打开并能读懂里面的内容,就用字符流。其他用字节流。

•   如果你什么都不知道,就用字节流

 

IO流常用基类

字节流的抽象基类:

•     InputStream OutputStream

字符流的抽象基类:

•     Reader Writer

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

•     如:InputStream的子类FileInputStream

•     如:Reader的子类FileReader

 

字节流写数据

OutputStream

•    FileOutputStream

往一个文本文件中写一句话:helloworld

•    分析发现其实更适合用字符流,但是由于字节流先出现,所以,我们考虑先使用字节流后面再讲会什么出现字符流。

FileOutputStream的构造方法

•    FileOutputStream(Filefile)

•    FileOutputStream(Stringname)

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * IO流的分类:
 * 		流向:
 * 			输入流	读取数据
 * 			输出流 写出数据
 * 		数据类型:
 * 			字节流
 * 				字节输入流	读取数据	InputStream
 * 				字节输出流	写出数据	OutputStream
 * 			字符流
 * 				字符输入流	读取数据	Reader
 * 				字符输出流	写出数据	Writer
 * 
 * 		注意:一般我们在探讨IO流的时候,如果没有明确说明按哪种分类来说,默认情况下是按照数据类型来分的。
 * 
 * 需求:我要往一个文本文件中输入一句话:"hello,io"
 * 
 * 分析:
 * 		A:这个操作最好是采用字符流来做,但是呢,字符流是在字节流之后才出现的,所以,今天我先讲解字节流如何操作。
 * 		B:由于我是要往文件中写一句话,所以我们要采用字节输出流。
 * 
 * 通过上面的分析后我们知道要使用:OutputStream
 * 但是通过查看API,我们发现该流对象是一个抽象类,不能实例化。
 * 所以,我们要找一个具体的子类。
 * 而我们要找的子类是什么名字的呢?这个时候,很简单,我们回想一下,我们是不是要往文件中写东西。
 * 文件是哪个单词:File
 * 然后用的是字节输出流,联起来就是:FileOutputStream
 * 注意:每种基类的子类都是以父类名作为后缀名。
 * 		XxxOutputStream
 * 		XxxInputStream
 * 		XxxReader
 * 		XxxWriter
 * 查看FileOutputStream的构造方法:
 * 		FileOutputStream(File file) 
 *		FileOutputStream(String name)
 *
 * 字节输出流操作步骤:
 * 		A:创建字节输出流对象
 * 		B:写数据
 * 		C:释放资源
 */
public class FileOutputStreamDemo {
	public static void main(String[] args) throws IOException {
		// 创建字节输出流对象
		// FileOutputStream(File file)
		// File file = new File("fos.txt");
		// FileOutputStream fos = new FileOutputStream(file);
		// FileOutputStream(String name)
		FileOutputStream fos = new FileOutputStream("fos.txt");
		/*
		 * 创建字节输出流对象了做了几件事情:
		 * A:调用系统功能去创建文件
		 * B:创建fos对象
		 * C:把fos对象指向这个文件
		 */
		
		//写数据
		fos.write("hello,IO".getBytes());
		fos.write("java".getBytes());
		
		//释放资源
		//关闭此文件输出流并释放与此流有关的所有系统资源。
		fos.close();
		/*
		 * 为什么一定要close()呢?
		 * A:让流对象变成垃圾,这样就可以被垃圾回收器回收了
		 * B:通知系统去释放跟该文件相关的资源
		 */
		//java.io.IOException: Stream Closed
		//fos.write("java".getBytes());
	}
}

 

字节流写数据的方式

public void write(int b)

public void write(byte[]b)

public void write(byte[]b,int off,int len)

import java.io.FileOutputStream;
import java.io.IOException;

/*
 * 字节输出流操作步骤:
 * A:创建字节输出流对象
 * B:调用write()方法
 * C:释放资源
 * 
 * public void write(int b):写一个字节
 * public void write(byte[] b):写一个字节数组
 * public void write(byte[] b,int off,int len):写一个字节数组的一部分
 */
public class FileOutputStreamDemo2 {
	public static void main(String[] args) throws IOException {
		// 创建字节输出流对象
		// OutputStream os = new FileOutputStream("fos2.txt"); // 多态
		FileOutputStream fos = new FileOutputStream("fos2.txt");

		// 调用write()方法
		//fos.write(97); //97 -- 底层二进制数据	-- 通过记事本打开 -- 找97对应的字符值 -- a
		// fos.write(57);
		// fos.write(55);
		
		//public void write(byte[] b):写一个字节数组
		byte[] bys={97,98,99,100,101};
		fos.write(bys);
		
		//public void write(byte[] b,int off,int len):写一个字节数组的一部分
		fos.write(bys,1,3);
		
		//释放资源
		fos.close();
	}
}

 

字节流写数据常见问题

创建字节输出流到底做了哪些事情?

数据写成功后,为什么要close()?

如何实现数据的换行?

如何实现数据的追加写入?

import java.io.FileOutputStream;
import java.io.IOException;

/*
 * 如何实现数据的换行?
 * 		为什么现在没有换行呢?因为你值写了字节数据,并没有写入换行符号。
 * 		如何实现呢?写入换行符号即可呗。
 * 		刚才我们看到了有写文本文件打开是可以的,通过windows自带的那个不行,为什么呢?
 * 		因为不同的系统针对不同的换行符号识别是不一样的?
 * 		windows:\r\n
 * 		linux:\n
 * 		Mac:\r
 * 		而一些常见的个高级记事本,是可以识别任意换行符号的。
 * 
 * 如何实现数据的追加写入?
 * 		用构造方法带第二个参数是true的情况即可
 */
public class FileOutputStreamDemo3 {
	public static void main(String[] args) throws IOException {
		// 创建字节输出流对象
		// FileOutputStream fos = new FileOutputStream("fos3.txt");
		// 创建一个向具有指定 name 的文件中写入数据的输出文件流。如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。
		FileOutputStream fos = new FileOutputStream("fos3.txt", true);

		// 写数据
		for (int x = 0; x < 10; x++) {
			fos.write(("hello" + x).getBytes());
			fos.write("\r\n".getBytes());
		}

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

 

字节流写数据加入异常处理

加入异常处理的IO流操作

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * 加入异常处理的字节输出流操作
 */
public class FileOutputStreamDemo4 {
	public static void main(String[] args) {
		// 分开做异常处理
		// FileOutputStream fos = null;
		// try {
		// fos = new FileOutputStream("fos4.txt");
		// } catch (FileNotFoundException e) {
		// e.printStackTrace();
		// }
		//
		// try {
		// fos.write("java".getBytes());
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
		//
		// try {
		// fos.close();
		// } catch (IOException e) {
		// e.printStackTrace();
		// }

		// 一起做异常处理
		// try {
		// FileOutputStream fos = new FileOutputStream("fos4.txt");
		// fos.write("java".getBytes());
		// fos.close();
		// } catch (FileNotFoundException e) {
		// e.printStackTrace();
		// } catch (IOException e) {
		// e.printStackTrace();
		// }

		// 改进版
		// 为了在finally里面能够看到该对象就必须定义到外面,为了访问不出问题,还必须给初始化值
		FileOutputStream fos = null;
		try {
			// fos = new FileOutputStream("z:\\fos4.txt");
			fos = new FileOutputStream("fos4.txt");
			fos.write("java".getBytes());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 如果fos不是null,才需要close()
			if (fos != null) {
				// 为了保证close()一定会执行,就放到这里了
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

 

字节流读取数据

InputStream

•    FileInputStream

把刚才写的数据读取出来显示在控制台

FileInputStream的构造方法

•    FileInputStream(Filefile)

•    FileInputStream(Stringname)

FileInputStream的成员方法

•    public int read()

•    public int read(byte[]b)

import java.io.FileInputStream;
import java.io.IOException;

/*
 * 字节输入流操作步骤:
 * A:创建字节输入流对象
 * B:调用read()方法读取数据,并把数据显示在控制台
 * C:释放资源
 * 
 * 读取数据的方式:
 * A:int read():一次读取一个字节
 * B:int read(byte[] b):一次读取一个字节数组
 */
public class FileInputStreamDemo {
	public static void main(String[] args) throws IOException {
		// FileInputStream(String name)
		// FileInputStream fis = new FileInputStream("fis.txt");
		FileInputStream fis = new FileInputStream("FileOutputStreamDemo.java");

		// // 调用read()方法读取数据,并把数据显示在控制台
		// // 第一次读取
		// int by = fis.read();
		// System.out.println(by);
		// System.out.println((char) by);
		//
		// // 第二次读取
		// by = fis.read();
		// System.out.println(by);
		// System.out.println((char) by);
		//
		// // 第三次读取
		// by = fis.read();
		// System.out.println(by);
		// System.out.println((char) by);
		// // 我们发现代码的重复度很高,所以我们要用循环改进
		// // 而用循环,最麻烦的事情是如何控制循环判断条件呢?
		// // 第四次读取
		// by = fis.read();
		// System.out.println(by);
		// // 第五次读取
		// by = fis.read();
		// System.out.println(by);
		// //通过测试,我们知道如果你读取的数据是-1,就说明已经读取到文件的末尾了

		// 用循环改进
		// int by = fis.read();
		// while (by != -1) {
		// System.out.print((char) by);
		// by = fis.read();
		// }

		// 最终版代码
		int by = 0;
		// 读取,赋值,判断
		while ((by = fis.read()) != -1) {
			System.out.print((char) by);
		}

		// 释放资源
		fis.close();
	}
}
import java.io.FileInputStream;
import java.io.IOException;

/*
 * 一次读取一个字节数组:int read(byte[] b)
 * 返回值其实是实际读取的字节个数。
 */
public class FileInputStreamDemo2 {
	public static void main(String[] args) throws IOException {
		// 创建字节输入流对象
		// FileInputStream fis = new FileInputStream("fis2.txt");
		FileInputStream fis = new FileInputStream("FileOutputStreamDemo.java");

		// 读取数据
		// 定义一个字节数组
		// 第一次读取
		// byte[] bys = new byte[5];
		// int len = fis.read(bys);
		// // System.out.println(len);
		// // System.out.println(new String(bys));
		// // System.out.println(new String(bys, 0, len));
		// System.out.print(new String(bys, 0, len));
		//
		// // 第二次读取
		// len = fis.read(bys);
		// // System.out.println(len);
		// // System.out.println(new String(bys));
		// // System.out.println(new String(bys, 0, len));
		// System.out.print(new String(bys, 0, len));
		//
		// // 第三次读取
		// len = fis.read(bys);
		// // System.out.println(len);
		// // System.out.println(new String(bys));
		// // System.out.println(new String(bys, 0, len));
		// System.out.print(new String(bys, 0, len));
		//
		// // 第四次读取
		// len = fis.read(bys);
		// // System.out.println(len);
		// // System.out.println(new String(bys, 0, len));
		// System.out.print(new String(bys, 0, len));
		// // 代码重复了,用循环改进
		// // 但是,我不知道结束条件
		// // len = fis.read(bys);
		// // System.out.println(len);
		// // len = fis.read(bys);
		// // System.out.println(len);
		// // 如果读取到的实际长度是-1,就说明没有数据了

		// byte[] bys = new byte[115]; // 0
		// int len = 0;
		// while ((len = fis.read(bys)) != -1) {
		// System.out.print(new String(bys, 0, len));
		// // System.out.print(new String(bys)); //千万要带上len的使用
		// }

		// 最终版代码
		// 数组的长度一般是1024或者1024的整数倍
		byte[] bys = new byte[1024];
		int len = 0;
		while ((len = fis.read(bys)) != -1) {
			System.out.print(new String(bys, 0, len));
		}

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

 

字节流读取数据两种方式图解比较

一次读取一个字节

一次读取一个字节数组

•    每次可以读取多个数据,提高了操作效率

 

字节流复制数据练习

把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * 复制文本文件。
 * 
 * 数据源:从哪里来
 * a.txt	--	读取数据	--	FileInputStream	
 * 
 * 目的地:到哪里去
 * b.txt	--	写数据		--	FileOutputStream
 * 
 * java.io.FileNotFoundException: a.txt (系统找不到指定的文件。)
 * 
 * 这一次复制中文没有出现任何问题,为什么呢?
 * 上一次我们出现问题的原因在于我们每次获取到一个字节数据,就把该字节数据转换为了字符数据,然后输出到控制台。
 * 而这一次呢?确实通过IO流读取数据,写到文本文件,你读取一个字节,我就写入一个字节,你没有做任何的转换。
 * 它会自己做转换。
 */
public class CopyFileDemo {
	public static void main(String[] args) throws IOException {
		// 封装数据源
		FileInputStream fis = new FileInputStream("a.txt");
		// 封装目的地
		FileOutputStream fos = new FileOutputStream("b.txt");

		int by = 0;
		while ((by = fis.read()) != -1) {
			fos.write(by);
		}

		// 释放资源(先关谁都行)
		fos.close();
		fis.close();
	}
}
import java.util.Arrays;

/*
 * 计算机是如何识别什么时候该把两个字节转换为一个中文呢?
 * 在计算机中中文的存储分两个字节:
 * 		第一个字节肯定是负数。
 * 		第二个字节常见的是负数,可能有正数。但是没影响。
 */
public class StringDemo {
	public static void main(String[] args) {
		// String s = "abcde";
		// // [97, 98, 99, 100, 101]

		String s = "我爱你中国";
		// [-50, -46, -80, -82, -60, -29, -42, -48, -71, -6]

		byte[] bys = s.getBytes();
		System.out.println(Arrays.toString(bys));
	}
}

c:\\a.txt内容复制到d:\\b.txt

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * 需求:把c:\\a.txt内容复制到d:\\b.txt中
 * 
 * 数据源:
 * 		c:\\a.txt	--	读取数据	--	FileInputStream
 * 目的地:
 * 		d:\\b.txt	--	写出数据	--	FileOutputStream
 */
public class CopyFileDemo {
	public static void main(String[] args) throws IOException {
		// 封装数据源
		FileInputStream fis = new FileInputStream("c:\\a.txt");
		FileOutputStream fos = new FileOutputStream("d:\\b.txt");

		// 复制数据
		byte[] bys = new byte[1024];
		int len = 0;
		while ((len = fis.read(bys)) != -1) {
			fos.write(bys, 0, len);
		}

		// 释放资源
		fos.close();
		fis.close();
	}
}

e:\\林青霞.jpg内容复制到当前项目目录下的mn.jpg

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * 需求:把e:\\林青霞.jpg内容复制到当前项目目录下的mn.jpg中
 * 
 * 数据源:
 * 		e:\\林青霞.jpg	--读取数据--FileInputStream
 * 目的地:
 * 		mn.jpg--写出数据--FileOutputStream
 */
public class CopyImageDemo {
	public static void main(String[] args) throws IOException {
		// 封装数据源
		FileInputStream fis = new FileInputStream("e:\\林青霞.jpg");
		// 封装目的地
		FileOutputStream fos = new FileOutputStream("mn.jpg");

		// 复制数据
		int by = 0;
		while ((by = fis.read()) != -1) {
			fos.write(by);
		}

		// 释放资源
		fos.close();
		fis.close();
	}
}

e:\\哥有老婆.mp4复制到当前项目目录下的copy.mp4

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * 需求:把e:\\哥有老婆.mp4复制到当前项目目录下的copy.mp4中
 * 
 * 数据源:
 * 		e:\\哥有老婆.mp4--读取数据--FileInputStream
 * 目的地:
 * 		copy.mp4--写出数据--FileOutputStream
 */
public class CopyMp4Demo {
	public static void main(String[] args) throws IOException {
		// 封装数据源
		FileInputStream fis = new FileInputStream("e:\\哥有老婆.mp4");
		// 封装目的地
		FileOutputStream fos = new FileOutputStream("copy.mp4");

		// 复制数据
		int by = 0;
		while ((by = fis.read()) != -1) {
			fos.write(by);
		}

		// 释放资源
		fos.close();
		fis.close();
	}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * 需求:把e:\\哥有老婆.mp4复制到当前项目目录下的copy.mp4中
 * 
 * 数据源:
 * 		e:\\哥有老婆.mp4--读取数据--FileInputStream
 * 目的地:
 * 		copy.mp4--写出数据--FileOutputStream
 */
public class CopyMp4Demo {
	public static void main(String[] args) throws IOException {
		// 封装数据源
		FileInputStream fis = new FileInputStream("e:\\哥有老婆.mp4");
		// 封装目的地
		FileOutputStream fos = new FileOutputStream("copy.mp4");

		// 复制数据
		byte[] bys = new byte[1024];
		int len = 0;
		while ((len = fis.read(bys)) != -1) {
			fos.write(bys, 0, len);
		}

		// 释放资源
		fos.close();
		fis.close();
	}
}

 

字节缓冲流

字节流一次读写一个数组的速度明显比一次读写一个字节的速度快很多,这是加入了数组这样的缓冲区效果,java本身在设计的时候,也考虑到了这样的设计思想(装饰设计模式后面讲解),所以提供了字节缓冲区流

字节缓冲输出流

•    BufferedOutputStream

字节缓冲输入流

•    BufferedInputStream

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * 通过定义数组的方式确实比以前一次读取一个字节的方式快很多,所以,看来有一个缓冲区还是非常好的。
 * 既然是这样的话,那么,java开始在设计的时候,它也考虑到了这个问题,就专门提供了带缓冲区的字节类。
 * 这种类被称为:缓冲区类(高效类)
 * 写数据:BufferedOutputStream
 * 读数据:BufferedInputStream
 * 
 * 构造方法可以指定缓冲区的大小,但是我们一般用不上,因为默认缓冲区大小就足够了。
 * 
 * 为什么不传递一个具体的文件或者文件路径,而是传递一个OutputStream对象呢?
 * 原因很简单,字节缓冲区流仅仅提供缓冲区,为高效而设计的。但是呢,真正的读写操作还得靠基本的流对象实现。
 */
public class BufferedOutputStreamDemo {
	public static void main(String[] args) throws IOException {
		// BufferedOutputStream(OutputStream out)
		// FileOutputStream fos = new FileOutputStream("bos.txt");
		// BufferedOutputStream bos = new BufferedOutputStream(fos);
		// 简单写法
		BufferedOutputStream bos = new BufferedOutputStream(
				new FileOutputStream("bos.txt"));

		// 写数据
		bos.write("hello".getBytes());

		// 释放资源
		bos.close();
	}
}
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

/*
 * 注意:虽然我们有两种方式可以读取,但是,请注意,这两种方式针对同一个对象在一个代码中只能使用一个。
 */
public class BufferedInputStreamDemo {
	public static void main(String[] args) throws IOException {
		// BufferedInputStream(InputStream in)
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
				"bos.txt"));

		// 读取数据
		// int by = 0;
		// while ((by = bis.read()) != -1) {
		// System.out.print((char) by);
		// }
		// System.out.println("---------");

		byte[] bys = new byte[1024];
		int len = 0;
		while ((len = bis.read(bys)) != -1) {
			System.out.print(new String(bys, 0, len));
		}

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

 

字节缓冲流复制数据练习

四种方式比较复制效率

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * 需求:把e:\\哥有老婆.mp4复制到当前项目目录下的copy.mp4中
 * 
 * 字节流四种方式复制文件:
 * 基本字节流一次读写一个字节:	共耗时:117235毫秒
 * 基本字节流一次读写一个字节数组: 共耗时:156毫秒
 * 高效字节流一次读写一个字节: 共耗时:1141毫秒
 * 高效字节流一次读写一个字节数组: 共耗时:47毫秒
 */
public class CopyMp4Demo {
	public static void main(String[] args) throws IOException {
		long start = System.currentTimeMillis();
		// method1("e:\\哥有老婆.mp4", "copy1.mp4");
		// method2("e:\\哥有老婆.mp4", "copy2.mp4");
		// method3("e:\\哥有老婆.mp4", "copy3.mp4");
		method4("e:\\哥有老婆.mp4", "copy4.mp4");
		long end = System.currentTimeMillis();
		System.out.println("共耗时:" + (end - start) + "毫秒");
	}

	// 高效字节流一次读写一个字节数组:
	public static void method4(String srcString, String destString)
			throws IOException {
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
				srcString));
		BufferedOutputStream bos = new BufferedOutputStream(
				new FileOutputStream(destString));

		byte[] bys = new byte[1024];
		int len = 0;
		while ((len = bis.read(bys)) != -1) {
			bos.write(bys, 0, len);
		}

		bos.close();
		bis.close();
	}

	// 高效字节流一次读写一个字节:
	public static void method3(String srcString, String destString)
			throws IOException {
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
				srcString));
		BufferedOutputStream bos = new BufferedOutputStream(
				new FileOutputStream(destString));

		int by = 0;
		while ((by = bis.read()) != -1) {
			bos.write(by);

		}

		bos.close();
		bis.close();
	}

	// 基本字节流一次读写一个字节数组
	public static void method2(String srcString, String destString)
			throws IOException {
		FileInputStream fis = new FileInputStream(srcString);
		FileOutputStream fos = new FileOutputStream(destString);

		byte[] bys = new byte[1024];
		int len = 0;
		while ((len = fis.read(bys)) != -1) {
			fos.write(bys, 0, len);
		}

		fos.close();
		fis.close();
	}

	// 基本字节流一次读写一个字节
	public static void method1(String srcString, String destString)
			throws IOException {
		FileInputStream fis = new FileInputStream(srcString);
		FileOutputStream fos = new FileOutputStream(destString);

		int by = 0;
		while ((by = fis.read()) != -1) {
			fos.write(by);
		}

		fos.close();
		fis.close();
	}
}

五、转换流

转换流出现的原因及思想

由于字节流操作中文不是特别方便,所以,java就提供了转换流。

字符流=字节流+编码表。

 

编码表概述和常见的编码表

编码表

•    由字符及其对应的数值组成的一张表

常见编码表

•    ASCII/Unicode 字符集

•    ISO-8859-1

•    GB2312/GBK/GB18030

•    BIG5

•    UTF-8

 

import java.io.FileInputStream;
import java.io.IOException;

/*
 * 字节流读取中文可能出现的小问题:
 */
public class FileInputStreamDemo {
	public static void main(String[] args) throws IOException {
		// 创建字节输入流对象
		FileInputStream fis = new FileInputStream("a.txt");

		// 读取数据
		// int by = 0;
		// while ((by = fis.read()) != -1) {
		// System.out.print((char) by);
		// }

		byte[] bys = new byte[1024];
		int len = 0;
		while ((len = fis.read(bys)) != -1) {
			System.out.print(new String(bys, 0, len));
		}

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

 

字符串中的编码问题

编码

•    把看得懂的变成看不懂的

解码

•    把看不懂的变成看得懂的

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

/*
 * String(byte[] bytes, String charsetName):通过指定的字符集解码字节数组
 * byte[] getBytes(String charsetName):使用指定的字符集合把字符串编码为字节数组
 * 
 * 编码:把看得懂的变成看不懂的
 * String -- byte[]
 * 
 * 解码:把看不懂的变成看得懂的
 * byte[] -- String
 * 
 * 举例:谍战片(发电报,接电报)
 * 
 * 码表:小本子
 * 		字符	数值
 * 
 * 要发送一段文字:
 * 		今天晚上在老地方见
 * 
 * 		发送端:今 -- 数值 -- 二进制 -- 发出去
 * 		接收端:接收 -- 二进制 -- 十进制 -- 数值 -- 字符 -- 今
 * 
 * 		今天晚上在老地方见
 * 
 * 编码问题简单,只要编码解码的格式是一致的。
 */
public class StringDemo {
	public static void main(String[] args) throws UnsupportedEncodingException {
		String s = "你好";

		// String -- byte[]
		byte[] bys = s.getBytes(); // [-60, -29, -70, -61]
		// byte[] bys = s.getBytes("GBK");// [-60, -29, -70, -61]
		// byte[] bys = s.getBytes("UTF-8");// [-28, -67, -96, -27, -91, -67]
		System.out.println(Arrays.toString(bys));

		// byte[] -- String
		String ss = new String(bys); // 你好
		// String ss = new String(bys, "GBK"); // 你好
		// String ss = new String(bys, "UTF-8"); // ???
		System.out.println(ss);
	}
}

 

转换流概述

OutputStreamWriter字符输出流

•    public OutputStreamWriter(OutputStream out)

•    public OutputStreamWriter(OutputStream out,String charsetName)

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

/*
 * OutputStreamWriter(OutputStream out):根据默认编码把字节流的数据转换为字符流
 * OutputStreamWriter(OutputStream out,String charsetName):根据指定编码把字节流数据转换为字符流
 * 把字节流转换为字符流。
 * 字符流 = 字节流 +编码表。
 */
public class OutputStreamWriterDemo {
	public static void main(String[] args) throws IOException {
		// 创建对象
		// OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
		// "osw.txt")); // 默认GBK
		// OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
		// "osw.txt"), "GBK"); // 指定GBK
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
				"osw.txt"), "UTF-8"); // 指定UTF-8
		// 写数据
		osw.write("中国");

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

InputStreamReader字符输入流

•    public InputStreamReader(InputStream in)

•    public InputStreamReader(InputStream in,String charsetName)

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/*
 * InputStreamReader(InputStream is):用默认的编码读取数据
 * InputStreamReader(InputStream is,String charsetName):用指定的编码读取数据
 */
public class InputStreamReaderDemo {
	public static void main(String[] args) throws IOException {
		// 创建对象
		// InputStreamReader isr = new InputStreamReader(new FileInputStream(
		// "osw.txt"));

		// InputStreamReader isr = new InputStreamReader(new FileInputStream(
		// "osw.txt"), "GBK");

		InputStreamReader isr = new InputStreamReader(new FileInputStream(
				"osw.txt"), "UTF-8");

		// 读取数据
		// 一次读取一个字符
		int ch = 0;
		while ((ch = isr.read()) != -1) {
			System.out.print((char) ch);
		}

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

 

OutputStreamWriter写数据

OutputStreamWriter写数据方法

•    public void write(int c)

•    public void write(char[] cbuf)

•    public void write(char[] cbuf,int off,int len)

•    public void write(String str)

•    public void write(String str,int off,int len)

字符流操作要注意的问题

•    flush()的作用

•    flush()close()的区别

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

/*
 * OutputStreamWriter的方法:
 * public void write(int c):写一个字符
 * public void write(char[] cbuf):写一个字符数组
 * public void write(char[] cbuf,int off,int len):写一个字符数组的一部分
 * public void write(String str):写一个字符串
 * public void write(String str,int off,int len):写一个字符串的一部分
 * 
 * 面试题:close()和flush()的区别?
 * A:close()关闭流对象,但是先刷新一次缓冲区。关闭之后,流对象不可以继续再使用了。
 * B:flush()仅仅刷新缓冲区,刷新之后,流对象还可以继续使用。
 */
public class OutputStreamWriterDemo {
	public static void main(String[] args) throws IOException {
		// 创建对象
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
				"osw2.txt"));

		// 写数据
		// public void write(int c):写一个字符
		// osw.write('a');
		// osw.write(97);
		// 为什么数据没有进去呢?
		// 原因是:字符 = 2字节
		// 文件中数据存储的基本单位是字节。
		// void flush()

		// public void write(char[] cbuf):写一个字符数组
		// char[] chs = {'a','b','c','d','e'};
		// osw.write(chs);

		// public void write(char[] cbuf,int off,int len):写一个字符数组的一部分
		// osw.write(chs,1,3);

		// public void write(String str):写一个字符串
		// osw.write("我爱林青霞");

		// public void write(String str,int off,int len):写一个字符串的一部分
		osw.write("我爱林青霞", 2, 3);

		// 刷新缓冲区
		osw.flush();
		// osw.write("我爱林青霞", 2, 3);

		// 释放资源
		osw.close();
		// java.io.IOException: Stream closed
		// osw.write("我爱林青霞", 2, 3);
	}
}

 

InputStreamReader读数据

OutputStreamWriter读数据方法

•    public int read()

•    public int read(char[] cbuf)

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/*
 * InputStreamReader的方法:
 * int read():一次读取一个字符
 * int read(char[] chs):一次读取一个字符数组
 */
public class InputStreamReaderDemo {
	public static void main(String[] args) throws IOException {
		// 创建对象
		InputStreamReader isr = new InputStreamReader(new FileInputStream(
				"StringDemo.java"));

		// 一次读取一个字符
		// int ch = 0;
		// while ((ch = isr.read()) != -1) {
		// System.out.print((char) ch);
		// }

		// 一次读取一个字符数组
		char[] chs = new char[1024];
		int len = 0;
		while ((len = isr.read(chs)) != -1) {
			System.out.print(new String(chs, 0, len));
		}

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

 

字符流复制文本文件

把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/*
 * 需求:把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt中
 * 
 * 数据源:
 * 		a.txt -- 读取数据 -- 字符转换流 -- InputStreamReader
 * 目的地:
 * 		b.txt -- 写出数据 -- 字符转换流 -- OutputStreamWriter
 */
public class CopyFileDemo {
	public static void main(String[] args) throws IOException {
		// 封装数据源
		InputStreamReader isr = new InputStreamReader(new FileInputStream(
				"a.txt"));
		// 封装目的地
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
				"b.txt"));

		// 读写数据
		// 方式1
		// int ch = 0;
		// while ((ch = isr.read()) != -1) {
		// osw.write(ch);
		// }

		// 方式2
		char[] chs = new char[1024];
		int len = 0;
		while ((len = isr.read(chs)) != -1) {
			osw.write(chs, 0, len);
			// osw.flush();
		}

		// 释放资源
		osw.close();
		isr.close();
	}
}

 

转换流的简化写法

转换流的名字比较长,而我们常见的操作都是按照本地默认编码实现的,所以,为了简化我们的书写,转换流提供了对应的子类。

FileWriter

FileReader

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/*
 * 由于我们常见的操作都是使用本地默认编码,所以,不用指定编码。
 * 而转换流的名称有点长,所以,Java就提供了其子类供我们使用。
 * OutputStreamWriter = FileOutputStream + 编码表(GBK)
 * FileWriter = FileOutputStream + 编码表(GBK)
 * 
 * InputStreamReader = FileInputStream + 编码表(GBK)
 * FileReader = FileInputStream + 编码表(GBK)
 * 
 /*
 * 需求:把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt中
 * 
 * 数据源:
 * 		a.txt -- 读取数据 -- 字符转换流 -- InputStreamReader -- FileReader
 * 目的地:
 * 		b.txt -- 写出数据 -- 字符转换流 -- OutputStreamWriter -- FileWriter
 */
public class CopyFileDemo2 {
	public static void main(String[] args) throws IOException {
		// 封装数据源
		FileReader fr = new FileReader("a.txt");
		// 封装目的地
		FileWriter fw = new FileWriter("b.txt");

		// 一次一个字符
		// int ch = 0;
		// while ((ch = fr.read()) != -1) {
		// fw.write(ch);
		// }

		// 一次一个字符数组
		char[] chs = new char[1024];
		int len = 0;
		while ((len = fr.read(chs)) != -1) {
			fw.write(chs, 0, len);
			fw.flush();
		}

		// 释放资源
		fw.close();
		fr.close();
	}
}

 

FileWriterFileReader

FileWriter写数据

FileReader读取数据

FileWriterFileReader实现文本文件的复制

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/*
 * 需求:把c:\\a.txt内容复制到d:\\b.txt中
 * 
 * 数据源:
 * 		c:\\a.txt -- FileReader
 * 目的地:
 * 		d:\\b.txt -- FileWriter
 */
public class CopyFileDemo3 {
	public static void main(String[] args) throws IOException {
		// 封装数据源
		FileReader fr = new FileReader("c:\\a.txt");
		// 封装目的地
		FileWriter fw = new FileWriter("d:\\b.txt");

		// 读写数据
		// int ch = 0;
		int ch;
		while ((ch = fr.read()) != -1) {
			fw.write(ch);
		}
		
		//释放资源
		fw.close();
		fr.close();
	}
}

字符缓冲流

BufferedWriter基本用法

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

/*
 * 字符流为了高效读写,也提供了对应的字符缓冲流。
 * BufferedWriter:字符缓冲输出流
 * BufferedReader:字符缓冲输入流
 * 
 * BufferedWriter:字符缓冲输出流
 * 将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。 
 * 可以指定缓冲区的大小,或者接受默认的大小。在大多数情况下,默认值就足够大了。 
 */
public class BufferedWriterDemo {
	public static void main(String[] args) throws IOException {
		// BufferedWriter(Writer out)
		// BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
		// new FileOutputStream("bw.txt")));

		BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt"));

		bw.write("hello");
		bw.write("world");
		bw.write("java");
		bw.flush();

		bw.close();
	}
}

BufferedReader基本用法

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/*
 * BufferedReader
 * 从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。 
 * 可以指定缓冲区的大小,或者可使用默认的大小。大多数情况下,默认值就足够大了。 
 * 
 * BufferedReader(Reader in)
 */
public class BufferedReaderDemo {
	public static void main(String[] args) throws IOException {
		// 创建字符缓冲输入流对象
		BufferedReader br = new BufferedReader(new FileReader("bw.txt"));

		// 方式1
		// int ch = 0;
		// while ((ch = br.read()) != -1) {
		// System.out.print((char) ch);
		// }

		// 方式2
		char[] chs = new char[1024];
		int len = 0;
		while ((len = br.read(chs)) != -1) {
			System.out.print(new String(chs, 0, len));
		}

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

字符缓冲流复制文本文件

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/*
 * 需求:把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt中
 * 
 * 数据源:
 * 		a.txt -- 读取数据 -- 字符转换流 -- InputStreamReader -- FileReader -- BufferedReader
 * 目的地:
 * 		b.txt -- 写出数据 -- 字符转换流 -- OutputStreamWriter -- FileWriter -- BufferedWriter
 */
public class CopyFileDemo {
	public static void main(String[] args) throws IOException {
		// 封装数据源
		BufferedReader br = new BufferedReader(new FileReader("a.txt"));
		// 封装目的地
		BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt"));

		// 两种方式其中的一种一次读写一个字符数组
		char[] chs = new char[1024];
		int len = 0;
		while ((len = br.read(chs)) != -1) {
			bw.write(chs, 0, len);
			bw.flush();
		}

		// 释放资源
		bw.close();
		br.close();
	}
}

特殊功能

•    BufferedWriter

•   void newLine()

•    BufferedReader

•   String readLine()

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/*
 * 字符缓冲流的特殊方法:
 * BufferedWriter:
 * 		public void newLine():根据系统来决定换行符
 * BufferedReader:
 * 		public String readLine():一次读取一行数据
 * 		包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null
 */
public class BufferedDemo {
	public static void main(String[] args) throws IOException {
		// write();
		read();
	}

	private static void read() throws IOException {
		// 创建字符缓冲输入流对象
		BufferedReader br = new BufferedReader(new FileReader("bw2.txt"));

		// public String readLine():一次读取一行数据
		// String line = br.readLine();
		// System.out.println(line);
		// line = br.readLine();
		// System.out.println(line);

		// 最终版代码
		String line = null;
		while ((line = br.readLine()) != null) {
			System.out.println(line);
		}
		
		//释放资源
		br.close();
	}

	private static void write() throws IOException {
		// 创建字符缓冲输出流对象
		BufferedWriter bw = new BufferedWriter(new FileWriter("bw2.txt"));
		for (int x = 0; x < 10; x++) {
			bw.write("hello" + x);
			// bw.write("\r\n");
			bw.newLine();
			bw.flush();
		}
		bw.close();
	}

}

字符缓冲流特殊功能复制文本文件

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/*
 * 需求:把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt中
 * 
 * 数据源:
 * 		a.txt -- 读取数据 -- 字符转换流 -- InputStreamReader -- FileReader -- BufferedReader
 * 目的地:
 * 		b.txt -- 写出数据 -- 字符转换流 -- OutputStreamWriter -- FileWriter -- BufferedWriter
 */
public class CopyFileDemo2 {
	public static void main(String[] args) throws IOException {
		// 封装数据源
		BufferedReader br = new BufferedReader(new FileReader("a.txt"));
		// 封装目的地
		BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt"));

		// 读写数据
		String line = null;
		while ((line = br.readLine()) != null) {
			bw.write(line);
			bw.newLine();
			bw.flush();
		}

		// 释放资源
		bw.close();
		br.close();
	}
}

 

IO流小结

|--字节流

|--字节输入流

  • InputStream

  • int read():一次读取一个字节

  • int read(byte[] bys):一次读取一个字节数组

  • |--FileInputStream

  • |--BufferedInputStream


|--字节输出流

  • OutputStream

  • void write(int by):一次写一个字节

  • void write(byte[] bys,int index,int len):一次写一个字节数组的一部分

  • |--FileOutputStream

  • |--BufferedOutputStream


|--字符流
|--字符输入流

  • Reader

  • int read():一次读取一个字符

  • int read(char[] chs):一次读取一个字符数组

  • |--InputStreamReader

  • |--FileReader

  • |--BufferedReader

  • String readLine():一次读取一个字符串


|--字符输出流

  • Writer

  • void write(int ch):一次写一个字符

  • void write(char[] chs,int index,int len):一次写一个字符数组的一部分

  • |--OutputStreamWriter

  • |--FileWriter

  • |--BufferedWriter

  • void newLine():写一个换行符

  • void write(String line):一次写一个字符串

 

IO流案例

l  复制文本文件

l  复制图片

l  ArrayList集合中的字符串数据存储到文本文件

l  从文本文件中读取数据(每一行为一个字符串数据)到集合中,并遍历集合

l  复制单极文件夹

l  复制单极文件夹中指定文件并修改文件名称

l  复制多极文件夹

l  键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件

 

IO流练习

l  已知s.txt文件中有这样的一个字符串:“hcexfgijkamdnoqrzstuvwybpl

 请编写程序读取数据内容,把数据排序后写入ss.txt中。

l  Reader模拟BufferedReaderreadLine()功能

l  自定义类模拟LineNumberReader的特有功能

•   获取每次读取数据的行号

l  登录注册IO

 


七、其他流

操作基本数据类型的流

操作基本数据类型

•    DataInputStream

•    DataOutputStream

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * 可以读写基本数据类型的数据
 * 数据输入流:DataInputStream
 * 			DataInputStream(InputStream in)
 * 数据输出流:DataOutputStream
 * 			DataOutputStream(OutputStream out) 
 */
public class DataStreamDemo {
	public static void main(String[] args) throws IOException {
		// 写
		// write();

		// 读
		read();
	}

	private static void read() throws IOException {
		// DataInputStream(InputStream in)
		// 创建数据输入流对象
		DataInputStream dis = new DataInputStream(
				new FileInputStream("dos.txt"));

		// 读数据
		byte b = dis.readByte();
		short s = dis.readShort();
		int i = dis.readInt();
		long l = dis.readLong();
		float f = dis.readFloat();
		double d = dis.readDouble();
		char c = dis.readChar();
		boolean bb = dis.readBoolean();

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

		System.out.println(b);
		System.out.println(s);
		System.out.println(i);
		System.out.println(l);
		System.out.println(f);
		System.out.println(d);
		System.out.println(c);
		System.out.println(bb);
	}

	private static void write() throws IOException {
		// DataOutputStream(OutputStream out)
		// 创建数据输出流对象
		DataOutputStream dos = new DataOutputStream(new FileOutputStream(
				"dos.txt"));

		// 写数据了
		dos.writeByte(10);
		dos.writeShort(100);
		dos.writeInt(1000);
		dos.writeLong(10000);
		dos.writeFloat(12.34F);
		dos.writeDouble(12.56);
		dos.writeChar('a');
		dos.writeBoolean(true);

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

 

内存操作流

操作字节数组

•    ByteArrayInputStream

•    ByteArrayOutputStream

操作字符数组

•    CharArrayReader

•    CharArrayWrite

操作字符串

•    StringReader

•    StringWriter

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/*
 * 内存操作流:用于处理临时存储信息的,程序结束,数据就从内存中消失。
 * 字节数组:
 * 		ByteArrayInputStream
 * 		ByteArrayOutputStream
 * 字符数组:
 * 		CharArrayReader
 * 		CharArrayWriter
 * 字符串:
 * 		StringReader
 * 		StringWriter
 */
public class ByteArrayStreamDemo {
	public static void main(String[] args) throws IOException {
		// 写数据
		// ByteArrayOutputStream()
		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		// 写数据
		for (int x = 0; x < 10; x++) {
			baos.write(("hello" + x).getBytes());
		}

		// 释放资源
		// 通过查看源码我们知道这里什么都没做,所以根本需要close()
		// baos.close();

		// public byte[] toByteArray()
		byte[] bys = baos.toByteArray();

		// 读数据
		// ByteArrayInputStream(byte[] buf)
		ByteArrayInputStream bais = new ByteArrayInputStream(bys);

		int by = 0;
		while ((by = bais.read()) != -1) {
			System.out.print((char) by);
		}

		// bais.close();
	}
}

 

打印流

打印流概述

•    字节流打印流

•    字符打印流

打印流特点

•    只能操作目的地,不能操作数据。

•    可以操作任意类型的数据。

•    如果启动了自动刷新,能够自动刷新。

•    可以操作文件的流

import java.io.IOException;
import java.io.PrintWriter;

/*
 * 打印流
 * 字节流打印流	PrintStream
 * 字符打印流	PrintWriter
 * 
 * 打印流的特点:
 * 		A:只有写数据的,没有读取数据。只能操作目的地,不能操作数据源。
 * 		B:可以操作任意类型的数据。
 * 		C:如果启动了自动刷新,能够自动刷新。
 * 		D:该流是可以直接操作文本文件的。
 * 			哪些流对象是可以直接操作文本文件的呢?
 * 			FileInputStream
 * 			FileOutputStream
 * 			FileReader
 * 			FileWriter
 * 			PrintStream
 * 			PrintWriter
 * 			看API,查流对象的构造方法,如果同时有File类型和String类型的参数,一般来说就是可以直接操作文件的。
 * 
 * 			流:
 * 				基本流:就是能够直接读写文件的
 * 				高级流:在基本流基础上提供了一些其他的功能
 */
public class PrintWriterDemo {
	public static void main(String[] args) throws IOException {
		// 作为Writer的子类使用
		PrintWriter pw = new PrintWriter("pw.txt");

		pw.write("hello");
		pw.write("world");
		pw.write("java");
		
		pw.close();
	}
}
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/*
 * 1:可以操作任意类型的数据。
 * 		print()
 * 		println()
 * 2:启动自动刷新
 * 		PrintWriter pw = new PrintWriter(new FileWriter("pw2.txt"), true);
 * 		还是应该调用println()的方法才可以
 * 		这个时候不仅仅自动刷新了,还实现了数据的换行。
 * 
 * 		println()
 *		其实等价于于:
 *		bw.write();
 *		bw.newLine();		
 *		bw.flush();
 */
public class PrintWriterDemo2 {
	public static void main(String[] args) throws IOException {
		// 创建打印流对象
		// PrintWriter pw = new PrintWriter("pw2.txt");
		PrintWriter pw = new PrintWriter(new FileWriter("pw2.txt"), true);

		// write()是搞不定的,怎么办呢?
		// 我们就应该看看它的新方法
		// pw.print(true);
		// pw.print(100);
		// pw.print("hello");

		pw.println("hello");
		pw.println(true);
		pw.println(100);

		pw.close();
	}
}

l 打印流复制文本文件

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/*
 * 需求:DataStreamDemo.java复制到Copy.java中
 * 数据源:
 * 		DataStreamDemo.java -- 读取数据 -- FileReader -- BufferedReader
 * 目的地:
 * 		Copy.java -- 写出数据 -- FileWriter -- BufferedWriter -- PrintWriter
 */
public class CopyFileDemo {
	public static void main(String[] args) throws IOException {
		// 以前的版本
		// 封装数据源
		// BufferedReader br = new BufferedReader(new FileReader(
		// "DataStreamDemo.java"));
		// // 封装目的地
		// BufferedWriter bw = new BufferedWriter(new FileWriter("Copy.java"));
		//
		// String line = null;
		// while ((line = br.readLine()) != null) {
		// bw.write(line);
		// bw.newLine();
		// bw.flush();
		// }
		//
		// bw.close();
		// br.close();

		// 打印流的改进版
		// 封装数据源
		BufferedReader br = new BufferedReader(new FileReader(
				"DataStreamDemo.java"));
		// 封装目的地
		PrintWriter pw = new PrintWriter(new FileWriter("Copy.java"), true);
		
		String line = null;
		while((line=br.readLine())!=null){
			pw.println(line);
		}
		
		pw.close();
		br.close();
	}
}

 

标准输入输出流

System类中的字段:inout

它们各代表了系统标准的输入和输出设备。

默认输入设备是键盘,输出设备是显示器。

System.in的类型是InputStream.

System.out的类型是PrintStreamOutputStream的子类FilterOutputStream 的子类.

import java.io.PrintStream;

/*
 * 标准输入输出流
 * System类中的两个成员变量:
 *		public static final InputStream in “标准”输入流。
 * 		public static final PrintStream out “标准”输出流。
 * 
 * 		InputStream is = System.in;
 * 		PrintStream ps = System.out;
 */
public class SystemOutDemo {
	public static void main(String[] args) {
		// 有这里的讲解我们就知道了,这个输出语句其本质是IO流操作,把数据输出到控制台。
		System.out.println("helloworld");

		// 获取标准输出流对象
		PrintStream ps = System.out;
		ps.println("helloworld");
		
		ps.println();
		// ps.print();//这个方法不存在
		
		// System.out.println();
		// System.out.print();
	}
}
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
/*
 * 转换流的应用。
 */
public class SystemOutDemo2 {
	public static void main(String[] args) throws IOException {
		// 获取标准输入流
		// // PrintStream ps = System.out;
		// // OutputStream os = ps;
		// OutputStream os = System.out; // 多态
		// // 我能不能按照刚才使用标准输入流的方式一样把数据输出到控制台呢?
		// OutputStreamWriter osw = new OutputStreamWriter(os);
		// BufferedWriter bw = new BufferedWriter(osw);
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
				System.out));

		bw.write("hello");
		bw.newLine();
		// bw.flush();
		bw.write("world");
		bw.newLine();
		// bw.flush();
		bw.write("java");
		bw.newLine();
		bw.flush();
		
		bw.close();
	}
}

 

随机访问流

RandomAccessFile概述

•    RandomAccessFile类不属于流,是Object类的子类。但它融合了InputStreamOutputStream的功能。支持对随机访问文件的读取和写入。

案例演示

import java.io.IOException;
import java.io.RandomAccessFile;

/*
 * 随机访问流:
 * 		RandomAccessFile类不属于流,是Object类的子类。
 * 		但它融合了InputStream和OutputStream的功能。
 * 		支持对文件的随机访问读取和写入。
 * 
 * public RandomAccessFile(String name,String mode):第一个参数是文件路径,第二个参数是操作文件的模式。
 * 		模式有四种,我们最常用的一种叫"rw",这种方式表示我既可以写数据,也可以读取数据 
 */
public class RandomAccessFileDemo {
	public static void main(String[] args) throws IOException {
		// write();
		read();
	}

	private static void read() throws IOException {
		// 创建随机访问流对象
		RandomAccessFile raf = new RandomAccessFile("raf.txt", "rw");

		int i = raf.readInt();
		System.out.println(i);
		// 该文件指针可以通过 getFilePointer方法读取,并通过 seek 方法设置。
		System.out.println("当前文件的指针位置是:" + raf.getFilePointer());

		char ch = raf.readChar();
		System.out.println(ch);
		System.out.println("当前文件的指针位置是:" + raf.getFilePointer());

		String s = raf.readUTF();
		System.out.println(s);
		System.out.println("当前文件的指针位置是:" + raf.getFilePointer());

		// 我不想重头开始了,我就要读取a,怎么办呢?
		raf.seek(4);
		ch = raf.readChar();
		System.out.println(ch);
	}

	private static void write() throws IOException {
		// 创建随机访问流对象
		RandomAccessFile raf = new RandomAccessFile("raf.txt", "rw");

		// 怎么玩呢?
		raf.writeInt(100);
		raf.writeChar('a');
		raf.writeUTF("中国");

		raf.close();
	}
}

 

合并流

SequenceInputStream概述

•    SequenceInputStream类可以将多个输入流串流在一起,合并为一个输入流,因此,该流也被称为合并流。

SequenceInputStream的构造方法

•    SequenceInputStream(InputStreams1, InputStream s2) 

•    SequenceInputStream(Enumeration<?extends InputStream> e)

•    把多个文件的内容写入到一个文本文件

import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;

/*
 * 以前的操作:
 * a.txt -- b.txt
 * c.txt -- d.txt
 * 
 * 现在想要:
 * a.txt+b.txt -- c.txt
 */
public class SequenceInputStreamDemo {
	public static void main(String[] args) throws IOException {
		// SequenceInputStream(InputStream s1, InputStream s2)
		// 需求:把ByteArrayStreamDemo.java和DataStreamDemo.java的内容复制到Copy.java中
		InputStream s1 = new FileInputStream("ByteArrayStreamDemo.java");
		InputStream s2 = new FileInputStream("DataStreamDemo.java");
		SequenceInputStream sis = new SequenceInputStream(s1, s2);
		BufferedOutputStream bos = new BufferedOutputStream(
				new FileOutputStream("Copy.java"));

		// 如何写读写呢,其实很简单,你就按照以前怎么读写,现在还是怎么读写
		byte[] bys = new byte[1024];
		int len = 0;
		while ((len = sis.read(bys)) != -1) {
			bos.write(bys, 0, len);
		}

		bos.close();
		sis.close();
	}
}
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;

/*
 * 以前的操作:
 * a.txt -- b.txt
 * c.txt -- d.txt
 * e.txt -- f.txt
 * 
 * 现在想要:
 * a.txt+b.txt+c.txt -- d.txt
 */
public class SequenceInputStreamDemo2 {
	public static void main(String[] args) throws IOException {
		// 需求:把下面的三个文件的内容复制到Copy.java中
		// ByteArrayStreamDemo.java,CopyFileDemo.java,DataStreamDemo.java

		// SequenceInputStream(Enumeration e)
		// 通过简单的回顾我们知道了Enumeration是Vector中的一个方法的返回值类型。
		// Enumeration<E> elements()
		Vector<InputStream> v = new Vector<InputStream>();
		InputStream s1 = new FileInputStream("ByteArrayStreamDemo.java");
		InputStream s2 = new FileInputStream("CopyFileDemo.java");
		InputStream s3 = new FileInputStream("DataStreamDemo.java");
		v.add(s1);
		v.add(s2);
		v.add(s3);
		Enumeration<InputStream> en = v.elements();
		SequenceInputStream sis = new SequenceInputStream(en);
		BufferedOutputStream bos = new BufferedOutputStream(
				new FileOutputStream("Copy.java"));

		// 如何写读写呢,其实很简单,你就按照以前怎么读写,现在还是怎么读写
		byte[] bys = new byte[1024];
		int len = 0;
		while ((len = sis.read(bys)) != -1) {
			bos.write(bys, 0, len);
		}

		bos.close();
		sis.close();
	}
}

 

序列化流

序列化流

•    ObjectOutputStream

反序列化流

•    ObjectInputStream

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/*
 * 序列化流:把对象按照流一样的方式存入文本文件或者在网络中传输。对象 -- 流数据(ObjectOutputStream)
 * 反序列化流:把文本文件中的流对象数据或者网络中的流对象数据还原成对象。流数据 -- 对象(ObjectInputStream)
 */
public class ObjectStreamDemo {
	public static void main(String[] args) throws IOException,
			ClassNotFoundException {
		// 由于我们要对对象进行序列化,所以我们先自定义一个类
		// 序列化数据其实就是把对象写到文本文件
		// write();

		read();
	}

	private static void read() throws IOException, ClassNotFoundException {
		// 创建反序列化对象
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
				"oos.txt"));

		// 还原对象
		Object obj = ois.readObject();

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

		// 输出对象
		System.out.println(obj);
	}

	private static void write() throws IOException {
		// 创建序列化流对象
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
				"oos.txt"));

		// 创建对象
		Person p = new Person("林青霞", 27);

		// public final void writeObject(Object obj)
		oos.writeObject(p);

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

序列化操作问题

•    为什么要实现序列化?

•    如何实现序列化?

•    序列化数据后,再次修改类文件,读取数据会出问题,如何解决呢?

•    使用transient关键字声明不需要序列化的成员变量

import java.io.Serializable;

/*
 * NotSerializableException:未序列化异常
 * 
 * 类通过实现 java.io.Serializable 接口以启用其序列化功能。未实现此接口的类将无法使其任何状态序列化或反序列化。
 * 该接口居然没有任何方法,类似于这种没有方法的接口被称为标记接口。
 * 
 * java.io.InvalidClassException: 
 * cn.itcast_07.Person; local class incompatible: 
 * stream classdesc serialVersionUID = -2071565876962058344, 
 * local class serialVersionUID = -8345153069362641443
 * 
 * 为什么会有问题呢?
 * 		Person类实现了序列化接口,那么它本身也应该有一个标记值。
 * 		这个标记值假设是100。
 * 		开始的时候:
 * 		Person.class -- id=100
 * 		wirte数据: oos.txt -- id=100
 * 		read数据: oos.txt -- id=100	
 * 
 * 		现在:
 * 		Person.class -- id=200
 * 		wirte数据: oos.txt -- id=100
 * 		read数据: oos.txt -- id=100
 * 我们在实际开发中,可能还需要使用以前写过的数据,不能重新写入。怎么办呢?
 * 回想一下原因是因为它们的id值不匹配。
 * 每次修改java文件的内容的时候,class文件的id值都会发生改变。
 * 而读取文件的时候,会和class文件中的id值进行匹配。所以,就会出问题。
 * 但是呢,如果我有办法,让这个id值在java文件中是一个固定的值,这样,你修改文件的时候,这个id值还会发生改变吗?
 * 不会。现在的关键是我如何能够知道这个id值如何表示的呢?
 * 不用担心,你不用记住,也没关系,点击鼠标即可。
 * 你难道没有看到黄色警告线吗?
 * 
 * 我们要知道的是:
 * 		看到类实现了序列化接口的时候,要想解决黄色警告线问题,就可以自动产生一个序列化id值。
 * 		而且产生这个值以后,我们对类进行任何改动,它读取以前的数据是没有问题的。
 * 
 * 注意:
 * 		我一个类中可能有很多的成员变量,有些我不想进行序列化。请问该怎么办呢?
 * 		使用transient关键字声明不需要序列化的成员变量
 */
public class Person implements Serializable {
	private static final long serialVersionUID = -2071565876962058344L;

	private String name;

	// private int age;

	private transient int age;

	// int age;

	public Person() {
		super();
	}

	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}
}

 

Properties集合

Properties概述

Properties作为Map集合的使用

import java.util.Properties;
import java.util.Set;

/*
 * Properties:属性集合类。是一个可以和IO流相结合使用的集合类。
 * Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。 
 * 
 * 是Hashtable的子类,说明是一个Map集合。
 */
public class PropertiesDemo {
	public static void main(String[] args) {
		// 作为Map集合的使用
		// 下面这种用法是错误的,一定要看API,如果没有<>,就说明该类不是一个泛型类,在使用的时候就不能加泛型
		// Properties<String, String> prop = new Properties<String, String>();

		Properties prop = new Properties();

		// 添加元素
		prop.put("it002", "hello");
		prop.put("it001", "world");
		prop.put("it003", "java");

		// System.out.println("prop:" + prop);

		// 遍历集合
		Set<Object> set = prop.keySet();
		for (Object key : set) {
			Object value = prop.get(key);
			System.out.println(key + "---" + value);
		}
	}
}

Properties的特殊功能

•    public ObjectsetProperty(String key,String value)

•    public StringgetProperty(String key)

•    public Set<String>stringPropertyNames()

import java.util.Properties;
import java.util.Set;

/*
 * 特殊功能:
 * public Object setProperty(String key,String value):添加元素
 * public String getProperty(String key):获取元素
 * public Set<String> stringPropertyNames():获取所有的键的集合
 */
public class PropertiesDemo2 {
	public static void main(String[] args) {
		// 创建集合对象
		Properties prop = new Properties();

		// 添加元素
		prop.setProperty("张三", "30");
		prop.setProperty("李四", "40");
		prop.setProperty("王五", "50");

		// public Set<String> stringPropertyNames():获取所有的键的集合
		Set<String> set = prop.stringPropertyNames();
		for (String key : set) {
			String value = prop.getProperty(key);
			System.out.println(key + "---" + value);
		}
	}
}

/*
 * class Hashtalbe<K,V> { public V put(K key,V value) { ... } }
 * 
 * class Properties extends Hashtable { public V setProperty(String key,String
 * value) { return put(key,value); } }
 */

PropertiesIO流的结合使用

  • 把键值对形式的文本文件内容加载到集合中

  • public void load(Reader reader)

  • public void load(InputStream inStream)

  • 把集合中的数据存储到文本文件中

  • public void store(Writer writer,String comments)

  • public void store(OutputStream out,String comments)

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Properties;

/*
 * 这里的集合必须是Properties集合:
 * public void load(Reader reader):把文件中的数据读取到集合中
 * public void store(Writer writer,String comments):把集合中的数据存储到文件
 * 
 * 单机版游戏:
 * 		进度保存和加载。
 * 		三国群英传,三国志,仙剑奇侠传...
 * 
 * 		吕布=1
 * 		方天画戟=1
 */
public class PropertiesDemo3 {
	public static void main(String[] args) throws IOException {
		// myLoad();

		myStore();
	}

	private static void myStore() throws IOException {
		// 创建集合对象
		Properties prop = new Properties();

		prop.setProperty("林青霞", "27");
		prop.setProperty("武鑫", "30");
		prop.setProperty("刘晓曲", "18");
		
		//public void store(Writer writer,String comments):把集合中的数据存储到文件
		Writer w = new FileWriter("name.txt");
		prop.store(w, "helloworld");
		w.close();
	}

	private static void myLoad() throws IOException {
		Properties prop = new Properties();

		// public void load(Reader reader):把文件中的数据读取到集合中
		// 注意:这个文件的数据必须是键值对形式
		Reader r = new FileReader("prop.txt");
		prop.load(r);
		r.close();

		System.out.println("prop:" + prop);
	}
}

|  案例1

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Properties;
import java.util.Set;

/*
 * 我有一个文本文件(user.txt),我知道数据是键值对形式的,但是不知道内容是什么。
 * 请写一个程序判断是否有“lisi”这样的键存在,如果有就改变其实为”100”
 * 
 * 分析:
 * 		A:把文件中的数据加载到集合中
 * 		B:遍历集合,获取得到每一个键
 * 		C:判断键是否有为"lisi"的,如果有就修改其值为"100"
 * 		D:把集合中的数据重新存储到文件中
 */
public class PropertiesTest {
	public static void main(String[] args) throws IOException {
		// 把文件中的数据加载到集合中
		Properties prop = new Properties();
		Reader r = new FileReader("user.txt");
		prop.load(r);
		r.close();

		// 遍历集合,获取得到每一个键
		Set<String> set = prop.stringPropertyNames();
		for (String key : set) {
			// 判断键是否有为"lisi"的,如果有就修改其值为"100"
			if ("lisi".equals(key)) {
				prop.setProperty(key, "100");
				break;
			}
		}

		// 把集合中的数据重新存储到文件中
		Writer w = new FileWriter("user.txt");
		prop.store(w, null);
		w.close();
	}
}

|  案例2

import java.util.Scanner;

/**
 * 这是猜数字小游戏
 */
public class GuessNumber {
	private GuessNumber() {
	}

	public static void start() {
		// 产生一个随机数
		int number = (int) (Math.random() * 100) + 1;

		// 定义一个统计变量
		int count = 0;

		while (true) {
			// 键盘录入一个数据
			Scanner sc = new Scanner(System.in);
			System.out.println("请输入数据(1-100):");
			int guessNumber = sc.nextInt();

			count++;

			// 判断
			if (guessNumber > number) {
				System.out.println("你猜的数据" + guessNumber + "大了");
			} else if (guessNumber < number) {
				System.out.println("你猜的数据" + guessNumber + "小了");
			} else {
				System.out.println("恭喜你," + count + "次就猜中了");
				break;
			}
		}
	}
}
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Properties;

/*
 * 我有一个猜数字小游戏的程序,请写一个程序实现在测试类中只能用5次,超过5次提示:游戏试玩已结束,请付费。
 */
public class PropertiesTest2 {
	public static void main(String[] args) throws IOException {
		// 读取某个地方的数据,如果次数不大于5,可以继续玩。否则就提示"游戏试玩已结束,请付费。"
		// 创建一个文件
		// File file = new File("count.txt");
		// if (!file.exists()) {
		// file.createNewFile();
		// }

		// 把数据加载到集合中
		Properties prop = new Properties();
		Reader r = new FileReader("count.txt");
		prop.load(r);
		r.close();

		// 我自己的程序,我当然知道里面的键是谁
		String value = prop.getProperty("count");
		int number = Integer.parseInt(value);

		if (number > 5) {
			System.out.println("游戏试玩已结束,请付费。");
			System.exit(0);
		} else {
			number++;
			prop.setProperty("count", String.valueOf(number));
			Writer w = new FileWriter("count.txt");
			prop.store(w, null);
			w.close();

			GuessNumber.start();
		}
	}
}

 

NIO包下的IO

NIO其实就是新IO的意思。

•    JDK4出现NIO。新IO和传统的IO有相同的目的,都是用于进行输入输出的,但新IO使用了不同的方式来处理输入输出,采用内存映射文件的方式,将文件或者文件的一段区域映射到内存中,就可以像访问内存一样的来访问文件了,这种方式效率比旧IO要高很多,但是目前好多地方我们看到的还是旧IO的引用,所以我们仍以旧IO为主,知道NIO即可。

•    JDK7IO改进(写一个案例)

•   Path

•   Paths

•   Files

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;

/*
 * nio包在JDK4出现,提供了IO流的操作效率。但是目前还不是大范围的使用。
 * 有空的话了解下,有问题再问我。
 * 
 * JDK7的之后的nio:
 * Path:路径
 * Paths:有一个静态方法返回一个路径
 * 		public static Path get(URI uri)
 * Files:提供了静态方法供我们使用
 * 		public static long copy(Path source,OutputStream out):复制文件
 * 		public static Path write(Path path,Iterable<? extends CharSequence> lines,Charset cs,OpenOption... options)
 */
public class NIODemo {
	public static void main(String[] args) throws IOException {
		// public static long copy(Path source,OutputStream out)
		// Files.copy(Paths.get("ByteArrayStreamDemo.java"), new
		// FileOutputStream(
		// "Copy.java"));

		ArrayList<String> array = new ArrayList<String>();
		array.add("hello");
		array.add("world");
		array.add("java");
		Files.write(Paths.get("array.txt"), array, Charset.forName("GBK"));
	}
}
把.h文件里面的代码放出来一下,CPP就不放了,自己下载哦。 #ifndef Download_h__ #define Download_h__ #include <wininet.h> #pragma comment(lib,"wininet.lib") class CHttpGet { public: //测试网络是否连接成功。 BOOL TestNetworkIsConnected(); //动态获取URL的文件名。 //LPCTSTR szURL URL地址 //LPSTR pFileName 文件名缓冲区,获取到文件名后,会文件名放入到该buffer //DWORD dwBufferOfLenght pFileName缓冲区大小。 //pResult 是否成功。 BOOL HttpGetFileName(LPCTSTR szURL,LPSTR pFileName,DWORD dwBufferOfLenght,BOOL * pResult); //将文件下载到缓冲区,而不是保存到文件 //szURL URL地址 //szBuffer 缓冲区。 //dwSize 缓冲区大小 //lpdwSizeOfRet实际下载到的数据大小。 DWORD URLDownloadToBuffer(LPCTSTR szURL,LPBYTE szBuffer,DWORD dwSize,DWORD *lpdwSizeOfRet); //下载文件 //szURL URL地址 //szFileSavePath 文件完整保存路径 // CheckFileTypeIsPe 是否需要检测文件是不是PE文件。 DWORD URLDownloadToFile(LPCTSTR szURL,LPCTSTR szFileSavePath,BOOL CheckFileTypeIsPe); }; #endif // Download_h__ CPP部分代码,详细自己下载。 完全原创。代码注释详细。 使用及其方便... DWORD CHttpGet::URLDownloadToFile(LPCTSTR szURL,LPCTSTR szFileSavePath,BOOL CheckFileTypeIsPe) { if(!CheckUrl(szURL)) return FALSE; HINTERNET hInternetOpen = InternetOpen( "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)", INTERNET_OPEN_TYPE_PRECONFIG,/*返回注册表中代理或直接的配置*/ NULL,//不使用代理 所以不要设置用户名 NULL,//不使用代理 所以不用设置密码 NULL ); if(hInternetOpen == NULL) return FALSE; HINTERNET hInternetUrl = InternetOpenUrl( hInternetOpen, szURL, "Accept: */*",//支持左所有文件 -1, INTERNET_FLAG_RELOAD|INTERNET_FLAG_PRAGMA_NOCACHE,/*不要从缓冲里面获取数据 */ NULL); if(hInternetUrl == NULL) return FALSE; CHAR szStatus[1024]={NULL}; DWORD dwBufferLen = sizeof(szStatus); //查询状态 HttpQueryInfo(hInternetUrl,HTTP_QUERY_STATUS_CODE,szStatus,&dwBufferLen;,NULL); //4xx(请求错误) DWORD dwCode = atoi(szStatus); if( dwCode > 400 && dwCode < 500) return FALSE; //5xx(服务器错误) if( dwCode >500 && dwCode < 600) return FALSE; //开始下载文件 HANDLE hFile = CreateFile(szFileSavePath,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); if(hFile == INVALID_HANDLE_VALUE) { InternetCloseHandle(hInternetUrl); InternetCloseHandle(hInternetOpen); return FALSE; } CHAR szReadBuffer[4096]={NULL}; DWORD dwInternetReadOfByte = 0; DWORD dwWriteByte = 0; BOOL bFirst = FALSE; BOOL bResult = TRUE; do { BOOL bRet = InternetReadFile(hInternetUrl,szReadBuffer,sizeof(szReadBuffer),&dwInternetReadOfByte;); //说明文件传送完了。 if(bRet == TRUE && dwInternetReadOfByte == 0) break; if(bFirst == FALSE && CheckFileTypeIsPe==TRUE) { bFirst = TRUE; if(((PIMAGE_DOS_HEADER)szReadBuffer)->e_magic!= IMAGE_DOS_SIGNATURE) { bResult = FALSE; break; } } WriteFile(hFile,szReadBuffer,dwInternetReadOfByte,&dwWriteByte;,NULL); } while (TRUE); CloseHandle(hFile); InternetCloseHandle(hInternetUrl); InternetCloseHandle(hInternetOpen); return bResult; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值