import java.io.*;
import java.time.LocalDate;

public class Test {
    public static void main(String[] args){
        RandomAccessTest.test();
    }
}

/*
    2.3 读写二进制数据
 */

/*
    2.3.2 随机访问文件
    写了大半天,突然发现这个实验好像不是太严谨:
        1.RandomAccessFile算长度时,应该是根据字节数算出来的
        2.写字符串时,我们只是指定了码元数量,我们写的是固定码元数量的字符串
        3.这样的化,我们记录的Employee.RECORD_SIZE根本就代表不了一条记录的长度
        4.但是我们最后又通过RandomAccessFile的长度和Employee.RECORD_SIZE来计算记录数量
        5.我觉得这个实验有问题,以后研究吧
 */

class Employee {
    private String name;
    private double salary;
    private LocalDate hireDay;
    public static final int NAME_SIZE = 30;
    public static final int RECORD_SIZE = 50;

    public Employee(String name, double salary, LocalDate hireDay) {
        this.name = name;
        this.salary = salary;
        this.hireDay = hireDay;
    }

    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 LocalDate getHireDay() {
        return hireDay;
    }

    public void setHireDay(LocalDate hireDay) {
        this.hireDay = hireDay;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", salary=" + salary +
                ", hireDay=" + hireDay +
                '}';
    }
}

class DataIO {
    //Java核心技术 卷二 第十版 2.3.2节
    //写出从字符串开头开始的指定数量的码元,如果码元过少,该方法会用‘0’来补齐字符串
    public static void writeFixedString(String s, int size, DataOutput output) throws IOException {
        for (int i = 0; i < size; i++) {
            char ch =0;
            if(i<s.length())
                ch = s.charAt(i);
            output.write(ch);
        }
    }

