IO流
概述:
用来处理设备(硬盘,控制台,内存)间的数据。
按照流操作的数据的类型不同:分为字节流和字符流。(字符流是为了方便中文的操作而来的。)
按照流的流向不同分为:输入流,输出流
2、IO流常用基类:
(1)字节流
输出字节流:
OutputStream:字节写入流抽象类
|--->FileOutputStream:字节写入流
|--->BufferedOutputStream:字节写入流缓冲区
|--->PrintStream:打印流
输入字节流:
InputStream:字节读取流抽象类
|--->FileInputStream:字节读取流
|--->BufferedInputStream:字节读取流缓冲区
(2)字符流
输出字符流:
Writer:字符写入流的抽象
|--->FileWriter:字符写入流
|--->BufferedWriter:字符写入流缓冲区
|--->OutputStreamWriter:字符通向字节的转换流(涉及键盘录入时用)
|---> PrintStream: 打印流,可处理各种类型的数据
输入字符流:
Reader: 字符读取流的抽象类
|--->FileReader:字符读取流
|--->LineNumberReader:跟踪行号的缓冲字符读取流
|--->BufferedReader:字符读取流缓冲区
|--->InputStreamReader:字节通向字符的转换流(涉及键盘录入时用)
IO流常用功能:
字符流:
(1)在硬盘上创建一个文件并写入信息
用字符写入流:FileWriter
FileWriter fw = new FileWriter("g:\\filewriter.txt");
fw.write("输入信息");
fw.write("也可以写入字符数组".toCharArray());
fw.flush();
fw.close();
(2)在原有文件上续写数据
FileWriter fw = new FileWriter("g:\\filewriter.txt",true);
fw.write("还可以续写信息");
fw.write("也可以写入字符数组".toCharArray());
fw.flush();
fw.close();
(3)读取硬盘上的文本文件,并将数据打印在控制台
FileReader fr = new FileReader("g:\\filewriter.txt");
第一种读取方法:一个一个字节的读
int ch = 0;
ch = fr.read();
sop((char)ch);
fr.close();
第二种读取方法:利用数组来提高效率
char[] buf = new char[1024];
int len = 0;
while((len = fr.read(buf))!=-1){
sop(new String(buf,0,len));
}
fr.close();
(4)拷贝文本文件
利用缓冲区提高数据读写效率
(无缓冲区就相当于一滴一滴的喝水,有缓冲区就相当于一杯一杯的喝水)
BufferedReader bufr = new BufferedReader(new FileReader("g:\\filewriter.txt"));
BufferedWriter bufw = new BufferedWriter(new FileWriter("d:\\copyfilewriter.txt"));
String line = null;
while((line = bufr.readLine())!=null){
burw.write(line);
bufw.newLine();
bufw.flush();
}
bufr.close();
bufw.close();
字节流:字节流写入时没有刷新
(1)在硬盘上创建一个文件并写入信息(字节流写入时没有刷新)
FileOutputStream fos = new FileOutputStream("g:\\filestream.txt");
fos.write(97);//写入一个字节,int:97代表写入char:a
fos.write("也可以写入字节数组".getBytes());//通常使用此种方式写入,直观!
fos.close();
(2)在硬盘已有文件上续写数据(字节流写入时没有刷新)
FileOutputStream fos = new FileOutputStream("g:\\filestream.txt",true);
fos.write("创建字节写入流时,传进去一个true参数就可以继续写入信息".getBytes());
fos.close();
(3)读取硬盘上的文件
FileInputStream fis = new FileInputStream("g:\\filestream.txt");
第一种读法:一个字节一个字节的读(此种读法慢)
int ch = 0;
while((ch = fis.read())!=-1){
sop((char)ch);
}
第一种读法:利用字节数组读(此种读法效率有一定提高)
byte[] buf = new byte[1024];
int len = 0;
while((len = fis.read())!=-1){
sop(new String(buf,0,len));
}
(4)拷贝字节文件,如图片或者MP3或者电影
第一种拷贝:不带缓冲区(慢,还是效率问题)
FileInputStream fis = new FileInputStream("g:\\1.mp3");
FileOutputStream fos = new FileOutputStream("g:\\copy1.mp3");
byte[] buf = new byte[1024];
int len = 0;
while((len = fis.read(buf))!=-1){
<span style="white-space:pre"> </span> fos.(buf,0,len);//字节流写入无需刷新
}
fis.close();
fos.close();
第二种拷贝:带缓冲区,高效
BufferedInputStream bufi = new BufferedInputStream(new FileInputStream("g:\\1.mp3"));
BufferedOutputStream bufo = new BufferedOutputStream(new FileOutputStream("g:\\copy1.mp3"));
int ch = 0;
while((ch = bufi.read())!=-1){
bufo.write(ch);
}
bufi.close();
bufo.close();
转换流:
(1)读取一个键盘录入
InputStream in = System.in;//创建一个键盘录入流,流不关则可以一直录入
int by1 = in.read();//一次读一个字节
int by2 = in.read();//一次读一个字节
sop(by1);//假设键盘录入的是abcd,则打印a
sop(by2);//假设键盘录入的是abcd,则打印b
in.close();
(2)键盘录入一行数据打印一行数据,如果录入的是over则结束录入
InputStream in = System.in;
StringBuilder sb = new StringBuilder();
while(true){
int ch = in.read();
if(ch=='\r')
continue;
if(ch=='\n'){
String line = sb.toString();
if("over".equals(line))
break;
sop(line.toUpperCase());//输出大写
sb.delete(0.sb.length());//清除上一行录入的数据
}else
sb.append((char)ch);
}
in.close();
(3)发现需求2中其实就是读一行的原理,故引入字节通向字符的桥梁:InputStreamReader
为提高效率加入缓冲区:
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while((line = bufr.readLine())!=null){
if("over".equals(line))
<span style="white-space:pre"> </span> break;
<span style="white-space:pre"> </span> sop(line.toUpperCase());//输出大写
<span style="white-space:pre"> </span> }
bufr.close();
(4)键盘录入数据并打印到控制台
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufw = new BufferedWriter(new OntputStreamWriter(System.out));
<span style="white-space:pre"> </span>String line = null;
<span style="white-space:pre"> </span>while((line = bufr.readLine())!=null){
<span style="white-space:pre"> </span>if("over".equals(line))
<span style="white-space:pre"> </span> break;
<span style="white-space:pre"> </span> bufw.write(line.toUpperCase());
<span style="white-space:pre"> </span> bufw.newLine();
<span style="white-space:pre"> </span> bufw.flush();
<span style="white-space:pre"> </span>}
bufr.close();
bufw.close();
(5)将键盘录入的数据存储到硬盘文件
则只需将(4)中的
BufferedWriter bufw = new BufferedWriter(new OntputStreamWriter(System.out));
改为:
BufferedWriter bufw = new BufferedWriter(new OntputStreamWriter(new FileWriter("g:\\demo.txt")));
即:
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufw = new BufferedWriter(new OntputStreamWriter(new FileWriter("g:\\demo.txt"));
<span style="white-space:pre"> </span>String line = null;
<span style="white-space:pre"> </span>while((line = bufr.readLine())!=null){
<span style="white-space:pre"> </span>if("over".equals(line))
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>bufw.write(line.toUpperCase());
<span style="white-space:pre"> </span>bufw.newLine();
<span style="white-space:pre"> </span>bufw.flush();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>bufr.close();
<span style="white-space:pre"> </span>bufw.close();
(6)将硬盘文件的数据打印到控制台
则只需将(4)中的
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
改为:
BufferedReader bufr = new BufferedReader(new InputStreamReader(new FileReader("g:\\demo.txt")));
即:
<span style="white-space: pre;"> </span> <span style="font-size:18px;">BufferedReader bufr = new BufferedReader(new InputStreamReader(new FileReader("g:\\demo.txt")));
<span style="white-space: pre;"> </span>BufferedWriter bufw = new BufferedWriter(new OntputStreamWriter(System.out));
<span style="white-space: pre;"> </span>String line = null;
<span style="white-space: pre;"> </span>while((line = bufr.readLine())!=null){
<span style="white-space: pre;"> </span>if("over".equals(line))
<span style="white-space: pre;"> </span>break;
<span style="white-space: pre;"> </span>bufw.write(line.toUpperCase());
<span style="white-space: pre;"> </span>bufw.newLine();
<span style="white-space: pre;"> </span>bufw.flush();
<span style="white-space: pre;"> </span>}
<span style="white-space: pre;"> </span>bufr.close();
<span style="white-space: pre;"> </span>bufw.close();</span>