概述
IO即Input/Output,用于处理设备之间的传输,读写文件,网络通讯等
java程序中,对数据的输入/输出操作通过流(stream)的方式进行
java.io包提供了各种类和接口,用来获取不同的数据,通过标准的方法输入或者输出数据
流的分类:
操作数据单位:字节流(8bit)、字符流(16bit)
数据流向:输入流、输出流(输入流写出的,输出流写入的)
流的角色:节点流、处理流
抽象基类 | 字节流 | 字符流 |
输入流 | InputStream | Reader |
输出流 | OutputStream | Writer |
节点流(也是文件流):FileInputStream、FileOutStream、FileReader、FileWriter
缓冲流(处理流的一种):BufferedInputStream、BufferedOutStream、BufferedReader、BufferedWriter
总之,对于文本文件(.txt、.java、.c.....)使用字符流处理,对于非文本文件(.jpg、.mp3、.doc、.ppt........)使用字节流处理
=========================================================================
输入流
{
File file = new File("readme.text");
FileReader reader = null;
try {
reader = new FileReader(file);
// 读入的操作
// read(char[] cb);返回每次读入cb数组中的字符的个数,如果达到文件末尾,返回-1
char[] cb = new char[5];
int len;
while ((len = reader.read(cb)) != -1) {
System.out.println((len));
// 方式一
// for (int i = 0; i < len; i++) {
// System.out.print(cb[i]);
// }
// 方式二
String str=new String(cb,0,len);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
{
//实例化File类的对象,指明要操作的文件
File file = new File("readme.text");
System.out.println(file.getName());
System.out.println(file.getAbsolutePath());
System.out.println(file.getAbsoluteFile());
FileReader fileReader = null;
try {
fileReader = new FileReader(file);
// 提供具体的字符流
// 数据的读入
// read()返回读入的一个字符,如果达到文件末尾,返回-1
int data = fileReader.read();
while (data != -1) {
System.out.print((char) data);
data = fileReader.read();
}
System.out.println(fileReader.read());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 流的关闭操作
if (fileReader != null) {
fileReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
输出流
// 从内存中写出数据到硬盘的文件里
// 说明 输出操作 对应的File可以不存在
// 如果不存在,输出过程会创建文件并写入
@Test
public void test3() {
FileWriter fw = null;
try {
File file = new File("readme1.text");
// 此处的true状态是写入 false状态代表覆盖
fw = new FileWriter(file,true);
fw.write("i have a dream\n");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fw!=null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
输入输出流
{
FileReader fr=null;
FileWriter fw=null;
try {
File file1=new File("readme1.text");
File file2=new File("readme2.text");
fr=new FileReader(file1);
fw=new FileWriter(file2,true);
char[] cb=new char[5];
int len;
while ((len=fr.read(cb))!=-1){
fw.write(cb,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (fw!=null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fr!=null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
缓冲流
{
FileInputStream fr = null;
FileOutputStream fw = null;
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try {
File file1 = new File("src/main/java/cn/lzc/day01/io/p1.jpg");
File file2 = new File("p2.jpg");
fr = new FileInputStream(file1);
fw = new FileOutputStream(file2);
bis=new BufferedInputStream(fr);
bos=new BufferedOutputStream(fw);
byte[] cb = new byte[1024];
int len;
while ((len = bis.read(cb)) != -1) {
bos.write(cb, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 资源关闭
// 要求,要先关闭外层的流,再关闭内层的流,
// 关闭外层流的同时,内层流也会随着关闭,所以内部流关闭省略
try {
if (bis != null)
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bos != null)
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void test7(){
BufferedReader br = null;
BufferedWriter bw=null;
try {
br=new BufferedReader(new FileReader(new File("readme1.text")));
bw=new BufferedWriter(new FileWriter(new File("readme2.text")));
String data;
while ((data=br.readLine())!=null) {
bw.write(data);
// 提供换行操作
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br!=null)
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bw!=null)
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
转换流
@Test
public void test8() {
InputStreamReader isr = null;
try {
isr = new InputStreamReader(new FileInputStream("readme1.text"), "gbk");
char[] data = new char[10];
int len;
while ((len = isr.read(data)) != -1) {
String str=new String(data,0,len);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (isr != null)
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
其他流
/**
*标准的输入、输出流
* System.in
* System.out
*
*
*/
public static void main(String[] args) {
BufferedReader br=null;
try {
InputStreamReader isr=new InputStreamReader(System.in);
br=new BufferedReader(isr);
String data;
while (true){
System.out.println("请输入字符串");
data=br.readLine();
if ("e".equalsIgnoreCase(data)||"exit".equalsIgnoreCase(data)){
System.out.println("程序结束");
break;
}
System.out.println(data.toUpperCase()+"\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(br!=null)
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
打印流
数据流
对象流