IO流
字节流输入:InputStream
字节流输出:OutputStream
字符流输入:Reader
字符流输出:Writer
由这四个父类派生出的子类的类名都以父类名为后缀结尾,如:
InputStream的子类:FileInputStream
Reader的子类:FileReader
InputStream:能够传输文字图片音频,由硬盘输入到内存
OutputStream:能够传输文字图片音频,由内存输出到硬盘
Reader:只对文字进行处理,将输入的字节流文字在缓冲区内转换成我们所能看懂的汉字
Writer:只对文字进行处理,将输出的字节流文字在缓冲区内转换成我们所能看懂的汉字,在关闭通道前要用flush();刷新缓冲区,否则缓冲区的内容不会传到硬盘
FileWriter
字符流输出:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class Liu {
public static void main(String[] args) {
File file=new File("D:\\a.txt");
FileWriter fe=null;
try {
fe=new FileWriter(file,true);//加true指定写入追加,不加true则为覆盖
fe.write("\n"+"他大舅大二舅都是他舅,高板凳低板凳都是木头!");
fe.flush();//刷新缓冲区
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fe!=null){//finally一定会执行,所以要确保写入的值不能为null,这样才能关闭流通道
try {
fe.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
FileReader
字符流输入:
import java.io.FileReader;
public class LiuRu {
public static void main(String[] args) {
FileReader ra = null;
try {
ra = new FileReader("D:\\a.txt");
int s = 0;
while ((s = ra.read()) != -1) {//char整数值在0-65535,若是到末尾就会返回-1,这里确保读出的字符在char值得范围内
System.out.print((char) s);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
边读边写(把出来的东西写在新在一个新建文件内)
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class DuXie {
public static void main(String[] args) {
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader("D:\\a.txt");//读取的目标文件
fw = new FileWriter("D:\\a1.txt");//新建的写入的目标文件
char[] ch = new char[1024];//创建一个数组来接收读取的信息
int length = 0;
while ((length = fr.read(ch)) != -1) {
fw.write(ch);//读取一次在新建文件中就写一次
fw.flush();//每写一次就刷新一次缓冲区
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
处理流
BufferedReader、BufferedWriter,这两个处理流并没有提供新的方法,但是它的底层提高了处理的效率
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class DuXiebuffer {
public static void main(String[] args) {
FileReader fr = null;
FileWriter fw = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
fr = new FileReader("D:\\a.txt");
br = new BufferedReader(fr);
fw = new FileWriter("D:\\a2.txt");
bw = new BufferedWriter(fw);
String str = null;
while ((str = br.readLine()) != null) {
bw.write(str);
bw.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
InputStream与OutputStream
用法与上面基本一样:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class INOut {
public static void main(String[] args) {
FileOutputStream os = null;
FileInputStream is = null;
try {
os = new FileOutputStream("D:\\a.jpg");
is = new FileInputStream("D:\\bizhi\\5b707a6b5ed1c08217b2ebe02832210f.jpg");
byte[] ch = new byte[1024];
int length = 0;
while ((length = is.read(ch)) != -1) {
os.write(ch, 0, length);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
BufferedInputStream与BufferedOutputStream
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class INOut {
public static void main(String[] args) {
FileOutputStream os = null;
FileInputStream is = null;
BufferedOutputStream bos=null;
BufferedInputStream bis=null;
try {
os = new FileOutputStream("D:\\c.jpg");
bos=new BufferedOutputStream(os);
is = new FileInputStream("D:\\bizhi\\5b707a6b5ed1c08217b2ebe02832210f.jpg");
bis=new BufferedInputStream(is);
byte[] ch = new byte[1024];
int length = 0;
while ((length = bis.read(ch)) != -1) {
bos.write(ch, 0, length);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
对象持久化
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class DuXiang {
public static void main(String[] args) {
Persons pos = new Persons();
pos.setName("sb");
pos.setAge(20);
pos.setNumber("021655487842");
pos.setXb("女");
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("D:\\sb.obj"));
oos.writeObject(pos);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}//对象持久化,生成序列化文件;持久化对象的类要实现 Serializable(空接口)接口,意在告诉机器此文件可被持久化
ObjectInputStream ois=null;
try {
ois = new ObjectInputStream(new FileInputStream("D:\\sb.obj"));
Object s=ois.readObject();
Persons p=(Persons)s;
System.out.println(p.getAge());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ois!=null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}//反序列化,读取序列化文件信息
//若文件信息不想被读取,可在属性前加static或者加transient,信息就不会被持久化
}
}