Java--IO--作业练习

  1.  

     

    1.   
      package com.model.work;
      
      import java.io.BufferedWriter;
      import java.io.File;
      import java.io.FileWriter;
      import java.io.IOException;
      
      /**
       * @Description:测试类
       * @Author: 张紫韩
       * @Crete 2021/6/19 14:42
       */
      public class WorkHomeDemo01 {
          public static void main(String[] args) throws IOException {
      
              String filePath="D:\\qq\\IDEA\\IdeaProjects\\java_mianshi_test\\mianshi_io\\src\\main\\resources\\File\\mytemp";
      
              File file = new File(filePath);
              if (file.exists()){
                  System.out.println("已经存在了");
              }else {
                  if (file.mkdir()){
                      System.out.println(file.getName()+"文件创建成功了");
                  };
              }
      
      
              File file1 = new File(filePath, "hello.txt");
              if(file1.createNewFile()){
                  System.out.println(file1.getName()+"创建成功了");
              }
      
              if(file1.exists()){
                  BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file1,true));
                  bufferedWriter.write("hello,world");
                  bufferedWriter.close();
                  System.out.println(file1.getName()+"已经存在了");
              }
          }
      }

       

  2.  

      

    1.   
      package com.model.work;
      
      import java.io.*;
      
      /**
       * @Description:测试类
       * @Author: 张紫韩
       * @Crete 2021/6/19 15:02
       */
      public class WorkHomoDemo02 {
          public static void main(String[] args) throws IOException {
      
              String filePath="D:\\qq\\IDEA\\IdeaProjects\\java_mianshi_test\\mianshi_io\\src\\main\\resources\\Reader\\CopyDemo02.txt";
      
              //如果文件编码格式改为gbk格式,如何读取
      
              String charSet="gbk";
              BufferedReader bufferedReader = new BufferedReader(
                      new InputStreamReader(
                              new FileInputStream(filePath),charSet));
              String readLine=null;
              int count=1;
              while((readLine=bufferedReader.readLine())!=null){
                  System.out.println(count+"\t"+readLine);
                  count++;
              }
      
              bufferedReader.close();
      
          }
      }

       

  3.  

     

    1.   
      package com.model.work;
      
      import java.io.*;
      import java.util.Properties;
      
      /**
       * @Description:测试类
       * @Author: 张紫韩
       * @Crete 2021/6/19 15:13
       */
      public class WorkHomeDemo03 {
          public static void main(String[] args) throws IOException, ClassNotFoundException {
      
              String filePath="D:\\qq\\IDEA\\IdeaProjects\\java_mianshi_test\\mianshi_io\\src\\main\\resources\\Properties\\dog.properties";
              Properties properties = new Properties();
              properties.load(new FileReader(filePath));
              int age = Integer.parseInt(properties.getProperty("age"));
              Dog dog=new Dog(properties.getProperty("name"), age, properties.getProperty("color"));
              System.out.println(dog.toString());
              System.out.println("姓名:"+dog.getName());
              System.out.println("年龄:"+dog.getAge());
              System.out.println("颜色:"+dog.getColor());
              String dogFile="D:\\qq\\IDEA\\IdeaProjects\\java_mianshi_test\\mianshi_io\\src\\main\\resources\\Properties\\dog.dat";
              ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(dogFile));
              objectOutputStream.writeObject(dog);
              objectOutputStream.close();
              System.out.println("序列化完成");
      
              ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(dogFile));
              Dog readDog = (Dog) objectInputStream.readObject();
              System.out.println(readDog.toString());
              objectInputStream.close();
              System.out.println("反序列化完成");
      
          }
      }
      class Dog implements Serializable {
          private String name;
          private int age;
          private String color;
      
          @Override
          public String toString() {
              return "Dog{" +
                      "name='" + name + '\'' +
                      ", age=" + age +
                      ", color='" + color + '\'' +
                      '}';
          }
      
          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 String getColor() {
              return color;
          }
      
          public void setColor(String color) {
              this.color = color;
          }
      
          public Dog(String name, int age, String color) {
              this.name = name;
              this.age = age;
              this.color = color;
          }
      }

       

posted @ 2021-06-19 15:36  张紫韩  阅读(59)  评论(0编辑  收藏  举报