xiaoxiao2023-11-21  163

    1. RandomAccessFile

    能读能写,可以获取文件指针的位置,能够定位指针位置

    public class FileDemo{ public static void main(String[] args) throws IOException { //write(); RandomAccessFile rw = new RandomAccessFile("zqq2.txt", "rw"); rw.writeInt(8); rw.writeInt(8); rw.writeBoolean(false); rw.close(); read1(); } private static void write() throws IOException { RandomAccessFile rw = new RandomAccessFile("zqq2.txt", "rw"); rw.writeInt(8); rw.writeInt(8); rw.writeBoolean(false); rw.close(); } private static void read1() throws IOException { RandomAccessFile e = new RandomAccessFile("zqq.txt", "rw"); int i = e.readInt(); long filePointer = e.getFilePointer(); System.out.println("指针位置"+filePointer); int i1 = e.readInt(); long filePointer1 = e.getFilePointer(); System.out.println("指针位置"+filePointer1); boolean b = e.readBoolean(); long filePointer2 = e.getFilePointer(); System.out.println("指针位置"+filePointer2); e.seek(0);//定位指针的位置 int and= e.readInt(); System.out.println(and); e.seek(8); boolean b1 = e.readBoolean(); System.out.println(b1); e.close(); } } 输出结果: 指针位置4 指针位置8 指针位置9 8 false

    2

    public class IODemo2 { //作业:断点复制 public static void main(String[] args) throws IOException { RandomAccessFile r = new RandomAccessFile("yinyue.txt", "rw"); RandomAccessFile w = new RandomAccessFile("yinyue2.txt", "rw"); File file = new File("yinyue2.txt"); if (file.exists()) { long length = file.length(); //定位字节位置 r.seek(length); w.seek(length); } else { r.seek(0); w.seek(0); } copyFile(r,w); } private static void copyFile(RandomAccessFile r, RandomAccessFile w) throws IOException { int len = 0; byte[] bytes = new byte[100]; while ((len = r.read(bytes)) != -1) { long pointer = r.getFilePointer();//获取文件指针位置 System.out.println(pointer); w.write(bytes, 0, len); } r.close(); w.close(); } }

    3.序列化流

    public class IODemo7 { public static void main(String[] args) throws IOException, ClassNotFoundException { //序列化:把内存中的数据写到硬盘上 //反序列化:把硬盘上的数据读取到内存中 //ObjectOutputStream 序列化流 //ObjectInputStream 反序列化流 //writeObj(); //Exception in thread "main" java.io.InvalidClassException:org.westos.demo6.Student; //local class incompatible:stream classdesc serialVersionUID = -4189265988613231743, // local class serialVersionUID =2152130991194164782 readObj(); } private static void readObj() throws IOException, ClassNotFoundException { ObjectInputStream in = new ObjectInputStream(new FileInputStream("obj.txt")); Object object = in.readObject(); ArrayList<Student> list = (ArrayList<Student>) object; Student student = list.get(list.size() - 1); System.out.println(student.getName()); System.out.println(student.getAge()); } private static void writeObj() throws IOException { Student student = new Student("张三", 23); Student student2 = new Student("张三2", 23); Student student3 = new Student("张三3", 23); Student student4 = new Student("张三4", 23); ArrayList<Student> list = new ArrayList<>(); list.add(student); list.add(student2); list.add(student3); list.add(student4); ObjectOutputStream obj = new ObjectOutputStream(new FileOutputStream("obj.txt")); //把一个对象序列化到硬盘上 obj.writeObject(list); //序列化对象时 要求此类实现 Serializable 序列化接口 //NotSerializableException } }

    4.

    public class IODemo { public static void main(String[] args) { //Properties 属性集合 键值都是String类型 Properties properties = new Properties(); //用父类的方法 存储键值 //properties.put("key","value"); //Object key = properties.get("key"); //用特有的方法 存储值 properties.setProperty("key","value"); //参数1 键 参数2:默认值,如果通过键没有找到对应的值,就使用默认值 String property = properties.getProperty("key","abc"); String property1 = properties.getProperty("key2", "abc"); System.out.println(property); System.out.println(property1); } } 输出结果: value abc

    5.

    public class IODemo4 { public static void main(String[] args) throws IOException { Properties properties = new Properties(); properties.setProperty("zhangsan","123456"); properties.setProperty("zhangsan2", "123456"); properties.setProperty("zhangsan3", "123456"); properties.setProperty("zhangsan4", "123456"); properties.setProperty("赵六", "123456"); //把集合中的数据写入到配置文件当中去 properties.store(new FileOutputStream("student.properties"),null); } } 实验结果: student.properties文件中的内容: \u8D75\u516D=123456 zhangsan=123456 zhangsan4=123456 zhangsan3=123456 zhangsan2=123456
    最新回复(0)