一、操作基本数据类型的流
DataInputStream
DataOutputStream
数据输出流允许应用程序以适当方式将基本java数据类型写入输出流中。然后,应用程序可以使用数据输入流将数据读入。
package a;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 操作基本数据类型的流
*/
public class DataInputStreamAndDatOutputStreamDemo {
public static void main(String[] args) throws IOException {
//写
writer();
//读
reader();
}
private static void reader() throws IOException {
//创建基本类型输入流
DataInputStream dis = new DataInputStream(new FileInputStream("dos.txt"));
//读基本类型的数据
int num = dis.readInt();
System.out.println("int类型的数据是:"+num);
short s = dis.readShort();
System.out.println("short类型的数据是:"+s);
byte b = dis.readByte();
System.out.println("byte类型的数据是:"+b);
double d = dis.readDouble();
System.out.println("double类型的数据是:"+d);
long l = dis.readLong();
System.out.println("long类型的数据是:"+l);
char c = dis.readChar();
System.out.println("char类型的数据是:"+c);
boolean bo = dis.readBoolean();
System.out.println("boolean类型的数据是:"+bo);
//关闭流
dis.close();
}
private static void writer() throws IOException {
//创建基本类型输出流
DataOutputStream dos = new DataOutputStream(new FileOutputStream("dos.txt"));
//输出基本类型的数据
dos.writeInt(2);
dos.writeShort(200);
dos.writeByte(2);
dos.writeDouble(2564231);
dos.writeLong(20L);
dos.writeChar('a');
dos.writeBoolean(true);
//关闭流
dos.close();
}
}
二、内存操作流
内存操作流一般用于处理临时信息,因为临时信息不需要保存,使用后就可以删除。
操作字节数组:
ByteArrayInputStream
ByteArrayOutputStream
操作字符数组
CharArrayReader
CharArrayWriter
操作字符串
StringReader
StringWriter
package a;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/*
* 字节数组输入流和字节数组输出流
* ByteArrayOutputStream和ByteArrayInputStream
*
* ByteArrayOutputStream:
* 实现了一个输出流,其中的数据被写入了一个byte数组。
* 缓冲区会随着数据的不断写入而自动增长。
* 但是注意的是,字节数组输出流是将数据输出到内存中,所以,构造方法没有提供文件,因为目的地是内存哦
* 但是,我们怎么获得写入到内存中的数据呢?
* 幸好,ByteArrayOutputStream提供了toByteArray()和 toString()方法来获取数据
*
*/
public class ByteArrayStream {
public static void main(String[] args) throws IOException {
//字节数组输出流对象
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//输出数据
baos.write("helloworld".getBytes());
//关闭流对象
baos.close();
//通过toByteArray()和 toString()方法来获取数据
byte[] b = baos.toByteArray();
System.out.println("通过toByteArray()来获得数据:"+new String(b));
System.out.println("通过toString()来获得数据:"+baos.toString());
System.out.print("通过字节数组输入流获得数组:");
//创建字节数组输入流对象
ByteArrayInputStream bais = new ByteArrayInputStream(b);
int by = 0;
while((by = bais.read()) != -1){
System.out.print((char)by);
}
}
}
三、打印流
打印流概述:
字节打印流:PrintStream
字符打印流:PrintWriter
打印流特点:
只能操作目的地,不能操作数据。
可以操作任意类型的数据。
如果启动了自动刷新,能够自动刷新。
可以操作文件的流
package a;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
/**
* 字节打印流
* PrintStream:字节打印流。使用字节输出流能够方便的打印出各种类型的数据
*
* PrintStream(OutputStream out, boolean autoFlush) 通过这个构造方法可以实现自动刷新
* autoFlush - boolean 变量;如果为 true,则每当写入 byte 数组、调用其中一个 println 方法或写入换行符或字节 ('\n') 时都会刷新输出缓冲区
*/
public class PrintStreamDemo {
public static void main(String[] args) throws IOException {
//创建字节打印流对象
PrintStream ps = new PrintStream(new FileOutputStream("ps"),true);
ps.write("helloworld".getBytes());
ps.write(2);
ps.print("你好");
ps.print(true);
ps.print('a');
ps.print(2.0);
ps.print(0.1f);
//释放资源
ps.close();
}
}
package a;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
/**
* 字节打印流
* PrintStream:字节打印流。使用字节输出流能够方便的打印出各种类型的数据
*
* PrintStream(OutputStream out, boolean autoFlush) 通过这个构造方法可以实现自动刷新
* autoFlush - boolean 变量;如果为 true,则每当写入 byte 数组、调用其中一个 println 方法或写入换行符或字节 ('\n') 时都会刷新输出缓冲区
*/
public class PrintStreamDemo {
public static void main(String[] args) throws IOException {
//创建字节打印流对象
PrintStream ps = new PrintStream(new FileOutputStream("ps"),true);
ps.write("helloworld".getBytes());
ps.write(2);
ps.print("你好");
ps.print(true);
ps.print('a');
ps.print(2.0);
ps.print(0.1f);
//释放资源
ps.close();
}
}
四、标准输入输出流
System类中有两个静态变量:in、out。
它们分别各自代表了系统标准的输入和输出设备。
默认输入设备是键盘,输出设备是显示器。
System.in的类型是InputStream。
System.out的类型时PrintStream。
五、随机访问文件
RandomAccessFile类不属于类,是Object的子类。
但是它融合了InputStream和OutputStream的功能,支持对随机访问文件的读取和写入。
package a;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* RandomAccessFile
* RandomAccessFile(String name, String mode)
* 创建从中读取和向其中写入(可选)的随机访问文件流,该文件具有指定名称。
* "r" 以只读方式打开。调用结果对象的任何 write 方法都将导致抛出 IOException。
* "rw" 打开以便读取和写入。如果该文件尚不存在,则尝试创建该文件。
* "rws" 打开以便读取和写入,对于 "rw",还要求对文件的内容或元数据的每个更新都同步写入到底层存储设备。
* "rwd" 打开以便读取和写入,对于 "rw",还要求对文件内容的每个更新都同步写入到底层存储设备。
* 一般使用rw模式。
*
*/
public class RandomAccessFileDemo {
public static void main(String[] args) throws IOException {
writer();
reader();
}
private static void reader() throws IOException {
RandomAccessFile rf = new RandomAccessFile("rf.txt","rw");
byte b = rf.readByte();
boolean bo = rf.readBoolean();
char c = rf.readChar();
String str = rf.readUTF();
System.out.println("int类型的数值:"+b);
System.out.println("boolean类型的数值:"+bo);
System.out.println("char类型的数值:"+c);
System.out.println("String类型的数值:"+str);
rf.close();
}
private static void writer() throws IOException {
RandomAccessFile rf = new RandomAccessFile("rf.txt","rw");
rf.writeByte(20);
rf.writeBoolean(true);
rf.writeChar('a');
rf.writeUTF("你好,世界");
rf.close();
}
}
int类型的数值:20
boolean类型的数值:true
char类型的数值:a
String类型的数值:你好,世界
六、合并流
sequenceInputStream类可以将多个输入流串流在一起,合并为一个输入流,因此,该流 也称为合并流。
构造方法:
public SequenceInputStream(InputStream s1,InputStream s2)
public SequenceInputStream(Enumeration<? extends InputStream> e)
package a;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.SequenceInputStream;
public class SequenceInputStreamDemo1 {
public static void main(String[] args) throws IOException {
InputStream is1 = new BufferedInputStream(new FileInputStream("a.txt"));
InputStream is2 = new BufferedInputStream(new FileInputStream("b.txt"));
OutputStream os = new BufferedOutputStream(new FileOutputStream("c.txt"));
SequenceInputStream sis = new SequenceInputStream(is1, is2);
byte[] b = new byte[1024];
int len = 0;
while((len = sis.read(b)) != -1){
os.write(b, 0, len);
}
sis.close();
os.close();
is1.close();
is2.close();
}
}
package a;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.SequenceInputStream;
import java.util.Vector;
public class SequenceInputStreamDemo2 {
public static void main(String[] args) throws IOException {
InputStream is1 = new BufferedInputStream(new FileInputStream("a.txt"));
InputStream is2 = new BufferedInputStream(new FileInputStream("b.txt"));
InputStream is3 = new BufferedInputStream(new FileInputStream("c.txt"));
OutputStream os = new BufferedOutputStream(new FileOutputStream("d.txt"));
Vector<InputStream> v = new Vector<InputStream>();
v.addElement(is1);
v.addElement(is2);
v.addElement(is3);
SequenceInputStream sis = new SequenceInputStream(v.elements());
byte[] b = new byte[1024];
int len = 0;
while((len = sis.read(b)) != -1){
os.write(b, 0, len);
}
sis.close();
os.close();
is1.close();
is2.close();
}
}
六、序列化流
序列化流:ObjectOutputStream
反序列化流:ObjectInputStream
对象序列化是将对象状态转换为可保持或传输的过程。一般的格式是与平台无关的二进制流,可以将这种二进制流持久保存在磁盘上,也可以通过网络将这种二进制流传输到另一个网络节点。
对象反序列化,是把这种二进制流数据还原为对象。
序列化
package a;
import java.io.Serializable;
public class Student implements Serializable{
private static final long serialVersionUID = -4300404356607362590L;
private String name;
private int age;
public Student(){}
public Student(String name,int age){
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
package a;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class ObjectStream {
public static void main(String[] args) throws IOException {
//创建序列化流对象
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("oos.txt"));
//创建学生对象
Student s1 = new Student("哈哈",2);
Student s2 = new Student("呵呵",3);
Student s3 = new Student("嘻嘻",4);
Student s4 = new Student("笨笨",5);
oos.writeObject(s1);
oos.writeObject(s2);
oos.writeObject(s3);
oos.writeObject(s4);
oos.close();
}
}
反序列化
package a;
import java.io.Serializable;
public class Student implements Serializable{
private static final long serialVersionUID = -4300404356607362590L;
private String name;
private int age;
public Student(){}
public Student(String name,int age){
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
package a;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class ObjectStream {
public static void main(String[] args) throws IOException {
//创建反序列化流对象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("oos.txt"));
Student ss1 = (Student) ois.readObject();
Student ss2 = (Student) ois.readObject();
Student ss3 = (Student) ois.readObject();
Student ss4 = (Student) ois.readObject();
System.out.println(ss1);
System.out.println(ss2);
System.out.println(ss3);
System.out.println(ss4);
ois.close();
}
}
Student [name=哈哈, age=2]
Student [name=呵呵, age=3]
Student [name=嘻嘻, age=4]
Student [name=笨笨, age=5]
如果不想某个属性被序列化,那么只要在属性前用transient修饰属性。
package a;
import java.io.Serializable;
public class Student implements Serializable{
private static final long serialVersionUID = -4300404356607362590L;
private String name;
private transient int age;
public Student(){}
public Student(String name,int age){
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
package a;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class ObjectStream {
public static void main(String[] args) throws Exception {
//创建序列化流对象
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("oos.txt"));
//创建学生对象
Student s1 = new Student("哈哈",2);
Student s2 = new Student("呵呵",3);
Student s3 = new Student("嘻嘻",4);
Student s4 = new Student("笨笨",5);
oos.writeObject(s1);
oos.writeObject(s2);
oos.writeObject(s3);
oos.writeObject(s4);
oos.close();
//创建反序列化流对象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("oos.txt"));
Student ss1 = (Student) ois.readObject();
Student ss2 = (Student) ois.readObject();
Student ss3 = (Student) ois.readObject();
Student ss4 = (Student) ois.readObject();
System.out.println(ss1);
System.out.println(ss2);
System.out.println(ss3);
System.out.println(ss4);
ois.close();
}
}
Student [name=哈哈, age=0]
Student [name=呵呵, age=0]
Student [name=嘻嘻, age=0]
Student [name=笨笨, age=0]
七、Properties集合
package a;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
public class PropertiesDemo {
public static void main(String[] args) {
Properties p = new Properties();
p.put("玉帝", "王母");
p.put("嫦娥", "后羿");
//遍历输出方式一
Set<Object> set = p.keySet();
for(Object o : set){
String str = (String) p.get(o);
System.out.println(o+":"+str);
}
//遍历输出方式二
Set<Map.Entry<Object,Object>> entrySet = p.entrySet();
for(Map.Entry<Object,Object> s : entrySet){
String key = (String) s.getKey();
String value = (String) s.getValue();
System.out.println(key+":"+value);
}
}
}
package a;
import java.util.Properties;
import java.util.Set;
/**
* Properties
* 特有功能:
* public Object setProperty(String key, String value)
* public String getProperty(String key)
* public Set<String> stringPropertyNames()
*/
public class PropertiesDemo {
public static void main(String[] args) {
Properties p = new Properties();
p.setProperty("玉帝", "王母");
p.setProperty("嫦娥", "后羿");
Set<String> keys = p.stringPropertyNames();
for(String key:keys){
System.out.println(key+":"+p.getProperty(key));
}
}
}
玉帝:王母
嫦娥:后羿
Properties和IO流的结合使用
package a;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Properties;
import java.util.Set;
/**
* public void load(Reader reader)
* public void store(Writer writer,String comments)
*/
public class PropertiesDemo2 {
public static void main(String[] args) throws Exception{
Properties p = new Properties();
p.setProperty("玉帝", "王母");
p.setProperty("嫦娥", "后羿");
p.store(new BufferedWriter(new FileWriter("p.properties")), "这是注释");
Properties p2 = new Properties();
p2.load(new FileReader("p.properties"));
Set<String> keys = p2.stringPropertyNames();
for(String key : keys){
System.out.println(key+":"+p2.getProperty(key));
}
}
}
玉帝:王母
嫦娥:后羿
转载于:https://blog.51cto.com/11841428/1869095