------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------
一:IO流概述
IO流用来处理设备之间的数据传输
Java对数据的操作是通过流的方式
Java用于操作流的对象都在IO包中
流按操作数据分为两种:字节流和字符流
流按流向分为输入流,输出流
IO流常用基类:
字节流的抽象基类:
InputStreanm OutputStream
字符流的抽象基类:
Reader Writer
(由这四个类派生出来的子类名称都是以其父类名作为子类名的后缀)
FileWriter:
write:将字符串写入到流中
flush:刷新流对象中的缓冲中的数据
close:关闭流资源,倒要先刷新
FileReader:
创建一个文件读写流对象,和指定名称的文件相关联
(要保证该文件是已经存在的,如果不存在会发生异常)
read:一次读一个字符,会自动往下读。如果已到达流的末尾,返回-1.
read(char[ ]):返回的是读到字符个数。如果已到达流额末尾,返回-1.
import java.io.*;
class J18_4
{
public static void main(String[] args)
{
FileWriter fw = null;
try
{
fw = new FileWriter("demo.txt",true);
fw.write("hoop\r\nzeng");
}
catch (IOException e)
{
System.out.println(e.toString());
}
finally
{
try
{
if(fw!=null)
fw.close();
}
catch (IOException e)
{
System.out.println(e.toString());
}
}
}
}
拷贝文本文件,练习代码:
//拷贝文本文件
import java.io.*;
class J18_7
{
public static void main(String[] args) throws IOException
{
//copy1();
//copy2();
}
static void copy1() throws IOException
{
FileReader fr = new FileReader("J18_6.java");
FileWriter fw = new FileWriter("J18_6_copy.txt");
int ch ;
while((ch=fr.read())!=-1)
fw.write(ch);
fr.close();
fw.close();
}
static void copy2()
{
FileReader fr = null;
FileWriter fw = null;
try
{
fr = new FileReader("J18_5.java");
fw = new FileWriter("J18_5_copy.txt");
char[] buf = new char[1024];
int len = 0;
while((len=fr.read(buf))!=-1)
fw.write(new String(buf,0,len));
}
catch (IOException e)
{
throw new RuntimeException();
}
finally
{
try
{
if (fr!=null)
{
fr.close();
}
}
catch (IOException e)
{
}
try
{
if (fw!=null)
{
fw.close();
}
}
catch (IOException e)
{
}
}
}
}
二:字符流缓冲区
缓冲区的出现提高了对数据的读写效率
对应类:
BufferedWriter
BufferedReader
缓冲区要结合流才可以使用
(关闭缓冲区,实质是关闭缓冲区中的流对象)
newLine():写入一个行分隔符
readLine():读取一个文本行(不包含任何行终止符)
练习代码:
//通过缓冲区复制文本文件
import java.io.*;
class J19_3
{
public static void main(String[] args)
{
BufferedReader br = null;
BufferedWriter bw = null;
try
{
br = new BufferedReader(new FileReader("J19_2.java"));
bw = new BufferedWriter(new FileWriter("J19_2_copy.txt"));
String s = null;
while((s=br.readLine())!=null)
{
bw.write(s);
bw.newLine();
bw.flush();
}
}
catch (IOException e)
{
throw new RuntimeException("读写失败");
}
finally
{
try
{
if(br!=null)
br.close();
}
catch (IOException e)
{
throw new RuntimeException("读取关闭失败");
}
try
{
if(bw!=null)
bw.close();
}
catch (IOException e)
{
throw new RuntimeException("写入关闭失败");
}
}
}
}
//测试文字
装饰设计模式:
当想要对已有的对象进行功能增强时,可以定义类。将已有对象传入,基于已有的功能,并提供加强功能。那么自定义的该类成为装饰类。
练习代码:
import java.io.*;
class MyBufferedReader extends Reader
{
private Reader fr;
MyBufferedReader(Reader fr)
{
this.fr = fr;
}
String myReadLine() throws IOException
{
StringBuilder sb = new StringBuilder();
int ch = 0;
while((ch=fr.read())!=-1)
{
if(ch=='\r')
continue;
if(ch=='\n')
return sb.toString();
sb.append((char)ch);
}
if(sb.length()!=0)
return sb.toString();
return null;
}
public int read(char[] cbuf,int off,int len) throws IOException
{
return fr.read(cbuf,off,len);
}
public void close() throws IOException
{
fr.close();
}
void myClose() throws IOException
{
fr.close();
}
}
class J19_4
{
public static void main(String[] args) throws IOException
{
FileReader fr = new FileReader("J19_3.java");
MyBufferedReader mbr = new MyBufferedReader(fr);
String s = null;
while((s=mbr.myReadLine())!=null)
System.out.println(s);
mbr.myClose();
}
}
三:字节流的缓冲区
BufferedOutputStream
BufferedInputStream
练习代码:
//拷贝图片
import java.io.*;
class J19_9
{
public static void main(String[] args)
{
FileInputStream fis = null;
FileOutputStream fos = null;
try
{
fis = new FileInputStream("D:\\1.jpg");
fos = new FileOutputStream("D:\\2.jpg");
byte[] buf = new byte[1024];
int len = 0;
while((len=fis.read(buf))!=-1)
{
fos.write(buf,0,len);
}
}
catch (IOException e)
{
throw new RuntimeException("读写失败");
}
finally
{
try
{
if(fis!=null)
fis.close();
}
catch (IOException e)
{
throw new RuntimeException("读取关闭失败");
}
try
{
if(fos!=null)
fos.close();
}
catch (IOException e)
{
throw new RuntimeException("写入关闭失败");
}
}
}
}
四:转换流
System.out:对应的是标准输出设备 控制台
System.in:对应的是标准输入设备 键盘
InputStreamReader:字节流通向字符流的桥梁
OutputStreamWriter:字符流通向字节流的桥梁
流操作的基本规律:
1,明确源和目的
2,操作的数据是否是纯文本
3,体系明确后,再明确要使用哪个具体对象
练习代码:
/*需求:
通过键盘录入数据
当录入一行数据后,就将该行数据进行打印
如果录入的数据是over,那么停止录入
*/
import java.io.*;
class J19_12
{
public static void main(String[] args) throws IOException
{
//InputStream in = System.in;
//InputStreamReader isr = new InputStreamReader(in);
//BufferedReader br = new BufferedReader(isr);
BufferedReader br = new BufferedReader
(new InputStreamReader(System.in));
//OutputStream os = System.out;
//OutputStreamWriter osw = new OutputStreamWriter(os);
//BufferedWriter bw = new BufferedWriter(osw);
BufferedWriter bw = new BufferedWriter
(new OutputStreamWriter(System.out));
String s = null;
while((s=br.readLine())!=null)
{
if(s.equals("over"))
break;
bw.write(s.toUpperCase());
bw.newLine();
bw.flush();
}
}
}