目录
处理IO流异常:jdk7 9新特性
/*
try-catch处理IO流异常,Properties集合
*/
import java.io.FileWriter;
import java.io.IOException;
/*在jdk1.7之前使用try-catch处理异常
格式:
try{
//可能会出现异常的代码
}catch(异常类的变量){
//异常的处理逻辑
}finally{
//一定会执行的代码
}
* */
public class Demo01IO03 {
public static void main(String[] args){
//提高变量fw的作用域,让finally可以使用
//变量的定义时可以没有值,但在使用时必须有值
// fw = new FileWriter("E:\\IdeaProjects\\java_lianxi\\1.txt"); 如果创建失败,fw没有值,就会报错
FileWriter fw=null;
try {
fw = new FileWriter("E:\\IdeaProjects\\java_lianxi\\1.txt");
for(int i=0;i<10;i++) {
fw.write("helloworld"+i+"\r\n");
}
}catch(IOException e){
System.out.println(e);
}finally {
//创建对象失败了,fw的默认值就是null,null不能调用方法,会抛出一个NullPointException;所以需要增加一个判断;创建对象成功再把资源释放
if (fw != null) {
try {//fw.close()方法声明抛出IOException异常对象,所以就处理这个异常 throws或try-catch
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/*jdk1.7的新特性:【了解】
在try的后面可以增加一个(),可以在里面定义一个流对象
那么这个流对象的作用域就在try中有效。
try中的代码执行完毕,会自动把对象释放,不用写finallyl
格式:
try(//定义流对象;...){
//可能会出现异常的代码
}catch(异常类的变量){
//异常的处理逻辑
}
*/
public class Demo01IO01 {
public static void main(String[] args) {
try( FileOutputStream fos1=new FileOutputStream("E:\\IdeaProjects\\java_lianxi\\2.txt",true);){
for (int i = 1; i <11 ; i++) {
fos1.write("你好".getBytes());
fos1.write("\r\n".getBytes());
fos1.close();
}
}catch(IOException e){
System.out.println(e);
}
}
}
/*jdk1.9的新特性:【了解】
在try的前面可以定义流对象
在try后面的()中也可以直接引入流对象的名称(变量名);
在try底代码块执行完毕之后;流对象也可以直接释放掉,不用写finally
格式:
A a=new A();
B b=new B();
try(a,b){
//可能会出现异常的代码
}catch(异常类的变量){
//异常的处理逻辑
}
*/
public class Demo01fuzhi {
public static void main(String[] args) throws IOException {
FileInputStream fis=new FileInputStream("E:\\IdeaProjects\\java_lianxi\\1.txt");
FileOutputStream fos=new FileOutputStream("D:\\1.txt");
try(fis,fos){
byte[] by1=new byte[1024];
int len=0;
while((len=fis.read())!=-1){
fos.write(by1,0,len);
}
fos.close();
fis.close();
}catch(Exception e){
sout(e);
}
}
}
Properties集合
/*
Properties集合
java.util.Properties extends Hashtable<k,v> implements Map<k,v>
Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
是唯一一个与IO流结合的集合:【集合中的数据是临时数据;程序一结束,集合中的数据在内存中就消失】
可以使用Properties集合的store(),把内存中的临时数据,持久化写入硬盘存储
可以使用Properties的方法load(),把硬盘中保存的文件(键值对),读取到集合中使用。
属性列表中每个键值对应的值都是字符串。
Properties集合是一个双列集合,k与v默认都是字符串。有一些操作字符串的特有方法。
方法:
Object setProperty(String key, String value) 调用 Hashtable 的方法 put。
String getProperty(String key) 通过k找v;相当于Map集合的get(key)方法。
Set<String> stringPropertyNames() 返回此属性列表中的键集,其中该键及其对应值是字符串;相当于Map集合的keySet();
void store(OutputStream out, String comments)
void store(Writer writer, String comments)
void load(InputStream inStream)
void load(Reader reader)
参数:OutputStream out:集合中不能存储中文
Writer write:集合中可以存储中文
String comments:解释说明文件的作用,注释不能使用中文,会产生乱码;一般使用“”空字符串
InputStream inStream:字节输入流;不能读取中文键值对
Reader read:字符输入流;可以读取中文键值对
临时数据写入硬盘存储使用步骤:
1、创建Properties对象,添加数据。
2、创建字符输出流对象,构造方法绑定要输出的目的地。
3、使用store(),把内存中的临时数据,持久化写入硬盘存储
4、释放资源
硬盘中保存的文件读取到集合中使用步骤:
1、创建Pro集合对象
2、使用load(),读取保存键值对的文件
3、遍历Properties集合
注意:键与值之间默认的连接符号可以使用 等号,空格
存储键值对的文件中可以只用井号进行注释,被注释的键值对不会再被读取
存储键值对的文件中,键与值默认都是字符串,不用在加引号了
*/
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
public class Demo01Properties {
public static void main(String[] args) throws IOException {
show01();
show02();
show03();
}
private static void show03() throws IOException {
Properties prop=new Properties();
prop.load(new FileReader("E:\\IdeaProjects\\java_lianxi\\1.txt"));//匿名对象不用关闭流了
Set<String> set = prop.stringPropertyNames();
for (String key : set) {
String value = prop.getProperty(key);
System.out.println(key+"="+value);
}
}
private static void show02() throws IOException {
Properties prop=new Properties();//泛型默认字符串
prop.setProperty("禾苗","160");
prop.setProperty("赵丽颖","161");
prop.setProperty("杨幂","162");
//创建流
FileWriter fw=new FileWriter("E:\\IdeaProjects\\java_lianxi\\1.txt");
prop.store(fw,"save data");
fw.close();
}
private static void show01() {
//存储数据
Properties prop=new Properties();//泛型默认字符串
prop.setProperty("禾苗","160");
prop.setProperty("赵丽颖","161");
prop.setProperty("杨幂","162");
//prop.put(1,true);//也可以用父接口的put()
//把集合中的键取出,存储在一个Set集合中
Set<String> set = prop.stringPropertyNames();
//遍历Set集合,取出键
for (String key: set) {
//通过键获取值
String value = prop.getProperty(key);
System.out.println(key+"--"+value);
}
}
}
缓冲流
/*
缓冲流:提高IO流的效率
如字节输入缓冲流:给字节输入流增加一个缓冲区【数组】,不用一个一个字节读取了,提高服务字节效率。
BufferedInputStream(new FileInputStream())
int len=fis.read();
字节缓冲流:BufferedOutStream,BufferedInputStream
字符缓冲流:BufferedWriter,BufferedReader
*/
/*字节缓冲输出流
java.io.BufferedOutputStream extends OutputStream 继承父类共性成员方法
* 构造方法:
* BufferedOutputStream(OutputStream out)
BufferedOutputStream(OutputStream out, int size)
参数:字节输出流,指定缓冲流内部大小;【不指定就默认】
* 使用步骤:
* 1、创建FileOutputStream对象,构造方法传递目的地路径
* 2、创建BufferedOutputStream对象,构造方法传递FileOutputStream对象
* 3、使用BufferedOutputStream的方法write(),把数据写入内部缓冲区
* 4、使用BufferedOutputStream的方法flush(),把内部缓冲区的数据刷新到文件中
* 5、释放资源(调用flush()刷新数据,就可以省略)
* */
public class Demo01Buffered01 {
public static void main(String[] args) throws IOException {
FileOutputStream fos=new FileOutputStream("E:\\IdeaProjects\\java_lianxi\\1.txt");
BufferedOutputStream bos=new BufferedOutputStream(fos);
bos.write("数据写入".getBytes());
bos.flush();
}
}
==
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
/*字节缓冲输入流
java.io.BufferedInputStream extends InputStream 继承来自父类的共性方法
* 构造方法:
* BufferedInputStream(InputStream in)
BufferedInputStream(InputStream in, int size)
参数:字节输入流,指定缓冲流大小【不写就默认】
*使用步骤:
* 1、创建一个FileInputStream对象,构造方法参数传递数据源路径
* 2、创建BufferedInputStream对象,构造方法传递FileInputStream对象
* 3、使用read(),读取文件
* 4、释放资源
* */
public class Demo01Buffered02 {
public static void main(String[] args) throws IOException {
FileInputStream fis=new FileInputStream("E:\\IdeaProjects\\java_lianxi\\1.txt");
BufferedInputStream bis=new BufferedInputStream(fis);
int len=0;//记录每次读取到的有效字节个数
byte[] by=new byte[1024];
while((len=bis.read(by))!=-1){
System.out.print((len+" "));//12
System.out.println(new String(by));//数据写入
}
bis.close();
}
}
==
//文件复制案例
public class Demo01fuzhi {
public static void main(String[] args) throws IOException {
long s=System.currentTimeMillis();//测定一下效率
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("E:\\IdeaProjects\\java_lianxi\\1.txt"));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("D:\\1.txt"));
//方法1、一次读取一个字节
/*int len=0;
while((len=bis.read())!=-1){
bos.write(len);
}
bos.close();
bis.close();
long e=System.currentTimeMillis();
System.out.println("复制文件共耗时:"+(e-s)+"毫秒");*/
//方法2、一次读取多个字节
int len=0;
byte[] by=new byte[1024];
while((len=bis.read(by))!=-1){
bos.write(by,0,len);
}
bos.close();
bis.close();
long e=System.currentTimeMillis();
System.out.println("复制文件共耗时:"+(e-s)+"毫秒");
}
}
============
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
/*字符缓冲输出流
java.io.BufferedWriter extends Writer 继承父类的公性方法
构造方法:
BufferedWriter(Writer out)
BufferedWriter(Writer out, int sz)
参数:字符输出流,指定缓冲区大小
* 特有成员方法:void newLine() 写入一个行分隔符 ;根据不同系统获取不同行分隔符
* 使用步骤:
1、创建一个BufferedWriter对象。构造方法传参字符输出流
2、调用字符缓冲输出流的方法write(),把数据写入内存缓冲区中
3、调用flush();把内存缓冲区的数据刷新到文件中
4、释放资源
* */
public class Demo01Buffer03 {
public static void main(String[] args) throws IOException {
BufferedWriter bw=new BufferedWriter(new FileWriter("E:\\IdeaProjects\\java_lianxi\\1.txt"));
for (int i = 0; i < 10; i++) {
bw.write("禾苗");
//bw.write("\r\n");
bw.newLine();//println()调用的就是newLine();
}
bw.flush();
bw.close();
}
}
====
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/*字符缓冲输入流
java.io.BufferedReader extends Reader 继承自父类的共性方法
* 构造方法:
BufferedReader(Reader in)
BufferedReader(Reader in, int sz)
参数:字符输入流,指定缓冲区大小
特有成员方法:
String readLine() 读取一个文本行。读取一行数据;行的终止符号:换行 ('\n')、回车 ('\r') 或回车后直接跟着换行。
返回值:包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null
使用步骤:
1、创建一个BufferedReader对象,构造方法中传递字符输入流
2、使用字符缓冲输入流对象中的方法read(),或readLine()读取文本
3、释放资源
* */
public class Demo01Buffered04 {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new FileReader("E:\\IdeaProjects\\java_lianxi\\1.txt"));
String line;
while((line=br.readLine())!=null){
System.out.println(line);
}
br.close();
}
}
=
import java.io.*;
import java.util.HashMap;
/*练习:将文本内容按照(1...2...3...)进行排序
文本内容为:
2.bbb
3.ccc
1.aaa
分析:1、创建一个HashMap,key 存储每行文本的序号;value 存储每行的文本
2、创建字符缓冲输入流对象,构造方法中传参字符输入流
3、创建字符缓冲输出流对象,构造方法中传参字符输出流
4、使用字符字符缓冲输入流中的方法readLine();逐行读取文本
5、对读取到的文本进行分割;获取行的序号和文本内容
6、把切割好的序号和文本的内容存储到HashMap集合中;key序号会自动排序
7、遍历HashMap集合,获取每一个键值对
8、把每一个键值对,拼接为文本行
9、把拼接好的文本行,使用字符缓冲输出流的方法write(),写入到文件中
10、释放资源
* */
public class Demo01lianxi {
public static void main(String[] args) throws IOException {
HashMap<String,String> map=new HashMap<>();
BufferedReader br=new BufferedReader(new FileReader("E:\\IdeaProjects\\java_lianxi\\1.txt"));
BufferedWriter bw=new BufferedWriter(new FileWriter("E:\\IdeaProjects\\java_lianxi\\2.txt"));
//4
String line;
while((line=br.readLine())!=null){
//5
String[] arr = line.split("\\.");//因为.是一个转义字符;所以要加\\代表只是一个.//key序号是有序的会自动排序。
//6
map.put(arr[0],arr[1]);
}
//7
for(String key:map.keySet()){//map.keySet()把key拿出来存储到一个Set集合中
String value = map.get(key);//通过key获取value
//8
line=key+"."+value;
//9
bw.write(line);
bw.newLine();
}
bw.close();
br.close();
}
}