JAVA流_文件输入输出流_缓存读写_序列化和反序列化

java流

输入流

InputStream类

  1. int read()
  2. void close()
  3. int available()
  4. skip(long n)
  5. boolean markSupported(),void mark(int readLimit),void reset()
FileStream类(InputStream的子类)

读文件

package tupnimaerts;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Read {
    public static void main(String[] args) throws FileNotFoundException {
        String fileName = "D:\\aa\\dd.txt";     // 文件名

        FileInputStream fStream = new FileInputStream(fileName); // 获得文件输入对象

        try {
            int ch = fStream.read();
            while (ch != -1){
                System.out.print((char)ch);
                ch = fStream.read();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

输出流

OutStream类

复制一个文件(字节流)
package tupnimaerts;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Copy {
    public static void main(String[] args) {
        String fileNameIn= "D:\\aa\\dd.txt";
        String fileNameOut= "D:\\aa\\ddCopy.txt";

        try {
            FileInputStream fin = new FileInputStream(fileNameIn);
            FileOutputStream fout = new FileOutputStream(fileNameOut);
            int ch = fin.read();
            while(ch != -1){
                fout.write(ch);
                ch = fin.read();
            }
            System.out.println("写出成功");
            fout.close();
            fin.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
复制一个文件(缓冲区)
package tupnimaerts;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Copy {
    public static void main(String[] args) {
        String fileNameIn= "D:\\aa\\ee.ppt";        // 打开的文件
        String fileNameOut= "D:\\aa\\eeCopy.ppt";   // 写出的文件

        try {
            FileInputStream fin = new FileInputStream(fileNameIn);
            FileOutputStream fout = new FileOutputStream(fileNameOut);

            byte[] buff = new byte[1024];       // 缓冲区
            int len = fin.read(buff);
            while(len != -1){
                fout.write(buff,0,len);
                len = fin.read(buff);
            }
            System.out.println("写出成功");
            fout.close();
            fin.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
拷贝较大的文件的时候尽量避免一个个字节读入,操作磁盘的过程是很慢的,直接读入一个扇区可以很大程度上加快拷贝的速度。

序列化和反序列化

package tupnimaerts;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

class St implements Serializable{
    // 需要实现序列化接口
    // 但是实际上什么也没干
    private String xm;
    private int nl;

    String getXm() {
        return xm;
    }

    int getNl() {
        return nl;
    }

    St(String xm, int nl) {
        this.xm = xm;
        this.nl = nl;
    }
}

public class ObjSave {
    public static void main(String[] args) {
        // 序列化对象 把对象写入到文件中
        // 反序列化对象 把文件中的对象读到文件中
        St st1 = new St("zs",19);
        St st2 = new St("ww",20);
        String fileName = "D:\\aa\\st.dat";
        List<St> stList = new ArrayList<>();
        try {
            FileOutputStream fout = new FileOutputStream(fileName);
            ObjectOutputStream oout = new ObjectOutputStream(fout);
            oout.writeObject(st1);
            oout.writeObject(st2);
            oout.close();// 先关闭处理流
            fout.close();// 再关闭节点流

            FileInputStream fin = new FileInputStream(fileName);
            ObjectInputStream oin = new ObjectInputStream(fin);

            while(true){
                try{
                    St t = (St)oin.readObject();
                    stList.add(t);
                }catch (java.io.EOFException e){
                    break;
                }
                // 系统并没有提供判断文件尾的函数,所以遇到文件尾之后会抛出文件IO异常
            }
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        for (St x : stList){
            System.out.println(x.getXm() + " " + x.getNl());
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值