- 介绍
- 实例
-
什么是io
io即input(输入),output(输出)。
输入:往内存中读入硬盘上的数据。
输出:从内存中往硬盘上写数据。
-
分类
流按照方向可分为:输入流,输出流。
按照处理数据单位:字节流,字符流。
- 字节流:按照一个字节的大小进行处理
- 字符流:按照一个字符进行读取。例如“我爱java”这个句子,如果按照一个字节的大小去读,就会出现乱码,因为中文大小是两个字节组成。因此就产生了字符流,进行读取。
-
字节流字符流区别
字节流一般用来处理图像、视频、音频、PPT、Word等类型的文件。
字符流一般用于处理纯文本类型的文件,如TXT文件等。
字节流一般以Stream结尾。
字符流一般以Reader/Writer结尾
-
操作步骤
- 确定源
- 打开流
- 操作
- 关闭流
public class IOStudy1 {
public static void main(String[] args) {
//确定源
File srcFile = new File("src/com/cc/iostudy/read.txt");
File destFile = new File("src/com/cc/iostudy/writer.txt");
FileInputStream is = null;
FileOutputStream os = null;
try {
//打开流
is = new FileInputStream(srcFile);
os = new FileOutputStream(destFile);
//操作
byte[] buffer = new byte[400];
int len = 0;
while((len = is.read(buffer)) != -1) {
os.write(buffer);
os.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}



