其他流

其他流

字节数组流

ByteArrayInoutStream 无需关闭
ByteArrayOutputStream 多了toByteArray方法

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    read(write());
}
public static byte[] write() throws IOException {
    byte[] dest;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    String msg = "fdsgds法国电视公司的";
    byte[] info = msg.getBytes();
    bos.write(info,0,info.length);

    dest = bos.toByteArray();
    System.out.println(new String(dest,0,dest.length));
    bos.close();
    return dest;
}
public static void read(byte[] src) throws IOException {
    InputStream is = new BufferedInputStream(new ByteArrayInputStream(src));

    byte[] flush = new byte[3];
    int len = 0;
    while(-1 != (len = is.read(flush))) {
        System.out.println(new String(flush,0,len));
    }
}
示例
public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    byte[] data = getBytesFromFile("src/com/cai/io/one1.txt");
    System.out.println(new String(data));
    toFileFromByteArray(data,"src/com/cai/io/one2.txt");
}
public static byte[] getBytesFromFile(String strPath) throws IOException {
    File src = new File(strPath);
    InputStream is = new BufferedInputStream(new FileInputStream(src));
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] flush = new byte[1024];
    int len = 0;
    while(-1!=(len = is.read(flush))) {
        bos.write(flush,0,len);
    }
    byte[] dest = bos.toByteArray();
    bos.close();
    is.close();
    return dest;
}
public static void toFileFromByteArray(byte[] src,String descPath) throws IOException {
    File dest = new File(descPath);
    InputStream is = new BufferedInputStream(new ByteArrayInputStream(src));
    OutputStream os = new BufferedOutputStream(new FileOutputStream(descPath));
    byte[] flush = new byte[1024];
    int len = 0;
    while(-1 != (len =is.read(flush))){
        os.write(flush, 0, len);
    }
    os.flush();
    os.close();
    is.close();
}

这里写图片描述

基本数据类型处理流

DataInputStream、DataOutputStream

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    write("src/com/cai/io/one3.txt");
    read("src/com/cai/io/one3.txt");
    System.out.println(writeToByteArray().length);
}
public static void write(String destPath) throws IOException {
    double p1 = 2.5;
    long p2 = 100L;
    String str = "数据类型";

    File dest = new File(destPath);
    DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dest)));
    dos.writeDouble(p1);
    dos.writeLong(p2);
    dos.writeUTF(str);
    dos.flush();
    dos.close();
}
public static void read(String destPath) throws IOException {
    File src = new File(destPath);
    DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(src)));
    System.out.println(dis.readDouble());
    System.out.println(dis.readLong());
    System.out.println(dis.readUTF());
    dis.close();
}
public static byte[] writeToByteArray() throws IOException {
    double p1 = 2.5;
    long p2 = 100L;
    String str = "数据类型";

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(bos));
    dos.writeDouble(p1);
    dos.writeLong(p2);
    dos.writeUTF(str);
    dos.flush();
    byte[] data = bos.toByteArray();
    dos.close();
    bos.close();
    return data;
}

这里写图片描述

引用类型处理流

反序列化: 输入流 ObjectInputStream readObject()
序列化: 输出流 ObjectOutputStream writeObject()
1. 不是所有对象都可以序列化 需要实现java.io.Serializable接口
2. 不是所有属性都需要序列化,不需要序列化的使用transient

public class Employee implements Serializable {
    private transient String name;
    private double salary;
    public Employee() {
        // TODO Auto-generated constructor stub
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }
    public Employee(String name, double salary) {
        super();
        this.name = name;
        this.salary = salary;
    }
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
        // TODO Auto-generated method stub
        write("src/com/cai/io/one5.txt");
        read("src/com/cai/io/one5.txt");
    }
public static void write(String destPath) throws IOException {
    File file = new File(destPath);
    ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
    oos.writeObject(new Employee("fdsf", 455.5));
    oos.flush();
    oos.close();
}
public static void read(String srcPath) throws IOException, ClassNotFoundException {
    File file = new File(srcPath);
    ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)));
    Employee employee = (Employee)ois.readObject();
    System.out.println(employee.getName());
    System.out.println(employee.getSalary());
}

这里写图片描述

打印流

PrintStream ps = System.out;
ps.println("test");
// 输出到文件
File file = new File("src/com/cai/io/one.txt");
ps=new PrintStream(new BufferedOutputStream(new FileOutputStream(file)));
ps.print("testhkhkj");
ps.flush();
ps.close();
// 从文件输入
Scanner scanner = new Scanner(System.in);
System.out.println(scanner.nextLine());
scanner = new Scanner(new BufferedInputStream(new FileInputStream(file)));
System.out.println(scanner.nextLine());
// 重定向
System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(file)),true));
System.out.println("aaaaaaaaaaaaa");
// 回到控制台,输入流:FileDescriptor.in,输出流: FileDescriptor.out
System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out)),true));
System.out.println("回到控制台");

这里写图片描述

RandomAccessFile

RandomAccessFile rnd = new RandomAccessFile("src/com/cai/io/one3.txt","r");
rnd.seek(0);
byte[] flush = new byte[1024];
int len = 0;
while(-1 != (len = rnd.read(flush))) {
    System.out.println(new String(flush,0,len));
}
rnd.close();

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值