IO流—缓冲流与Propeties类
1.缓冲流
Java中提高了一套缓冲流,它的存在,可提高IO流的读写速度
缓冲流,根据流的分类分类字节缓冲流与字符缓冲流。
字节缓冲流根据流的方向,共有2个
写入数据到流中,字节缓冲输出流 BufferedOutputStream
读取流中的数据,字节缓冲输入流 BufferedInputStream
字节缓冲输出流BufferedOutputStream
构造方法
public BufferedOutputStream(OutputStream out)创建一个新的缓冲输出流,以将数据写入指定的底层
代码演示:
public class BufferedOutputStreamDemo01 {
public static void main(String[] args) throws IOException {
//写数据到文件的方法
write();
}
/*
* 写数据到文件的方法
* 1,创建流
* 2,写数据
* 3,关闭流
*/
private static void write() throws IOException {
//创建基本的字节输出流
FileOutputStream fileOut = new FileOutputStream("abc.txt");
//使用高效的流,把基本的流进行封装,实现速度的提升
BufferedOutputStream out = new BufferedOutputStream(fileOut);
//2,写数据
out.write("hello".getBytes());
//3,关闭流
out.close();
}
}
字节缓冲输入流 BufferedInputStream
构造方法
public BufferedInputStream(InputStream in)
代码演示:
/*
* 从文件中读取数据
* 1,创建缓冲流对象
* 2,读数据,打印
* 3,关闭
*/
private static void read() throws IOException {
//1,创建缓冲流对象
FileInputStream fileIn = new FileInputStream("abc.txt");
//把基本的流包装成高效的流
BufferedInputStream in = new BufferedInputStream(fileIn);
//2,读数据
int ch = -1;
while ( (ch = in.read()) != -1 ) {
//打印
System.out.print((char)ch);
}
//3,关闭
in.close();
}
体验一下使用基本的流与使用高效的流完成复制
代码演示:
/*
* 需求:将d:\\test.avi文件进行复制
* 采用4种方式复制
* 方式1: 采用基本的流,一次一个字节的方式复制 共耗时 224613毫秒
* 方式2: 采用基本的流,一个多个字节的方式赋值 共耗时 327毫秒
* 方式3: 采用高效的流,一次一个字节的方式复制 共耗时 2047毫秒
* 方式4: 采用高效的流,一个多个字节的方式赋值 共耗时 96毫秒
*
* 数据源: d:\\test.avi
* 目的地1: d:\\copy1.avi
* 目的地2: d:\\copy2.avi
* 目的地3: d:\\copy3.avi
* 目的地4: d:\\copy4.avi
*
* 实现的步骤:
* 1,指定数据源
* 2,指定目的地
* 3,读数据
* 4,写数据
* 5,关闭流
*
*/
public class CopyAVI {
public static void main(String[] args) throws IOException {
//开始计时
long start = System.currentTimeMillis();
//方式1: 采用基本的流,一次一个字节的方式复制
//method1("d:\\test.avi", "d:\\copy1.avi");
//方式2: 采用基本的流,一个多个字节的方式赋值
//method2("d:\\test.avi", "d:\\copy2.avi");
//方式3: 采用高效的流,一次一个字节的方式复制
//method3("d:\\test.avi", "d:\\copy3.avi");
//方式4: 采用高效的流,一个多个字节的方式赋值
method4("d:\\test.avi", "d:\\copy4.avi");
//结束计时
long end = System.currentTimeMillis();
//打印耗时多少毫秒
System.out.println("共耗时 " +(end - start)+ "毫秒");
}
//方式4: 采用高效的流,一个多个字节的方式赋值
private static void method4(String src, String dest) throws IOException {
//1,指定数据源
BufferedInputStream in = new BufferedInputStream(new FileInputStream(src));
//2,指定目的地
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
//3,读数据
byte[] buffer = new byte[1024];
int len = -1;
while ( (len = in.read(buffer)) != -1) {
//4,写数据
out.write(buffer, 0, le n);
}
//5,关闭流
in.close();
out.close();
}
//方式3: 采用高效的流,一次一个字节的方式复制
private static void method3(String src, String dest) throws IOException {
//1,指定数据源
BufferedInputStream in = new BufferedInputStream(new FileInputStream(src));
//2,指定目的地
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
//3,读数据
int ch = -1;
while ((ch=in.read()) != -1) {
//4,写数据
out.write(ch);
}
//5,关闭流
in.close();
out.close();
}
//方式2: 采用基本的流,一个多个字节的方式赋值
private static void method2(String src, String dest) throws IOException {
//1,指定数据源
FileInputStream in = new FileInputStream(src);
//2,指定目的地
FileOutputStream out = new FileOutputStream(dest);
//3,读数据
byte[] buffer = new byte[1024];
int len = -1;
while ( (len=in.read(buffer)) != -1) {
//4,写数据
out.write(buffer, 0, len);
}
//5,关闭流
in.close();
out.close();
}
//方式1: 采用基本的流,一次一个字节的方式复制
private static void method1(String src, String dest) throws IOException {
//1,指定数据源
FileInputStream in = new FileInputStream(src);
//2,指定目的地
FileOutputStream out = new FileOutputStream(dest);
//3,读数据
int ch = -1;
while (( ch=in.read()) != -1) {
//4,写数据
out.write(ch);
}
//5,关闭流
in.close();
out.close();
}
}
字符缓冲流
字符缓冲输入流 BufferedReader
字符缓冲输出流 BufferedWriter
完成文本数据的高效的写入与读取的操作
字符缓冲输出流 BufferedWriter
将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。
方法:
void newLine() 根据当前的系统,写入一个换行符
/*
* BufferedWriter 字符缓冲输出流
* 方法
* public void newLine()写入一个行分隔符
*
* 需求: 通过缓冲输出流写入数据到文件
* 分析:
* 1,创建流对象
* 2,写数据
* 3,关闭流
*
*/
public class BufferedWriterDemo {
public static void main(String[] args) throws IOException {
//创建流
//基本字符输出流
FileWriter fileOut = new FileWriter("file.txt");
//把基本的流进行包装
BufferedWriter out = new BufferedWriter(fileOut);
//2,写数据
for (int i=0; i<5; i++) {
out.write("hello");
out.newLine();
}
//3,关闭流
out.close();
}
}
字符缓冲输入流 BufferedReader
从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
方法:
public String readLine() 读取一个文本行,包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null
/*
* BufferedReader 字符缓冲输入流
*
* 方法:
* String readLine()
* 需求:从文件中读取数据,并显示数据
*/
public class BufferedReaderDemo {
public static void main(String[] args) throws IOException {
//1,创建流
BufferedReader in = new BufferedReader(new FileReader("file.txt"));
//2,读数据
//一次一个字符
//一次一个字符数组
//一次读取文本中一行的字符串内容
String line = null;
while( (line = in.readLine()) != null ){
System.out.println(line);
}
//3,关闭流
in.close();
}
}
使用字符缓冲流完成文本文件的复制
代码演示:
/*
* 采用高效的字符缓冲流,完成文本文件的赋值
*
* 数据源: file.txt
* 目的地: copyFile.txt
*
* 分析:
* 1,指定数据源, 是数据源中读数据,采用输入流
* 2,指定目的地,是把数据写入目的地,采用输出流
* 3,读数据
* 4,写数据
* 5,关闭流
*/
public class CopyTextFile {
public static void main(String[] args) throws IOException {
//1,指定数据源, 是数据源中读数据,采用输入流
BufferedReader in = new BufferedReader(new FileReader("file.txt"));
//2,指定目的地,是把数据写入目的地,采用输出流
BufferedWriter out = new BufferedWriter(new FileWriter("copyFile.txt"));
//3,读数据
String line = null;
while ( (line = in.readLine()) != null ) {
//4,写数据
out.write(line);
//写入换行符号
out.newLine();
Out.flush();
}
//5,关闭流
out.close();
in.close();
}
}
流的操作规律
IO流中对象很多,解决问题(处理设备上的数据时)到底该用哪个对象呢?
把IO流进行了规律的总结(四个明确):
l 明确一:要操作的数据是数据源还是数据目的。
源:InputStream Reader
目的:OutputStream Writer
先根据需求明确要读,还是要写。
l 明确二:要操作的数据是字节还是文本呢?
源:
字节:InputStream
文本:Reader
目的:
字节:OutputStream
文本:Writer
已经明确到了具体的体系上。
l 明确三:明确数据所在的具体设备。
源设备:
硬盘:文件 File开头。
内存:数组,字符串。
键盘:System.in;
网络:Socket
目的设备:
硬盘:文件 File开头。
内存:数组,字符串。
屏幕:System.out
网络:Socket
完全可以明确具体要使用哪个流对象。
l 明确四:是否需要额外功能呢?
额外功能:
转换吗?转换流。InputStreamReader OutputStreamWriter
高效吗?缓冲区对象。BufferedXXX
InputStream
FileInputStream
BufferedInputStream
OuputStream
FileOutputStream
BufferedOuputStream
Writer
OutputStreamWriter
FileWriter
BufferedWriter
Reader
InputStreamReader
FileReader
BufferedReader
2. Properties类
Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
特点:
1、Hashtable的子类,map集合中的方法都可以用。
2、该集合没有泛型。键值都是字符串。
3、它是一个可以持久化的属性集。键值可以存储到集合中,也可以存储到持久化的设备(硬盘、U盘、光盘)上。键值的来源也可以是持久化的设备。
4、有和流技术相结合的方法。
properties用到的一些方法:
load(InputStream) / void 把指定流所对应的文件中的数据,读取出来,保存到Propertie集合中
load(Reader) / void
store(OutputStream,commonts) / void把集合中的数据,保存到指定的流所对应的文件中,参数commonts代表对描述信息
stroe(Writer,comments) / void;
代码演示:
/*
*
* Properties集合,它是唯一一个能与IO流交互的集合
*
* 需求:向Properties集合中添加元素,并遍历
*
* 方法:
* public Object setProperty(String key, String value)调用 Hashtable 的方法 put。
* public Set<String> stringPropertyNames()返回此属性列表中的键集,
* public String getProperty(String key)用指定的键在此属性列表中搜索属性
*/
public class PropertiesDemo01 {
public static void main(String[] args) {
//创建集合对象
Properties prop = new Properties();
//添加元素到集合
//prop.put(key, value);
prop.setProperty("周迅", "张学友");
prop.setProperty("李小璐", "贾乃亮");
prop.setProperty("杨幂", "刘恺威");
//System.out.println(prop);//测试的使用
//遍历集合
Set<String> keys = prop.stringPropertyNames();
for (String key : keys) {
//通过键 找值
//prop.get(key)
String value = prop.getProperty(key);
System.out.println(key+"==" +value);
}
}
}
将集合中内容存储到文件
操作流程:
需求:使用Properties集合,完成把集合内容存储到IO流所对应文件中的操作
分析:
1,创建Properties集合
2,添加元素到集合
3,创建流
4,把集合中的数据存储到流所对应的文件中
stroe(Writer,comments)
store(OutputStream,commonts)
把集合中的数据,保存到指定的流所对应的文件中,参数commonts代表对描述信息
5,关闭流
代码演示:
public class PropertiesDemo02 {
public static void main(String[] args) throws IOException {
//1,创建Properties集合
Properties prop = new Properties();
//2,添加元素到集合
prop.setProperty("周迅", "张学友");
prop.setProperty("李小璐", "贾乃亮");
prop.setProperty("杨幂", "刘恺威");
//3,创建流
FileWriter out = new FileWriter("prop.properties");
//4,把集合中的数据存储到流所对应的文件中
prop.store(out, "save data");
//5,关闭流
out.close();
}
}
读取文件中的数据,并保存到集合
操作流程:
需求:从属性集文件prop.properties 中取出数据,保存到集合中
分析:
1,创建集合
2,创建流对象
3,把流所对应文件中的数据 读取到集合中
load(InputStream) 把指定流所对应的文件中的数据,读取出来,保存到Propertie集合中
load(Reader)
4,关闭流
5,显示集合中的数据
代码演示:
public class PropertiesDemo03 {
public static void main(String[] args) throws IOException {
//1,创建集合
Properties prop = new Properties();
//2,创建流对象
FileInputStream in = new FileInputStream("prop.properties");
//FileReader in = new FileReader("prop.properties");
//3,把流所对应文件中的数据 读取到集合中
prop.load(in);
//4,关闭流
in.close();
//5,显示集合中的数据
System.out.println(prop);
}
}