java io流与文件

2018-07-31  本文已影响0人  pinnuli

一、流

读写字节
InputStream.read和OutpueStream.write
组合流过滤器

FileInputStream fin = new FileInputStream("test.txt");
DataInputStream din = new DataInputStream(fin);
double s = din.readDouble();
DataInputStream din = new DataInputStream(
    new BUfferInputStream(
        new FileInputStream(test.txt")));
PushbackInputStream pbin = new PushbackInputStream(
    new BUfferInputStream(
        new FileInputStream(test.txt")));
//预读写一个字节
int b = pbin.read();
//不是所期望时将其推回流中
if(b != '<') pbin.unead(b);

二、文本输入与输出

输出:PrintWrite
PrintWrite out = new PrintWrite("test.txt");等同于PrintWrite out = new PrintWrite(new FileWrite("test.txt"));
输入:Scanner
文本格式存储对象
用自己的格式依次存储各个字段,以特定字符分隔,如:PINUULI|201625010417|1997|guangdong


三、读写二进制数据

:实现DataInput接口,如DataInputStream,readInt,readBoolean等方法
:实现DataOutput接口,如DataOutputStream,writeInt,writeBoolean等方法
随机访问文件RandomAccessFile
可以在文件中的任何位置查找或者写入数据:

RandomAccessFile in = new RandomAccessFile("test.txt","r");
RandomAccessFile inOut = new RandomAccessFile("test.txt","rw");

四、ZIP文件

每个zip文档都有一个头,包含注入给个文件名字和使用的压缩方法等信息。
ZipInputStream

ZipInputStream zin = new ZipInputStream(new FileInputStream("test.zip"));
ZipEntry entry;
Scanner in = new Scanner(zin);
while((entry = zin.getNextEntry()) != null){
    while(in.hasNextLine()){
        System.out.println(in.nextLine());
    }
    zin.closeEntry();
}

ZipOutputStream

FileOutputStream fout = new FilePutputStream("test.zip");
ZipOutputStream zout = new ZipOutputStream(fout);

// 写一个文件
ZipEntry ze = new ZipEntry("filename");
zout.putNextENtry(ze);
send data to zout;
zout.closeEntry();

zout.close();

五、对象流与序列化

一些数据域是不可序列化,或者没必要序列化的,比如只对本地方法有意义的窗口句柄的整数值,重新加载或者传送到其他机器上都没有用,那么就可以将他们标记成是transient,这些域在序列化时就会被跳过。
可以把一些域存储为你想要的格式,想要为默认的读写行文添加验证时。

当你只是想跳过一些域,或者想将这些域保存为你想要的格式,而大部分域依然按照默认的格式保存时,可以仍然实现Serializable接口,将那些数据域标记成transient,读写时调用默认的读写方法之后,再做自己想要的处理,如:

public class LabeledPoint implements Serializable{
    private String label;

    //对于类LabeledPoint,point不能序列化,那么标志成transient,序列化时就会被跳过,之后存储点的坐标
    private transient Point2D.Double point;
    ···
    //重写读写方法
    private void writeObject(ObjectOutputStream out) throws Exception{
        out.defaultWriteObjecy();
        out.writeDouble(point.getX());
        out.writeDouble(point.getY());
    }

    private void readObject(ObjectInputStream in) throws IOException{
        in.defaultReadObject();
        double x = in.readDouble();
        double y = in.readDouble();
        point = new Point2D.DOuble(x,y);
    }
}

当你只需要保存一部分域时,使用transient关键字就有点麻烦,那么可以通过实现Externalizable接口,指定要保存的域

public class Student implements Externalizable{
    private String name;
    private String stuId;
    private int age;
    ···
    //重写读写方法
    private void writeExternal(ObjectOutput out) throws IOException{
        out.writeUTF(name);
        out.writeInt(age);
    }

    private void readExternal(ObjectInput in) throws IOException{
        name = in.readUTF():
        age = in.readInt();
    }
}
> PS:readObjecty和writeObject方法时私有的,只有被序列化机制调用,在流中只记录该对象所属的类,而readExternal/writeExternal方法时公共的,而且对包括超类数据在内的整个对象的存储和回复负责。

六、操作文件

Path

Path absolutye = Paths.get("/home"."cay");
Path relative = Paths.get("myprog","conf","user.properties");

读写文件:Files类可以使得普通文件操作变得快捷

参阅:
慕课网:文件传输基础——Java IO流
java核心技术 卷II:高级特性

上一篇 下一篇

猜你喜欢

热点阅读