概要:IO流思维导图
目录
四、OutputStreamWriter 类与 InputStreamReader类
1.DataOutputStream ,是 OutputSream 的子类。
2.DataInputStream,是InputStream 的子类。
1、ObjectOutStream对象输出流,是 OutputSream 的子类。
2、ObjectInputStream对象输入流,是InputStream 的子类。
一、File类
File类可以可以进行,创建、删除文件等操作。(参考文档JDK8)
使用方法:
import java.io.File
import java.io.IOException
public class FileDemo01{
public static void main(String args[]){
File f = new File("d:"+File.separator+"test.txt"); //必须给出完整路径
try{
f.createNewFile(); //创建文件
}catch(IOException e){
e.printStackTrace;
}
}
}
【注意】
Windows 中用反斜杠表示目录的分隔符 “ \ ”,Linux 中使用正斜杠表示目录的分隔符 “ / ”。在操作文件中一定要使用 File.separator 表示分隔符,因为发布时往往在Liunx上部署,所以要注意。
二、字节流
字节流主要操作 byte 类型数据,以 byte 数组为准,主要的操作类是OutputStream 和 InputStream 类。
1、字节输出流:OutputStream
OutputStream是一个抽象类,如果要使用此类,则首先必须通过子类 FileOutputStream来实例化。
使用方法:
File f = new File("d:"+File.separator+"test.txt"); //声明File对象
OutputStream out = null; //准备好一个输出的对象
out = new FileOutputStream(f); //通过对象多态性,进行实例化
String str = "Hello World!!!"
byte b[] = str.getBytes(); //只能输出byte数组,所以将字符串变为 byte 数组
out.write(b); //输出内容,保存文件
out.close(); //关闭输出流
2、字节输入流:InputStream
InputStream是一个抽象类,如果要使用此类,则首先必须通过子类 FileInputStream来实例化。
使用方法:
File f = new File("d:"+File.separator+"test.txt"); //声明File对象
InputStream input= null; //准备好一个输入的对象
input = new FileInputStream(f); //通过对象多态性,进行实例化
byte b[] = new byte[1024]; //所以的内容读到此数组当中
input.read(b); //取出内容,内容读到byte数组当中
input.close(); //关闭输出流
System.out.println("内容为:"+new String(b)); //把byte数组变为字符串输出
三、字符流
一个字符等于两个字节,JAVA 提供了 Reader 和 Writer 两个字符流类。
1、字符输出流Writer
Writer 是一个抽象类,如果要使用此类,则首先必须通过子类 FileWriter 来实例化。
使用方法:
File f = new File("d:"+File.separator+"test.txt"); //声明File对象
Writer out = null; //准备好一个输出的对象
out = new FileWriter (f); //通过对象多态性,进行实例化
String str = "Hello World!!!"
out.write(str); //输出内容
out.close(); //关闭输出流
【注意】
总体流程和OutputStream差不多,唯一的好处是,可以直接输出字符串,而不是将字符串变为byte数组之后再输出。
2、字符输入流Reader
Reader 是一个抽象类,如果要使用此类,则首先必须通过子类 FileReader 来实例化。
使用方法:
File f = new File("d:"+File.separator+"test.txt"); //声明File对象
Reader re = null; //准备好一个输入的对象
re = new FileReader (f); //通过对象多态性,进行实例化
char[] c = new char[1024]; //所以内容读到此数组当中
int len = re.read(c); //输出内容
re.close(); //关闭输出流
System.out.println("内容为:"+new String(c,0,len));
三、字节流 和 字符流 的区别
实际上字节流在操作时本身不会用到缓冲区(内存),是文件本身直接的操作,而且字符流在操作时使用了缓冲区,再通过缓冲区再操作文件。不关闭字节流,内容依旧写到了文件;不关字符流,内容还在缓存当中,没有写道文件。flush() 方法:强制清空缓冲区内容。
四、OutputStreamWriter 类与 InputStreamReader类
字节流-字符流的转换类,分别是Writer和Reader的子类。
字节输出转为字符输出:
File f = new File("d:"+File.separator+"test.txt"); //声明File对象
Writer out = null; //准备好一个输出的对象
out = new OutputStreamWriter (new FileOutputStream(f)); //通过对象多态性,进行实例化
out.write("Hello World!!!");
out.close();
字节输入转为字符输入:
File f = new File("d:"+File.separator+"test.txt"); //声明File对象
Reader re = null; //准备好一个输出的对象
re = new InputStreamReader(new FileInputStream(f)); //通过对象多态性,进行实例化
char c[] = new char[1024];
int len = re.read(c);
re.close();
System.out.println(new String(c,0,len));
五、内存操作流
ByteArrayInputStream / ByteArrayOutputStream
使用方法:
String str = "HelloWorld!!!";
ByteArrayInputStream bis = null; //声明一个内存输出流
ByteArrayOutputStream bos = null; //声明一个内存输入流
bis = new ByteArrayInputStream (str.getBytes());
bos = new ByteArrayOutputStream ();
System.out.println(bos.toString());
六、打印流
PrintStream 是 OutputStream 的子类
使用方法:
PrintStream ps = null;
ps = new PrintStream(new FileOutputStream(new File("d:"+File.separator+"test.txt")));
ps.print("hello");
ps.close();
七、System类对IO的支持
(1) System.out 是PrintStream的对象,PrintStream的方法他也可以直接用。
(2) System.err 错误信息输出。
(3) System.in 是键盘输入流,是InputStream的对象。
(4) 输入 / 输出 重定向 System.setOut:定义重定向的位置;System.out:输出到指定位置,而不是向屏幕输出了。
八、BufferedReader 类
用于从缓冲区读取内容,所有的输入字节数据都将放在缓冲区中
九、Scanner 类
jdk1.5之后Java提供了专门的输入数据类,也方便于对输入数据的验证。
使用方法:
Scanner scan = new Scanner(System.in);
System.out.println("请输入数据...");
String str = scan.next();
十、数据操作流
1.DataOutputStream ,是 OutputSream 的子类。
使用方法:
File f = new File("d:"+File.separator+"test.txt");
dos = new DataOutputStream(new FileOutputStream(f));
String str = new String("hello world!!!");
dos.writeChar(str);
dos.close();
2.DataInputStream,是InputStream 的子类。
使用方法:
File f = new File("d:"+File.separator+"test.txt");
dis= new DataInputStream(new FileInputStream(f));
dis.readChar();
dis.close();
在使用数据输入流读取时,要使用readChar()来读取“\t” 分隔符,“\n”换行符。
十一、对象操作流(序列化)
如过一个类的对象想被序列化,需要实现java.io.Serializable接口。序列化就是把一个对象变为二进制的数据流。 使用对象输出输入序列化对象的步骤有时也称为序列化。
【注意】
进行序列化和反序列化操作时,需要考虑JDK版本,版本不一致可能会导致异常,所以在序列化操作当中 serialVersionUID 常量可以验证版本的一致性。
1、ObjectOutStream对象输出流,是 OutputSream 的子类。
此类使用形式于 PrintStream 非常相似,在实例化时也需要传入一个OutputStream 的子类对象。
使用方法:
File f = new File("d:"+File.separator+"test.txt");
ObjectOutputStream oos = null;
OutputStream out = new FileOutputStream(f); //文件输出流
oos = new ObjectOutputStream(out); //对象输出流实例化
oos.writeObject(new User("张三",20));
oos.close();
一个对象被序列化后,只有属性被序列化,方法是不能被序列化的。
2、ObjectInputStream对象输入流,是InputStream 的子类。
此类可以把序列好的对象反序列化,此类使用形式于 PrintStream 类似。此类同样需要接收InputStream 类的实例才可以实例化。
使用方法:
File f = new File("d:"+File.separator+"test.txt");
ObjectInputStream ois = null;
InputStream input = new FileInputStream(f);
ois = new ObjectOutputStream(input);
Object obj = ois.readObject();
oos.close();
System.out.println(obj);
3、Externalizable接口
被 Externalizable 接口声明的类的对象的内容将被序列化,它是 Serializable接口的子接口,在其中定义了两个方法。接口继承 DataOutput 和 DataInput 。
(1)writeExternal(ObjectOutput out):指定要被保存的信息,对象序列化时调用。
(2)readExternal(ObjectInput in):读取被保存的信息,对象反序列化时调用。
Externalizable 和 Serializable 区别
4.transient关键字
Serialization 接口实现的操作实际上是将一个对象中全部属性进行序列化,当然也可以使用Externalizable接口以实现部分属性的序列化,但这样操作比较麻烦。
当使用 Serialization 接口实现序列化操作时,如果一个对象中的某个属性不希望被序列化,则可以使用transient 关键字进行声明,只需要在属性的前面加上transient关键字。
public class Person implements Serializable{ //此类对象可以被序列化
private transient String name; //此属性将不被序列化
private int age; //此属性将被序列化
}
基本上的内容就是这些啦。