一、输入流和输出流
1、输入流:抽象类InputStream,所有的输入流都继承该类
int read() 从文件读取8位字节,转为整数,并返回整数
int read(byte[] b) 从文件中读取若干字节保存在b数组中,并返回整数表示读取的字节数
int read(byte[] b,int off,int len) 读取若干字节,保存在b中,off表示保存数据的起始下标,len表示读取的字节数目
void close() 关闭输入流
2、输出流:抽象类OutputStream,所有的输出流都继承该类
void write(int b) 向输出流写入一个字节
void write(byte[] b) 把参数b指定的字节数组中的数据写到输出流
void write(byte[] b,int off,int len) 把参数b指定的字节数组中的数据按下标off和长度len写入输出流
void flush() 刷新输出流,强制写出缓冲流中的所有字节
void close() 关闭输出流
二、字节输入流和字节输出流
1、字节流读取文件:FileInputStream
public static void main(String[] args) throws Exception{
FileInputStream in=new FileInputStream("example.txt");
int b; //读取到的数据类型为int类型
while(true){
b=in.read(); //将字节输入的数据临时储存在变量b中
if(b==-1)
break;
System.out.println(b); // 将变量b打印出来
}
in.close();
}
2、字节流输出文件:FileOutputStreasm
//使用OutputStream中的write()方法直接向文件中写入数据会把文件中的原有数据清除掉
//如果不想删除文件中之前的数据,则可以使用构造方法来进行输出
public static void main(String[] args) throws Exception{
FileOutputStream out=new FileOutputStream("example.txt");//会清除原有数据
String str="我是呆呆";
byte[] b=str.getBytes(); //将字符串转化为字节数组
for (int i=0;i<b.length;i++){
out.write(b[i]); //方式一 //将字节数组中储存的数据循环输出到文件中
}
out.write(b); //方式二 //直接写入数据
out.close();
}
//使用OutputStream 的构造方法来进行输出数据到文件中去 ,则不删除文件中原有的数据
public static void main(String[] args) throws Exception{
FileOutputStream out = new FileOutputStream("example.txt",true);//保留原有数据
String str="我是呆呆";
byte[] b=str.getBytes();
out.write(b);
out.close();
}
3、文件的拷贝:FileInputStream+FileOutputStream
//通过while循环对文件进行逐个字节的读取和写入,效率较低
public static void main(String[] args) throws Exception {
FileInputStream in = new FileInputStream("Test1\\example.txt");//创建输入流
FileOutputStream out = new FileOutputStream("Test2\\example.txt");//创建输出流
int b;
while (true) {
b = in.read();
if (b == -1)
break;
out.write(b);
}
System.out.println("拷贝成功!");
in.close();
out.close();
}
4、创建字节流的缓冲区:byte[] arr=new byte[1024];
//创建一个字节缓冲流对文件进行读写,效率较高
public static void main(String[] args) throws Exception{
FileInputStream in=new FileInputStream("Test1\\example.txt");//创建输入流
FileOutputStream out=new FileOutputStream("Test2\\example.txt");//创建输出流
int b;
byte[] arr=new byte[1024]; //创建一个大小为1024字节的字节数组作为缓冲区
while((b=in.read(arr))!=-1){
out.write(arr,0,b);//从第一个字节开始向文件中写入b个字节,每次写入arr个
}
System.out.println("拷贝成功!");
in.close();
out.close();
}
5、字节缓冲流:BufferedInputStream+BufferedOutputStream
//使用装饰设计模式,构造方法分别接收InputStream和OutputStream的参数作为被包装对象
//在读写时提供缓冲区,每个缓冲区大小为8192字节
public static void main(String[] args) throws Exception{
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("Test1\\example.txt")); //包装输入类
BufferedOutputStream bos =new BufferedOutputStream(new FileOutputStream("Test2\\des.txt")); //包装输出类
int len;
while((len=bis.read())!=-1){
bos.write(len);
}
bis.close();
bos.close();
System.out.println("拷贝成功!");
}
三、字符输入流和字符输出流
1、字符输入流:FileReader
/*
1、一次性输入文件中的所有数据,为int类型
2、再逐个转为char类型进行打印
*/
public static void main(String[] args) throws Exception {
FileReader in = new FileReader("example.txt"); //创建字符输入流
int b;
while ((b = in.read()) != -1) {
System.out.print((char) b); //强转并打印
}
in.close();
}
2、字符输出流:FileWriter
public static void main(String[] args) throws Exception{
FileWriter out=new FileWriter("example.txt",true); //不删除原有数据
String str="\n想,我该怎么办?\n";
out.write(str);
out.close();
System.out.println("写入成功!");
}
3、按行拷贝数据:BufferedReader+BufferedWriter
public static void main(String[] args) throws Exception {
FileReader in = new FileReader("example.txt");
BufferedReader br = new BufferedReader(in);//创建缓冲对象
FileWriter out = new FileWriter("example01.txt", true);
BufferedWriter bw = new BufferedWriter(out);//创建缓冲对象
String str;
while ((str = br.readLine()) != null) {
bw.write(str);
bw.newLine(); //根据不同类型的操作系统写入相对应的换行符
}
br.close();
bw.close();
}
4、写入行号
public static void main(String[] args) throws Exception {
FileReader in = new FileReader("example.txt");
FileWriter out = new FileWriter("example.txt", true);
LineNumberReader lr = new LineNumberReader(in);//对字符输入流进行包装
lr.setLineNumber(0); //设置开始读取的行号
String line = null;
while ((line = lr.readLine()) != null) {
out.write(lr.getLineNumber() + " " + line); //将行号写入到文件中
out.write("\r\n");
}
lr.close();
out.close();
}
四、转换流:InputStreamReader+OutputStreamWriter
//过程:
//创建字节输入流-转换-包装
//创建字节输出流-转换-包装
//注明:只可用于文本类文件的转换,用于其他文件的转换会丢失数据
public static void main(String[] args) throws Exception {
FileInputStream in = new FileInputStream("example.txt");
InputStreamReader intemp1 = new InputStreamReader(in);//转换为字符输入流
BufferedReader intemp2 = new BufferedReader(intemp1);//将字符输入流进行包装
FileOutputStream out = new FileOutputStream("example.txt", true);
OutputStreamWriter outtemp1 = new OutputStreamWriter(out);//转换为字节输出流
BufferedWriter outtemp2 = new BufferedWriter(outtemp1);//将字符输出流进行包装
String str;
while ((str = intemp2.readLine()) != null) {
outtemp2.write(str);
outtemp2.newLine();//根据不同操作系统输出相对应的换行符
}
intemp2.close();
outtemp2.close();
}
五、对象流:ObjectInputStream+ObjectOutputStream
//将对象序列化并输出到硬盘中
//必须先实现Serializable接口
//保存在文件中的是一个二进制路径
//例如 IO流.储存对象.将对象序列化.Person@18bf509
public static void main(String[] args) throws Exception {
Person personout = new Person();
System.out.println("-------文件写入之前------");
System.out.println("姓名:" + personout.getName() + " 年龄:" + personout.getAge() + " 手机号:" + personout.getPhone());
FileOutputStream out = new FileOutputStream("Person.txt"); //先创建一个字节输出流
ObjectOutputStream objout = new ObjectOutputStream(out); //然后转化为对象输出流
objout.writeObject(personout); //调用writeObject()方法将对象进行序列化输出
System.out.println("已将对象按二进制的格式进行序列化输出!");
System.out.println("************分割线***********");
FileInputStream in = new FileInputStream("Person.txt"); //创建一个字节输入流
ObjectInputStream objin = new ObjectInputStream(in); //将字节输入流转化为对象输入流
Person personin = (Person) objin.readObject(); //将输入的对象强转为所需要的对象类型
System.out.println("该对象保存的地址为:" + personin);
System.out.println("姓名:" + personin.getName() + " 年龄:" + personin.getAge() + " 手机号:" + personin.getPhone());
}
class Person implements Serializable { //实现Serializable接口
private String name = "呆呆";
private int age = 20;
private long phone = 456123789;
public Person() {
}
public Person(String name, int age, long phone) {
this.name = name;
this.age = age;
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public long getPhone() {
return phone;
}
public void setPhone(long phone) {
this.phone = phone;
}
}
六、成员数据流:DataInputStream+DataOutputStream
//读取数据时,要按照添加顺序中的数据类型进行读取,并且每次读取一行
public static void main(String[] args) throws Exception {
FileOutputStream out = new FileOutputStream("Data.txt");
BufferedOutputStream outtemp = new BufferedOutputStream(out); //创建缓冲区
DataOutputStream outdata = new DataOutputStream(outtemp);
outdata.writeInt(123);
outdata.writeInt(13456);
outdata.writeByte(123); //输出不同类型的数据
outdata.writeChar('1');
outdata.writeBoolean(true);
outdata.writeUTF("呆呆");
outdata.close();
System.out.println("数据写入成功!");
FileInputStream in = new FileInputStream("Data.txt");
BufferedInputStream intemp = new BufferedInputStream(in); //创建缓冲区
DataInputStream indata = new DataInputStream(intemp);
System.out.println("数据读取成功!");
System.out.println(indata.readInt());
System.out.println(indata.readInt());
System.out.println(indata.readByte()); //读取不同类型的数据
System.out.println(indata.readChar());
System.out.println(indata.readBoolean());
System.out.println(indata.readUTF());
indata.close();
}
七、打印流:PrintStream
//需要重写toString()方法
//将基本数据类型或者引用数据类型转化为字符串进行打印输出
import java.io.*;
public class Demo01 {
public static void main(String[] args) throws Exception {
//创建一个打印流,传入FileOutStream对象 ,会把数据输出在该对象中去
PrintStream ps = new PrintStream(new FileOutputStream("example01.txt"), true);
int num = 19;
Student stu = new Student();
ps.println("这是一个数字:" + num);
ps.println(stu);
ps.close();
System.out.println("打印输出完成!");
}
}
class Student {
private String name = "呆呆";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override //重写toString()方法
public String toString() {
return getName();
}
}
八、标准输入输出流:System.in、System.out、System.err
九、管道流:PipedInputStream+PipedOutputStream
十、临时存储流:
-
字节型临时存储:ByteArrayInputStream+ByteArrayOutputStream
-
字符型临时存储:CharArrayInputStream+CharArrayOutputStream