IO流2

字符流

字符流的读,第一种

public static String read(String path) throws IOException {
    FileReader fr = new FileReader(path);
    int temp = 0;
    StringBuilder sb = new StringBuilder();
    while (-1 != (temp = fr.read())) {
        sb.append((char) temp);
    }
    fr.close();
    return sb.toString();

第二种

char[] c = new char[(int) new File(path).length()];
fr.read(c);
int i = 0;
for (i = 0; i < c.length; i++) {
    if (c[i]=='\u0000'){
        break;
    }
}
fr.close();
return new String(c,0,i);

public static void write(String path,String str,boolean isAppend)throws IOException{
    FileWriter fw = new FileWriter(path,isAppend);
    char[] c = new char[(int) new File(path).length()];
    fw.write(c);
    fw.close();
}

public static void main(String[] args) {
    try {
        String s = read("a.txt");
        System.out.println(s);
        write("a.txt",s,true);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Buffered类的读写

在这里插入图片描述

public static String readBuffer(String path) throws IOException {
    FileReader fr = new FileReader(path);
    BufferedReader br = new BufferedReader(fr);
    String temp;
    StringBuffer sb = new StringBuffer();
    while (null!=(temp=br.readLine())){
        sb.append(temp+"\n");
    }
    //后开先关
    br.close();
    fr.close();
    return sb.toString();
}


public static void writeBuffer(String path,String str,boolean isAppend) throws IOException{
    FileWriter fw = new FileWriter(path);
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write(str);
    bw.close();
    fw.close();
}


public static void main(String[] args) {
    try {
        String s = readBuffer("a.txt");
        System.out.println(s);
        writeBuffer("a.txt",s,true);
    }catch (IOException e){
        e.printStackTrace();
    }
}

转换流

public static String readCharset(String path) throws IOException {
    //先用文本接收在转换
    FileInputStream fis = new FileInputStream(path);
    InputStreamReader isr = new InputStreamReader(fis,"GBK");
    BufferedReader br = new BufferedReader(isr);
    String temp;
    StringBuffer sb = new StringBuffer();
    while (null!=(temp = br.readLine())){
        sb.append(temp);
    }
    br.close();
    isr.close();
    fis.close();
    return sb.toString();
}


public static void writeCharset(String path,String str,boolean isAppend,String charsetName) throws IOException{
    FileOutputStream fos = new FileOutputStream(path,isAppend);
    OutputStreamWriter osw = new OutputStreamWriter(fos,charsetName);
    osw.write(str);
    osw.close();
    fos.close();
}


public static void main(String[] args) {
    try {
        String s = readCharset("E:\\ideaproject\\ideacode\\a.txt");
        System.out.println(s);
        writeCharset("a.txt",s,false,"GB2312");
    }catch (IOException e){
        e.printStackTrace();
    }
}

读写二进制文件

DataInputStream类

与FileInputStream类结合使用读取二进制文件
在这里插入图片描述

public static void copyDataFile(String inPath,String outPath) throws IOException {
    FileInputStream fis = new FileInputStream(inPath);
    DataInputStream dis = new DataInputStream(fis);
    byte[] b = new byte[dis.available()];
    dis.read(b);
    System.out.println(new String(b));

DataOutputStream类

与FileOutputStream类结合使用写二进制文件
在这里插入图片描述

FileOutputStream fos = new FileOutputStream(outPath);
DataOutputStream dos = new DataOutputStream(fos);
dos.write(b);

dos.close();
fos.close();
dis.close();
fis.close();

public static void main(String[] args) throws IOException{
//        copyDataFile();
    }

序列化和反序列化(对象流)

在这里插入图片描述
在这里插入图片描述
Student类实现可序列化接口Serializable

//可序列化接口
public class Student implements Serializable {


    private int id;
    private String name;
    private double score;


    public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public double getScore() {
        return score;
    }


    public void setScore(double score) {
        this.score = score;
    }


    public Student() {


    }


    public Student(int id, String name, double score) {
        this.id = id;
        this.name = name;
        this.score = score;
    }


    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", score=" + score +
                '}';
    }
}
public static void writeObject(String path,boolean isAppend,Object obj) throws IOException {
    FileOutputStream fos = new FileOutputStream(path);
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(obj);
    oos.close();
}


public static Object read(String path) throws IOException, ClassNotFoundException {
    FileInputStream fis = new FileInputStream(path);
    ObjectInputStream ois = new ObjectInputStream(fis);
    Object o = ois.readObject();
    ois.close();
    return o;
}


public static void main(String[] args) {
    Student s = new Student(1,"张三",98.5);
    try {
        writeObject("obj.txt",true,s);
        Object o = read("obj.txt");
        if (o instanceof Student){
            Student stu = (Student) o;
            System.out.println(o);
        }
    }catch (IOException | ClassNotFoundException e){
        e.printStackTrace();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值