    //Java核心技术 卷二 第十版 2.3.2节
    //从输入流中读入字符,直至读入size个码元,或者直至遇到具有0值的字符串,然后跳出输入字段中剩余的0值。
    public static String readFixedString1(int size, DataInput in) throws IOException {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < size; i++) {
            char c;
            if ((c = in.readChar()) != 0) {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    //功能和上一个方法是一样的,但是这个效率会高那么一点点
    public static String readFixedString2(int size, DataInput in) throws IOException {
        StringBuilder sb = new StringBuilder();
        /*
        int i;
        for (i = 0; i < size; i++) {
            char c;
            if ((c = in.readChar()) == 0) {
                break;
            }
            sb.append(c);
        }
        in.skipBytes(2*(size-i));   //这个地方不是太严谨
        */

        //用书中代码测试一下
        int i =0;
        boolean more = true;
        while (more && i < size) {
            char ch = in.readChar();
            i++;
            if (ch == 0) {
                more = false;
            } else {
                sb.append(ch);
            }
        }
        in.skipBytes(2 * (size - i));

        return sb.toString();
    }
}

class RandomAccessTest {
    public static void test() {
        Employee[] staff = new Employee[]{
                new Employee("A", 10, LocalDate.now()),
                new Employee("B", 20, LocalDate.now()),
                new Employee("C", 30, LocalDate.now())
        };

        //写入
        try(DataOutputStream out = new DataOutputStream(new FileOutputStream("employee1.dat"))) {

            for (Employee e : staff) {
                writeData(out, e);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //读取

        try(RandomAccessFile in = new RandomAccessFile("employee1.dat","r")) {
            int n = (int) (in.length() / Employee.RECORD_SIZE);
            Employee[] newStaff = new Employee[n];

            for (int i = n - 1; i >= 0; i--) {
                in.seek(i*Employee.RECORD_SIZE);
                newStaff[i] = readDate(in);
            }

            for (Employee e : newStaff) {
                System.out.println(e);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static void writeData(DataOutput out, Employee employee) throws IOException {
        DataIO.writeFixedString(employee.getName(), Employee.NAME_SIZE, out);
        out.writeDouble(employee.getSalary());

        LocalDate hireDay = employee.getHireDay();
        out.writeInt(hireDay.getYear());
        out.writeInt(hireDay.getMonthValue());
        out.writeInt(hireDay.getDayOfMonth());
    }

    private static Employee readDate(DataInput input) throws IOException {
        String name = DataIO.readFixedString2(Employee.NAME_SIZE, input);
        double salary = input.readDouble();
        int
                y= input.readInt(),
                m= input.readInt(),
                d= input.readInt();
        return new Employee(name, salary, LocalDate.of(y, m, d));
    }
}

/*
    2.3.3 ZIP文档

    ZipFile API:
        ZipFile(String name)
        ZipFile(File file)
        Enumeration entries()
        ZipEntry getEntry(String name)
        InputStream getInputStream(ZipEntry ze)
        String getName()

    从这个类的API中可以看出来,还有一种使用Zip的方案。先通过ZipFile对象,得到
    这个压缩包中的每一条记录,然后再指定某条具体的记录,得到其中的数据。
 */

《Java核心技术卷二》笔记

Java读写二进制数据的更多相关文章

  1. IO流-文本IO\读写二进制数据

    文本IO 一.简述 OutputStreamWriter类使用选定的编码方式吧Unicode字符流转换为字节流,InputStreamReader类将包含字节的输入流转为可以产生Unicode字符的读 ...

  2. 背水一战 Windows 10 (89) - 文件系统: 读写文本数据, 读写二进制数据, 读写流数据

    [源码下载] 背水一战 Windows 10 (89) - 文件系统: 读写文本数据, 读写二进制数据, 读写流数据 作者:webabcd 介绍背水一战 Windows 10 之 文件系统 读写文本数 ...

  3. [Java] 读写字节数据,过滤流DataOutputStream和DataInputStream

    package test.stream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io ...

  4. [Java] 读写字符串数据

    package test.stream; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...

  5. Java/Android 二进制数据与String互转

    将经过加密的二进制数据保存到本地的方法 byte[] src = new byte[] { 122,-69, -17, 92, -76, 52, -21, -87, -10, 105, 76, -75 ...

  6. SQLite入门(二)读写二进制数据

    //读二进制数据的函数 BOOL OpenBinDataFile(BYTE **pBUf,UINT &len) {     if (pBUf == NULL)     {         re ...

  7. java读写串口数据

    本博文参考自https://www.cnblogs.com/Dreamer-1/p/5523046.html 最近接触到了串口及其读写,在此记录java进行串口读写的过程. 1.导入串口支持包 需要下 ...

  8. java:判断二进制数据中第n位是否为1

    可以使用位运算来判断. &是位的与运算符,是指二进制数按位“与”的操作, 逻辑与就是两者都为真的时候才为真,其他真假,假真,假假的运算结果都是假.二进制写法如下 1 & 1 = 1 , ...

  9. Qt里怎么处理二进制数据

    Qt里有个专门的类QDataStream就是专门读写二进制数据的, 它与QByteArray搭配在网络编程中有奇效. 来个栗子: // write data QByteArray data; QDat ...

随机推荐

  1. SEED缓冲区溢出实验笔记

    缓冲区溢出实验(Linux 32位) 参考教程与材料:http://www.cis.syr.edu/~wedu/seed/Labs_12.04/Software/Buffer_Overflow/ (本 ...

  2. 16.09 working note

    这个月最主要任务是linux shell script学习. 其次是继续spring源码学习. 其余时间C.C++和Java学习. 01 9月第一天,9点多才到家.做道简单的oj题练习下.因为简单,所 ...

  3. Use Spring @Scheduled To Achieve Timing Task

    Today I have a chance to use the @Scheduled to achieve a timing task.It is useful.Anyway,it was the ...

  4. nginx 负载均衡,多站点共享Session

    原文:nginx 负载均衡,多站点共享Session nginx 负载均衡,多站点共享Session 多站点共享Session常见的作法有: 使用.net自动的状态服务(Asp.net State S ...

  5. Qt4.7.4下单独编译QtWebkit

    最近编译出了Qt4.7.4的嵌入式版本,但没有编译QtWebkit库.在编译一个使用Webkit的工程时出错,而根据工程的需要,要单独编译QtWebkit库.    由于不想再次编译整个的Qt库,于是 ...

  6. Microsoft Development Platform Technologies

  7. 多进程demo

    多进程实现DOS重定向输出,界面如下: 主要的代码实现如下: #define MAXREADBUFFERLEN (60 * 1000) void CRedirectDlg::OnBnClickedBu ...

  8. axios和vuex

    0.babel 将es6代码转换成各个浏览器都能识别的代码 一.axios 1.官方网站 https://www.kancloud.cn/yunye/axios/234845 2.引用: (1)cdn ...

  9. Alwayson架构下 服务器 各虚拟IP漂移监控告警的功能实现

    1.需求概括 我们知道,在SQL Server Alwayson 架构中,有多种虚拟IP,例如 WindowsCluster IP,ListenIP,角色高可用性IP(类似于侦听IP).在某些条件下, ...

  10. YARN分析系列之一 -- 总览YARN组件

    下图简单明了的描述了hadoop yarn 的功能是如何从 hadoop 中细化出来的. 注:图片来自 https://apprize.info/php/hadoop/9.html Hadoop 从 ...