Java缓存流(高效率的流)与Properties集合
1.BufferedOutStream
2.BufferedInputStream(OutputStream out)
构造方法:
参数:字节输出流的父类 FileOutString
作用:传入哪个流哪个流高效
1.实现步骤
1.1高效写入
public class Test {
public static void main(String[] args) throws IOException {
FileOutputStream foStream = new FileOutputStream("/Users/lanou/Desktop/test/kke.txt");
BufferedOutputStream bo = new BufferedOutputStream(foStream);
bo.write("hello".getBytes());
foStream.close();
bo.close();
}
}
1.2高效读取
public class Test {
public static void main(String[] args) throws IOException {
FileInputStream foStream = new FileInputStream("/Users/lanou/Desktop/test/kke.txt");
BufferedInputStream bo = new BufferedInputStream(foStream);
byte[] bs = new byte[1024];
int len = 0;
while ((len = bo.read(bs)) != -1) {
System.out.println(new String(bs,0,len));
System.out.println(len);
System.out.println(bo.read(bs));
System.out.println((len = bo.read(bs)) );
}
foStream.close();
bo.close();
}
}
2.缓存字符流
BufferedWriter
BufferedReader
构造方法
参数:writer(父类)
可传
FileWriter OutputStreamWriter BufferedReader
特有方法:
newLine() 无关平台 Mac \n Windows/n
1.缓存流写入
public class Test {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("/Users/lanou/Desktop/test/ppp.txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.write("床前明月光");
bw.newLine();
bw.flush();
bw.write("疑是地上霜");
bw.newLine();
bw.flush();
bw.close();
fw.close();
}
}
2.缓存流读取文件内容
public class Test {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("/Users/lanou/Desktop/test/ppp.txt");
BufferedReader bfr = new BufferedReader(fr);
String string = "";
while ((string = bfr.readLine()) != null) {
System.out.println(string);
System.out.println("*");
}
bfr.close();
fr.close();
}
}
结论:按行读取 是不能把换行读取的
3.缓存流复制文件
public class Test {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("/Users/lanou/Desktop/test/ppp.txt");
BufferedReader bfr = new BufferedReader(fr);
FileWriter fw = new FileWriter("/Users/lanou/Desktop/test/ppp01.txt");
BufferedWriter bfw = new BufferedWriter(fw);
String string = "";
while ((string = bfr.readLine()) != null) {
System.out.println(string);
bfw.write(string);
bfw.newLine();
bfw.flush();
}
bfw.close();
bfr.close();
}
}
总结:
1.明确要操作的是数据源和 数据目的地
读数据源
InputStream Reader
写到数据目的地
OutPutStream Writer
2.明确要操作的是什么内容
文本音频图片等等...使用字节流(全能流)
文本字符流
文本(按编码格式读写)
使用字符流
3.明确流要在什么设备上使用
文本
网络 通过流进行数据交换 --- 字节流
4.是否需要提高效率
buffered 缓存流
2.Properties集合
2.1Properties集合的功能测试
public class Test {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
properties.put("name", "等等");
properties.put("name", "哈哈");
properties.put("哈哈", "哈哈");
System.out.println(properties);
Set<String> set = properties.stringPropertyNames();
for (String string : set) {
String value = properties.getProperty(string);
System.out.println(string + "***" + value);
}
}
}
结论:
1.Properties集合能去重
2.Properties集合值value存在于键值key中
3.Properties集合(双列)父类Hashtable
4.该集合key和value最好使用字符串
5.properties是集合中唯一一个能 IO 流配合的类
2.1用Properties储存目标文件内容
public class Test {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
FileReader fr = new FileReader("/Users/lanou/Desktop/test/znb.properties");
properties.load(fr);
System.out.println(properties);
fr.close();
}
}
2.2用Properties向目标文件写入内容
public class Test {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
properties.setProperty("姓名", "狗蛋");
properties.setProperty("年龄", "59");
properties.setProperty("英文名", "狗蛋");
FileWriter fw = new FileWriter("/Users/lanou/Desktop/test/znb.properties");
properties.store(fw, "wan");
fw.close();
}
}
注释:
1.创建文件时后缀可以随便取名但为了好辨识类型,一般写明确相关后缀
2.在Properties文件中 可以使用#来注释
3.作用:properties是集合中唯一一个能 IO 流配合的类