JavaSE|IO流:FileInputStream、FileOutputStream、BufferedInputStream、BufferedOutputStream,
分享于 点击 12840 次 点评:223
JavaSE|IO流:FileInputStream、FileOutputStream、BufferedInputStream、BufferedOutputStream,
JavaSE|IO流
- IO流分类
- FileOutputStream
- 构造方法
- 成员方法
- 常见问题
- 加入异常处理
- FileInputStream
- 构造方法
- 成员方法
- 字节缓冲流
- BufferedOutputStream
- 构造方法
- BufferedInputStream
- 构造方法
IO流用来处理设备之间的数据传输。
Java对数据的操作是通过流的方式。
Java用于操作流的对象都在IO包中。
IO流分类
- 按照数据流向
– 输入流 读入数据
– 输出流 写出数据 - 按照数据类型
– 字节流:字节输入流 InputStream; 字节输出流 OutputStream
– 字符流:字符输入流 Reader; 字符输出流 Writer
字节流:
- InputStream
– FileInputStream
– BufferedInputStream - OutputStream
– FileOutputStream
– BufferedOutputStream
字符流:
- Reader
– FileReader
– BufferedReader - Writer
– FileWriter
– BufferedWriter
如何选择使用字节流还是字符流?
如果数据所在的文件通过windows自带的记事本打开并能读懂里面的内容,就用字符流。其他用字节流。如果你什么都不知道,就用字节流。
PS:一般在探讨IO流的时候,如果没有明确说明按哪种分类来说,默认情况下是按照数据类型来分的。
FileOutputStream
构造方法
// 创建字节输出流对象
// 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());
- FileOutputStream(File file, boolean append)
- FileOutputStream(String name, boolean append)
成员方法
FileOutputStream fos = new FileOutputStream("fos2.txt");
// 调用write()方法
fos.write(97); //97 -- 底层二进制数据 -- 通过记事本打开 -- 找97对应的字符值 -- a
fos.write(57); //9
fos.write(55); //7
//public void write(byte[] b):写一个字节数组
byte[] bys={97,98,99,100,101};
fos.write(bys); //abcde
//public void write(byte[] b,int off,int len):写一个字节数组的一部分
fos.write(bys,1,3); //bcd
//释放资源
fos.close();
常见问题
加入异常处理
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();
}
}
}
}
}
FileInputStream
把刚才写的数据读取出来显示在控制台。
构造方法
成员方法
FileInputStream fis = new FileInputStream("FileOutputStreamDemo.java");
// 用循环改进
// 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();
- public int read(byte[] b)
从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。返回读入缓冲区的字节总数,如果因为已经到达文件末尾而没有更多的数据,则返回 -1。
// 创建字节输入流对象
// FileInputStream fis = new FileInputStream("fis2.txt");
FileInputStream fis = new FileInputStream("FileOutputStreamDemo.java");
// 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();
字节缓冲流
字节流一次读写一个数组的速度明显比一次读写一个字节的速度快很多,这是加入了数组这样的缓冲区效果,java本身在设计的时候,也考虑到了这样的设计思想(装饰设计模式后面讲解),所以提供了字节缓冲区流。
BufferedOutputStream
该类实现缓冲的输出流。通过设置这种输出流,应用程序就可以将各个字节写入底层输出流中,而不必针对每次字节写入调用底层系统。
构造方法
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bos.txt"));
bos.write("hello);
bos.close();
BufferedInputStream
构造方法
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("bos.txt"));
byte[] bys = new byte[1024];
int len = 0;
while((len = bis.read(bys)) != -1 ){
System.out.println(new String(bys, 0, len));
}
bis.close();
相关文章
- 暂无相关文章
用户点评