IO流概念 OutputStream 写字节 InputStream 读字节 综合复制图片 四.字符输入流FileReader 读
五.字符输出流 FileWriter 写 六.使用try catch 处理异常 七.Properties集合 store方法 写入文件 load 方法 加载文件
IO流概念
1. java.io. FileOutputStream extends OutputStream extends writer writer 所有输出流的超类
1.OutputStream: 字节输出流 意思是把内存中的数据写入到磁盘
2.java.io.FileInputStream extends InputStream extends reader reader 所有输入流的超类
1.InputStream:字节输入流 从磁盘把数据输入到内存
这里的 输入输出指的是相对于内存来说
3.程序 读写 获得文件对象 会有IO异常 需要处理
1. - flush :刷新缓冲区,流对象可以继续使用。
2. - close: 先刷新缓冲区,然后通知系统释放资源。流对象不可以再被使用了。
OutputStream 写字节
1.创建一个FileOutputStream对象,构造方法中传递写入数据的目的地
public FileOutputStream(xxxx, boolean append)
FileOutputStream fos = new FileOutputStream("a.txt"); FileOutputStream fos = new FileOutputStream(new File("a.txt"));
构造方法可以传入一个字符串类型,也可以是一个File 文件类型 ,如果append为true表示 追加写 默认为false
2.调用FileOutputStream对象中的方法write,把数据写入到文件中
1.fos.write(97); 写入一个字节,其实是一个二进制数,但文本编辑器 对应Ascll码翻译过来显示 a
2.public void write(byte b[]) 写入一个byte类型的数组
3.public void write(byte b[ ], int off, int len) 写入一个byte类型的数组 并且可以指定起始位置,和长度
byte[] bytes= {65,66,67,68,69};
fos.write(bytes); ABCDE
fos.write(bytes,1,2); BC
byte[] bytes = "你好".getBytes();
System.out.println(Arrays.toString(bytes)); [-28, -67, -96, -27, -91, -67] utf-8一个中文是三个字节
fos.write(bytes);
fos.write("\r\n".getBytes()); 换行写
fos.close();
4.注意: 如果写的第一个字节是正数(0-127),那么显示的时候会查询ASCII表
如果写的第一个字节是负数,那第一个字节会和第二个字节,两个字节组成一个中文显示,查询系统默认码表(GBK)
3.释放资源(流使用会占用一定的内存,使用完毕要把内存清空,提供程序的效率) fos.close()
InputStream 读字节
1.创建FileInputStream对象,构造方法中绑定要读取的数据源
FileInputStream fis = new FileInputStream("b.txt"); 可以是str类型 也可以是 File 类型的文件
2.使用FileInputStream对象中的方法read,读取文件
1.fis.read() 读取文件中的字节 每read一次指针左移一次 返回值为Ascll码值
FileInputStream fis = new FileInputStream("a.txt"); abc
int num = fis.read(); 97
int num2 = fis.read(); 98 每read一次指针左移一次
int num3 =(char)fis.read(); c 强转 为字符c
int num4 =fis.read(); -1 空时为-1
2.循环读取
int num ;
while ((num=fis.read())!=-1){ 判断读出字节不为-1时
System.out.println(num);
}
2.int read( byte[ ] b ) 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。
1.方法的参数byte[ ]的作用?
起到缓冲作用,存储每次读取到的多个字节 数组的长度一把定义为1024(1kb)或者1024的整数倍
2.方法的返回值 每次读取的有效字节个数
FileInputStream fis = new FileInputStream("a.txt");
byte[] bytes = new byte[1024];
int num ; 记录每次读取的有效字节个数
while ((num=fis.read(bytes))!=-1){
System.out.println(Arrays.toString(bytes)); 返回Ascll码 数组[ 98, 99, 100, 97, 100, 97, -27, -92, -89, -27]
System.out.println(new String(bytes,0,num)); abcdada大厦sad手动安师大安师大安师大
}
String构造方法 new String(bytes,0,len) 将字节数组转换为字符串 参数起始位置,和个数 为读取的有效字节个数
3.释放资源 fis.close();
综合复制图片
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("D:\\java\\Java源码\\first\\jichu-day01\\img.jpg");
FileOutputStream fos = new FileOutputStream("img2.jpg");
byte[] bytes = new byte[1024];
int num ;
while ((num=fis.read(bytes))!=-1){
fos.write(bytes,0,num);
}
fis.close();
fos.close();}
四.字符输入流FileReader 读
1.java.io.FileReader extends InputStreamReader extends Reader FileReader(String fileName) FileReader(File file) 2. char[] chars = new char[1024]; 定一个字符数组 ,来缓冲读到的 字符 放入数组
3.定义一个num 接受 读到的有效的字符个数
4.String构造方法 new String(char,0,len) 将字符数组转换为字符串 参数起始位置,和个数 为读取的有效字符个数
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("D:\\java\\Java源码\\first\\a.txt");
int len = 0; 读单个 存放Ascii码值
while((len = fr.read())!=-1){
System.out.print((char)len);
}
char[] chars = new char[1024]; 读多个 数组存放多个Ascii码值 不能使用byte
int num; 存放有效个数
while ((num = fr.read(chars)) != -1) {
System.out.println(new String(chars,0,num));
}
}
五.字符输出流 FileWriter 写
1.void write(char[] cbuf) 写入字符数组。
- abstract void write(char[] cbuf, int off, int len) 写入字符数组的某一部分,off数组的开始索引,len写的字符个数
2.void write(String str)写入字符串。
- void write(String str, int off, int len) 写入字符串的某一部分,off字符串的开始索引,len写的字符个数。
FileWriter fw = new FileWriter("D:\\java\\Java源码\\first\\a.txt",true); 追加写
fw.write(97); 可以是字节 字符 字符串 字符数组
fw.flush();
fw.close();
六.使用try catch 处理异常
1.使用finally 让资源回收
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("a.txt");
fw.write("我的儿子");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}}}}
注意: 1.fw 作用域问题,所以在try外面 定义fw, 提高变量fw的作用域
2.如果fw创建失败还是 null 使用close()会出先空指针异常 所以先判断一下
2.jdk7 try(){} 新特性
1.在try的后边可以增加一个(),在括号中可以定义流对象 那么这个流对象的作用域就在try中有效
try中的代码执行完毕,会自动把流对象释放,不用写finally
public static void main(String[] args) {
char[] chars = new char[1024];
int num;
try(FileReader fw = new FileReader("a.txt");) {
while ((num=fw.read(chars))!=-1){
System.out.println(new String(chars,0,num));
}
}catch (Exception e){
System.out.println(e);
}}
3.jdk9 try(){} 新特性
在try 外定义 可放入对象名 如果有多个 用, 隔开 只在 try 有效 执行完自动释放
public static void main(String[] args) {
char[] chars = new char[1024];
int num;
FileReader fw = new FileReader("a.txt");
try(fw ) {
while ((num=fw.read(chars))!=-1){
System.out.println(new String(chars,0,num));
}
}catch (Exception e){
System.out.println(e);}}
七.Properties集合
1. java.util.Properties extends Hashtable<k,v> implements Map<k,v>
2. Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。是一个唯一和IO流相结合的集合
1. 可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储
1.可以使用Properties集合中的方法load,把硬盘中保存的文件(键值对),读取到集合中使用
Properties集合是一个双列集合,key和value默认都是字符串 类似Python 的loads 和 dumps方法
3.Properties 集合的特有方法
1. Object setProperty(String key, String value) 相当于Map集合的 Put方法 添加一个键值对
2. String getProperty(String key) 通过key找到value值, 相当于Map集合中的get(key)方法
3. Set<String> stringPropertyNames() 返回此属性列表中的键集 相当于Map集合中的SetKey() 方法
Properties properties = new Properties(); 默认是String类型
properties.setProperty("打啊", "20");
properties.setProperty("看见", "16");
properties.setProperty("辅导", "19");
Set<String> keys = properties.stringPropertyNames(); 将键存放在一个集合中
for (String key : keys) {
System.out.println(properties.get(key));
}
store方法 写入文件
1.void store(OutputStream out, String comments) 字节输出流,写入中文是乱码
2.void store(Writer writer, String comments) 字符输出流,可以写中文 都有一个注释都文件的解释
properties.store( new FileWriter("abc.txt") ,"data_save");
load 方法 加载文件
1. void load(InputStream inStream) 字节输入流,写入中文是乱码
2. void load(Reader reader) 字符输入流,可以写中文
3.注意存储键值对的文件中,键与值默认的连接符号可以使用=,空格(其他符号) 可以加上#就不会读取了
Properties prop = new Properties();
prop.load(new FileReader("abc.txt"));
Set<String> set = prop.stringPropertyNames();
for (String key : set) {
String value = prop.getProperty(key);
System.out.println(key+"="+value);
}