IO流(输入流&输出流)
IO(Input Output)流
1:IO流用来处理设备之间的数据传输
2:Java对数据的操作是通过流的方式
3:Java用于操作流的对象都在IO包中
4: 流按操作数据分为两种:字节流和字符流。
5:流按流向分为:输入流,输出流。
输入流和输出流相对于内存中:输入
将内存的数据写入到外设中:输出。
把数据写入到内存当中,输入。
字符流的由来:
其实就是:字节流读取文字字节数据后,不直接操作而是先查指定的编码表。获取对应的文字。
在对这个文字进行操作。简单说:字节流+编码表。
字符流-FileWriter
IO流常用基类
1:字节流的抽象基类:
(1):InputStream, OutputStream。
2:字符流的抽象基类:
(1):Reader, Writer。
3:注:由这四个类派生出来的子类名称都是
以其父类名作为子类名的后缀。
(1)如:InputStream的子类FileInputStream。
(2)如:Reader的子类FileReader。
字节流的两个顶层父类:
1:InputStream 2:OutputStream
字符流的两个顶层父类:
1:Reader(读 往内存里面输入) 2:Writer(写 从内存里面写到硬盘里面 <输出>)
这些体系的子类都是以父类名作为后缀。
//需要:将一些文字存储到硬盘一个文件中。
记住:如果要操作文字数据,建议优先考虑字符流。
而且要将数据从内存写到硬盘上,要使用字符流中的输出流。Writer
硬盘的数据基本体现是文件,希望找到一个可以操作文件的Writer。
找到了FileWriter
public class Three1 {
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
public static void main(String[] args) throws IOException {
//创建一类可以往文件中写入字符数据的字符输出流对象。
/*
既然是往一个文件中写入文字数据,那么在创建对象时,就必须明确该文件(用于存储数据的目的地)
如果文件不存在,则会自动创建。
如果文件存在,则会被覆盖。
如果构造函数中加入true,可以实现对文件进行续写!
* */
FileWriter fw=new FileWriter("E:\\Io\\io.txt",true);
/*
调用Writer对象中的write(String)方法,写入数据。
其实数据写入到临时存储缓冲区中。
* */
fw.write("aaaa"+LINE_SEPARATOR+"hahahaha");
fw.write("zzzz");
/*
*进行刷新,将数据直接写入到目的地中。
* */
// fw.flush();
//关闭流,关闭资源。在关闭前会先调用flush刷新缓冲区中的数据到目的地
fw.close();
}
}
字符流-FileWriter-IO异常处理
public class Five1{
private static final String LINE_SEPARATOR = System.getProperty("LINE.SEPARATOR");
public static void main(String[] args) throws IOException {
//IO异常的基本处理
FileWriter fw=null;
try {
fw=new FileWriter("demo.txt",true);
fw.write("aaaa"+LINE_SEPARATOR+"zzzzz");
} catch (Exception e) {
if(fw!=null){
try {
fw.close();
} catch (Exception e2) {
throw new RuntimeException("关闭失败");
}
}
}
}
}
字符流-FileReader-读取方式一
//需求:读取一个文本文件,把内容输出到控制台
//找到了FileReader
public class Six1 {
public static void main(String[] args) throws IOException {
//1,创建读取字符流的对象
/*
在创建读取流文件时,必须要明确被读取的文件,一定要确定该文件是存在的。
*/
FileReader fr=new FileReader("E:\\Io\\io.txt");
/* //用Reader中的read方法读取字符。
int ch=fr.read();
System.out.println(ch);
int ch1=fr.read();
System.out.println((char)ch1);
fr.close();*/
//使用while循环遍历读取数据
int ch=0;
while ((ch=fr.read())!=-1){
System.out.println(ch);
//System.out.println((char)ch);
}
}
}
(字符流-FileReader-读取方式二)
public class Seven1 {
public static void main(String[] args) throws IOException {
FileReader fr=new FileReader("E:\\Io\\io.txt");
/*
使用read(char[])读取文本文件数据。
先创建字符数组。
* */
/*
char[] buf=new char[3];
int num=fr.read(buf);//将读取到的字符存储到数组中。
System.out.println(num+":"+new String(buf)); //结果1 3:abc
int num1=fr.read(buf);
System.out.println(num1+":"+new String(buf));//结果2 2:dec
int num2=fr.read(buf);
System.out.println(num2+":"+new String(buf));//结果3 -1:dec
*/
/*
*注意文本文件里面只有abcde这5个英文数据。
解释为什么 结果2和结果3的结果一样。
首先数组创建出来三个小空间,开始的时候,就给每个小空间
分别赋值上 a b c
然后,第二次读的时候,d e就分别覆盖 a b ,还有c 没有覆盖,所以c
的位置就不变
同理可以知道结果3,因为没有值啦,就覆盖不了,就保留原先的dec啦。
* */
char[] buf=new char[3];
int num=fr.read(buf);//将读取到的字符存储到数组中。
System.out.println(num+":"+new String(buf,0,num)); //结果1 3:abc 指定输出多少个文字
int num1=fr.read(buf);
System.out.println(num1+":"+new String(buf,0,num1));//结果2 2:de
/* char [] buf=new char[3];
int len=0;
while((len=fr.read(buf))!=-1){
System.out.println(new String(buf,0,len));
}*/
}
}
作业:将C盘中的一个文本文件复制到D盘。
分析:
复制原理:
读取C盘文件中的数据。
将这些数据写入到D 盘中。
连读带写。
思路:
1.需要读取源。
2.将读到的源数据写入到目的地。
3.既然是操作文本数据,使用字符流。
//方法1
public class IO08_1 {
public static void main(String[] args) throws IOException {
//1,读取一个已有的文本文件,使用字符流读取流和文件相关联。
FileReader fr =new FileReader("E:\\Io\\io.txt");
//2,创建一个目的,用于存储读到数据。
FileWriter fw=new FileWriter("E:\\Io\\ioCopy.txt");
//3,频繁的读写操作。
int ch=0;
while((ch=fr.read())!=-1){
System.out.println((char)ch);
fw.write((char)ch);
}
//4,关闭流资源。
fw.close();
fr.close();
}
}
//方法2
public class Io09_1 {
public static void main(String[] args) throws IOException {
FileReader fr=null;
FileWriter fw=null;
try {
fr=new FileReader("E:\\Io\\io.txt");
fw=new FileWriter("E:\\Io\\ioCopy1.txt");
char[] buf=new char[3];//这就是缓冲区
int ch=0;
while((ch=fr.read(buf))!=-1){
fw.write(buf, 0, ch);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(fr!=null){
fr.close();
}
} catch (Exception e2) {
throw new RuntimeException("关闭流失败");
}
try {
if(fw!=null){
fw.close();
}
} catch (Exception e2) {
throw new RuntimeException("关闭流失败");
}
}
}
}