public class Test05 { public static void main(String[] args) { List list = new ArrayList<>(); RandomAccessFile raf = null; try { raf = new RandomAccessFile("./emp.dat", "r"); for (int i = 0; i < raf.length() / 56; i++) { raf.seek(i * 56); byte[] data = new byte[32]; raf.read(data); String name = new String(data, "utf-8").trim(); short age = raf.readShort(); data = new byte[10]; raf.read(data); String gender = new String(data, "utf-8").trim(); int salary = raf.readInt(); long hiredate = raf.readLong(); Emp emp = new Emp(name, age, gender, salary, hiredate); list.add(emp); } Collections.sort(list); for (Emp emp : list) { System.out.println(emp); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (raf != null) { try { raf.close(); } catch (IOException e) { e.printStackTrace(); } } } } }


原文地址: https://www.cveoy.top/t/topic/oHRV 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录