文章目录
IO流原理一、流的分类二、流的体系结构三、具体代码分析3.1 FileReader字符流的使用3.1.1 read的简单使用3.1.2 总结说明3.1.3 read的重载方法
3.2 FileWriter字符流的使用3.2.1 writer方法的使用3.2.2 总结说明
3.3 文件的复制操作3.3.1 代码实现3.3.2 总结说明
3.4 FileInputStream的使用3.5 图片复制3.6 总结
四、流的转换4.1 处理流到缓冲流4.1.1 概述4.1.2 具体分类4.1.3 代码实现
4.2 处理流到转换流4.2.1 概述4.2.2 具体分类4.2.3 代码实现
4.3 其他流4.3.1 标椎的输入输出流4.3.2 代码实现
IO流原理
一、流的分类
操作数据单位:字节流、字符流数据流向:输入流、输出流流的角色:字节流、处理流
二、流的体系结构
抽象基类节点流(或文件流)缓冲流(处理流的一种)
InputStreamFileInputStreamBufferedInputStreamOutputStreamFileOutputStreamBufferedOutputStreamReaderFileReaderBufferedReaderWriterFileWriterBufferedWriter
三、具体代码分析
3.1 FileReader字符流的使用
3.1.1 read的简单使用
public class FileReaderWriterTest {
@Test
public void testFileReader() {
File file
=new File("hello");
FileReader fr
= null
;
try {
fr
= new FileReader(file
);
int data
;
while((data
=fr
.read())!=-1){
System
.out
.print((char)data
);
}
} catch (IOException e
) {
e
.printStackTrace();
}finally {
try {
if(fr
!=null
){
fr
.close();
}
} catch (IOException e
) {
e
.printStackTrace();
}
}
}
}
3.1.2 总结说明
read()的理解:返回读入的一个字符。如果达到文件末尾,返回-1。异常处理:为了保证资源一定可以执行关闭操作,需要使用try-catch-finally处理。读入的文件一定要存在,否则会报FileNotFoundException异常。
3.1.3 read的重载方法
public class FileReaderWriterTest {
@Test
public void testFileReader1(){
File file
=new File("hello");
FileReader fr
=null
;
try {
fr
=new FileReader(file
);
char [] tmp
=new char[5];
int len
;
while((len
=fr
.read(tmp
))!=-1){
String str
=new String(tmp
,0,len
);
System
.out
.print(str
);
}
} catch (IOException e
) {
e
.printStackTrace();
} finally {
try {
if(fr
!=null
){
fr
.close();
}
} catch (IOException e
) {
e
.printStackTrace();
}
}
}
}
3.2 FileWriter字符流的使用
3.2.1 writer方法的使用
public class FileReaderWriterTest {
@Test
public void testFileWriter() {
File file
=new File("helloworld");
FileWriter fw
=null
;
try {
fw
=new FileWriter(file
);
fw
.write("I have a dream!\n");
fw
.write("you need to have a dream!");
} catch (IOException e
) {
e
.printStackTrace();
} finally {
try {
if(fw
!=null
){
fw
.close();
}
} catch (IOException e
) {
e
.printStackTrace();
}
}
}
}
3.2.2 总结说明
输出操作对应文件可以不存在,并不会报异常File对应的硬盘中的文件如果存在:如果使用构造器:FileWriter(file,false)/FileWriter(file):对原有文件进行覆盖 - 如果使用构造器:FileWriter(file,true):不会对原有文件进行覆盖,而是在原有文件基础上追加内容。
3.3 文件的复制操作
3.3.1 代码实现
FileReader和FileWriter
public class FileReaderWriterTest {
@Test
public void testFileReaderWriter() {
File srcFile
=new File("helloworld");
File desFile
=new File("hello1");
FileReader fr
= null
;
FileWriter fw
= null
;
try {
fr
= new FileReader(srcFile
);
fw
= new FileWriter(desFile
);
char [] cbuf
=new char[5];
int len
;
while((len
=fr
.read(cbuf
))!=-1){
fw
.write(cbuf
,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();
}
}
}
}
3.3.2 总结说明
相当于是对一个文件进行复制不能去操作图片等字节数据
3.4 FileInputStream的使用
字节流
代码实现
public class FileInputOutputStreamTest {
@Test
public void teatFileInputStream() {
File file
=new File("hello");
FileInputStream fis
= null
;
try {
fis
= new FileInputStream(file
);
byte[] buffer
=new byte[5];
int len
;
while((len
=fis
.read(buffer
))!=-1){
String str
=new String(buffer
,0,len
);
System
.out
.print(str
);
}
} catch (IOException e
) {
e
.printStackTrace();
} finally {
try {
if(fis
!=null
){
fis
.close();
}
} catch (IOException e
) {
e
.printStackTrace();
}
}
}
}
3.5 图片复制
FileOutputStream
public class FileInputOutputStreamTest {
@Test
public void testFileInputOutputStream() {
File srcFile
=new File("1000610.jpg");
File desFile
=new File("2.jpg");
FileInputStream fis
= null
;
FileOutputStream fos
= null
;
try {
fis
= new FileInputStream(srcFile
);
fos
= new FileOutputStream(desFile
);
byte[] buffer
=new byte[5];
int len
;
while((len
=fis
.read(buffer
))!=-1){
fos
.write(buffer
,0,len
);
}
System
.out
.println("复制成功!");
} catch (IOException e
) {
e
.printStackTrace();
} finally {
try {
if(fos
!=null
){
fos
.close();
}
} catch (IOException e
) {
e
.printStackTrace();
}
try {
if(fis
!=null
){
fis
.close();
}
} catch (IOException e
) {
e
.printStackTrace();
}
}
}
}
3.6 总结
对于文本文件(.txt .java .c .cpp…),使用字符流处理。对于非文本文件(.jpg .mp3 .mp4 .avi .doc .ppt …)使用字节流处理。步骤:
创建文件创建流读或写数据关闭流
四、流的转换
4.1 处理流到缓冲流
4.1.1 概述
缓冲流在内部提供了一个缓冲区,会提高读写速率处理流,就是“套接”在已有流之上
4.1.2 具体分类
BufferedInputStream(字节流)BufferedOutputStream(字节流)BufferedReader(字符流)BufferedWriter(字符流)
4.1.3 代码实现
字节流
public class BufferedTest {
@Test
public void BufferedStreamTest() {
BufferedInputStream bis
= null
;
BufferedOutputStream bos
= null
;
try {
File srcFile
=new File("2.jpg");
File desFile
=new File("3.jpg");
FileInputStream fis
=new FileInputStream(srcFile
);
FileOutputStream fos
=new FileOutputStream(desFile
);
bis
= new BufferedInputStream(fis
);
bos
= new BufferedOutputStream(fos
);
byte[] buffer
=new byte[10];
int len
;
while((len
=bis
.read(buffer
))!=-1){
bos
.write(buffer
,0,len
);
}
System
.out
.println("复制成功!");
} 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();
}
}
}
}
字符流
public class BufferedTest {
}
@Test
public void testBufferedReaderBufferedWriter2(){
BufferedReader bf
= null
;
BufferedWriter bw
= null
;
try {
bf
= new BufferedReader(new FileReader(new File("hello")));
bw
= new BufferedWriter(new FileWriter(new File("hello3")));
char[] buffer
=new char[10];
int len
;
while((len
=bf
.read(buffer
))!=-1){
bw
.write(buffer
,0,len
);
}
String data
;
while((data
=bf
.readLine())!=null
){
bw
.write(data
);
bw
.newLine();
}
} catch (IOException e
) {
e
.printStackTrace();
} finally {
try {
if(bf
!=null
){
bf
.close();
}
} catch (IOException e
) {
e
.printStackTrace();
}
try {
if(bw
!=null
) {
bw
.close();
}
} catch (IOException e
) {
e
.printStackTrace();
}
}
}
}
4.2 处理流到转换流
4.2.1 概述
转换流属于字符流,将一个字节的输入流转换为字符的书输入流,将一个字符的输出流转换为字节的输出流。
4.2.2 具体分类
InputStreamReader:将一个字节的输入流转换为字符的输出流OutputStreamWriter:将一个字符的输出流转换为字节的输出流
4.2.3 代码实现
InputStreamReader的使用
public class InputStreamReaderTest {
@Test
public void test1() {
InputStreamReader isr
= null
;
try {
FileInputStream fis
=new FileInputStream("hello1");
isr
= new InputStreamReader(fis
,"UTF-8");
char [] cbuf
=new char[5];
int len
;
while((len
=isr
.read(cbuf
))!=-1){
String str
=new String(cbuf
,0,len
);
System
.out
.print(str
);
}
} catch (IOException e
) {
e
.printStackTrace();
} finally {
try {
if(isr
!=null
){
isr
.close();
}
} catch (IOException e
) {
e
.printStackTrace();
}
}
}
}
综合使用InputStreamReader和OutputStreamWriter
public class InputStreamReaderTest {
@Test
public void teat2(){
File file1
=new File("hello1");
File file2
=new File("hello_gbk");
InputStreamReader isr
= null
;
OutputStreamWriter osw
= null
;
try {
FileInputStream fis
=new FileInputStream(file1
);
FileOutputStream fos
=new FileOutputStream(file2
);
isr
= new InputStreamReader(fis
,"utf-8");
osw
= new OutputStreamWriter(fos
,"gbk");
char[] cbuf
=new char[5];
int len
;
while((len
=isr
.read(cbuf
))!=-1){
osw
.write(cbuf
,0,len
);
}
} catch (IOException e
) {
e
.printStackTrace();
} finally {
try {
if(isr
!=null
){
isr
.close();
}
} catch (IOException e
) {
e
.printStackTrace();
}
try {
if(osw
!=null
){
osw
.close();
}
} catch (IOException e
) {
e
.printStackTrace();
}
}
}
}
4.3 其他流
4.3.1 标椎的输入输出流
System.in:标准的输入流,默认从键盘输入System.out:标准的输出流,默认从控制台输出System类的setIn()/setOut()方式重新指定输入和输出流
4.3.2 代码实现
public class OtherStreamTest {
public static void main(String
[] args
) {
test1();
}
public static void test1() {
BufferedReader br
= null
;
try {
InputStreamReader isr
= new InputStreamReader(System
.in
);
br
= new BufferedReader(isr
);
while (true) {
System
.out
.println("请输入字符串:");
String data
= br
.readLine();
if (data
.equalsIgnoreCase("e") || data
.equalsIgnoreCase("exit")) {
System
.out
.println("程序退出!");
break;
}
String upperCase
= data
.toUpperCase();
System
.out
.println(upperCase
);
}
} catch (IOException e
) {
e
.printStackTrace();
} finally {
if (br
!= null
) {
try {
br
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
}
}
}