guyanqing 1 年之前
父節點
當前提交
0e9fa81b34

+ 0 - 1
abc.txt

@@ -1 +0,0 @@
-xiaoniankuailehappynewyearxiaoniankuailehappynewyearxiaoniankuailehappynewyear

二進制
employee.dat


二進制
employee1.dat


+ 0 - 1
fw.txt

@@ -1 +0,0 @@
-爱扣钉꿈

+ 8 - 0
pom.xml

@@ -19,6 +19,14 @@
             <version>4.13.2</version>
             <version>4.13.2</version>
             <scope>compile</scope>
             <scope>compile</scope>
         </dependency>
         </dependency>
+
+        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
+        <dependency>
+            <groupId>commons-io</groupId>
+            <artifactId>commons-io</artifactId>
+            <version>2.5</version>
+        </dependency>
+
     </dependencies>
     </dependencies>
 
 
     <properties>
     <properties>

二進制
save.dat


+ 0 - 1
sf-1.txt

@@ -1 +0,0 @@
-abcdabcd

+ 0 - 1
sf.txt

@@ -1 +0,0 @@
-abcdabcd

+ 0 - 1
sf1.txt

@@ -1 +0,0 @@
-loveCoding1loveCoding1

+ 85 - 0
src/main/java/com/sf/day22/Employee.java

@@ -0,0 +1,85 @@
+package com.sf.day22;
+
+import java.io.Serializable;
+
+public class Employee implements Serializable {
+
+    private static long height = 123L;
+    private transient byte sex;
+    private String name;
+    private int age;
+    private double weight;
+    private boolean flag;
+
+    public Employee() {
+    }
+
+    public Employee(byte sex, String name, int age, double weight, boolean flag) {
+        this.sex = sex;
+        this.name = name;
+        this.age = age;
+        this.weight = weight;
+        this.flag = flag;
+    }
+
+
+
+    public static long getHeight() {
+        return height;
+    }
+
+    public static void setHeight(long height) {
+        Employee.height = height;
+    }
+
+    public byte getSex() {
+        return sex;
+    }
+
+    public void setSex(byte sex) {
+        this.sex = sex;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public int getAge() {
+        return age;
+    }
+
+    public void setAge(int age) {
+        this.age = age;
+    }
+
+    public double getWeight() {
+        return weight;
+    }
+
+    public void setWeight(double weight) {
+        this.weight = weight;
+    }
+
+    public boolean isFlag() {
+        return flag;
+    }
+
+    public void setFlag(boolean flag) {
+        this.flag = flag;
+    }
+
+    @Override
+    public String toString() {
+        return "Employee{" +
+                "sex=" + sex +
+                ", name='" + name + '\'' +
+                ", age=" + age +
+                ", weight=" + weight +
+                ", flag=" + flag +
+                '}';
+    }
+}

+ 35 - 0
src/main/java/com/sf/day22/Test01.java

@@ -0,0 +1,35 @@
+package com.sf.day22;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.junit.Test;
+
+import java.io.*;
+
+public class Test01 {
+
+    /**
+     * 从键盘输入字符串,要求将读取到的整行字符串转成大写输出。
+     * 然后继续进行输入操作,直至当输入“e”或者“exit”时,退出程序。
+     */
+    @Test
+    public void t1() throws IOException{
+        System.out.println("请输入字符串,直至当输入“e”或者“exit”时,退出程序。");
+        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
+        String str;
+            while ((str = br.readLine()) !=null){
+                if("e".equals(str) || "exit".equals(str)){
+                    break;
+                }
+                System.out.println(str.toUpperCase());
+            }
+    }
+
+    @Test
+    public void t2() throws IOException {
+//        int copy = IOUtils.copy(new FileInputStream("D:\\a.txt"), new FileOutputStream("D:\\aaaaaa.txt"));
+//        IOUtils.closeQuietly();
+//        System.out.println(copy);
+        FileUtils.writeStringToFile(new File("D:\\a.txt"),"eeeeee",true);
+    }
+}

+ 101 - 0
src/main/java/com/sf/day22/Test02.java

@@ -0,0 +1,101 @@
+package com.sf.day22;
+
+import com.sun.corba.se.impl.interceptors.PICurrent;
+import org.junit.Test;
+import org.omg.CORBA.PUBLIC_MEMBER;
+import sun.misc.OSEnvironment;
+
+import java.io.*;
+import java.util.ArrayList;
+import java.util.List;
+
+public class Test02 {
+
+    @Test
+    public void save() throws IOException {
+        String name = "张三";
+        int age = 18;
+        double height = 182;
+        boolean flag = false;
+
+        //序列化
+        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("save.dat"));
+        oos.writeUTF(name);
+        oos.writeInt(age);
+        oos.writeDouble(height);
+        oos.writeBoolean(flag);
+        oos.close();
+    }
+
+    /**
+     * 反序列化
+     */
+    @Test
+    public void t2() throws IOException, ClassNotFoundException {
+        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("save.dat"));
+        String name = ois.readUTF();
+        System.out.println(name);
+        System.out.println(ois.readInt());
+        System.out.println(ois.readDouble());
+        System.out.println(ois.readBoolean());
+        ois.close();
+    }
+
+    @Test
+    public void t3() throws IOException{
+        Employee employee = new Employee((byte) 12,"zhangsan",12,190.8,true);
+        //创建序列化的对象流
+        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("employee1.dat"));
+        oos.writeObject(employee);
+        //关闭资源
+        oos.close();
+    }
+
+    /**
+     * 对Employee对象进行反序列化
+     * @throws IOException
+     */
+    @Test
+    public void t4() throws IOException, ClassNotFoundException {
+        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("employee1.dat"));
+        Object o = ois.readObject();
+        if(o instanceof Employee){
+            Employee employee = (Employee) o;
+            System.out.println(employee);
+        }
+        //关闭资源
+        ois.close();
+    }
+
+    @Test
+    public void t5() throws IOException{
+        User user1 = new User("admin1",1001);
+        User user2 = new User("admin2",1002);
+        User user3 = new User("admin3",1003);
+        User user4 = new User("admin4",1004);
+        User user5 = new User("admin5",1005);
+
+        List<User> userList = new ArrayList<>();
+        userList.add(user1);
+        userList.add(user2);
+        userList.add(user3);
+        userList.add(user4);
+        userList.add(user5);
+
+        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("user.dat"));
+        oos.writeObject(userList);
+        oos.close();
+    }
+
+    @Test
+    public void t6() throws IOException, ClassNotFoundException {
+        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("user.dat"));
+        Object o = ois.readObject();
+        //转换成集合
+        System.out.println(o);
+        List<User> userList = (List<User>) o;
+        for (User user : userList) {
+            System.out.println(user);
+        }
+    }
+}

+ 41 - 0
src/main/java/com/sf/day22/User.java

@@ -0,0 +1,41 @@
+package com.sf.day22;
+
+import java.io.Serializable;
+
+public class User implements Serializable {
+//  static final long UUID =   123425364758907L;
+    private String name;
+    private int id;
+
+    public User() {
+    }
+
+    public User(String name, int id) {
+        this.name = name;
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    @Override
+    public String toString() {
+        return "User{" +
+                "name='" + name + '\'' +
+                ", id=" + id +
+                '}';
+    }
+}

二進制
target/classes/com/sf/day22/Employee.class


二進制
target/classes/com/sf/day22/Test01.class


二進制
target/classes/com/sf/day22/Test02.class


二進制
target/classes/com/sf/day22/User.class


二進制
user.dat