今天是周日,底下的程序是周五写的,礼拜五礼拜六太浪了就没写博客,嘿嘿,以后估计就是每周写五篇了。这周末过得很无趣啊,没啥好说的,每天就是起床吃饭,刷会儿手机,健身,回来看看电影写写代码。每天的日子很简单啊。周五的时候导师还问我进度如何。。。咋说呢,没写呗,我觉得他好像很无奈了,还说我进度太慢。别说,这进度是挺慢的,尴尬啊,JAVA基础还是得学学。这周咋办呢,写安卓吧。还剩两个月多就辞职吧,我反正是想好了,我爸妈估计也会同意的。就是我舅又得训我,嘿嘿嘿。到时候好好请胡过一顿,和他聊聊天,那得谢谢人家啊。就这样,明天就是新的一周了,继续过吧,还剩两个月多,估计会很快了。

package homework;


import java.io.*;

/**
 * 练习-向文件中写入两个数字,然后把这两个数字分别读取出来
 * 要求
 * 第一种方式: 使用缓存流把两个数字以字符串的形式写到文件里,再用缓存流以字符串的形式读取出来,然后转换为两个数字。
 * 注: 两个数字之间要有分隔符用于区分这两个数字。
 * 比如数字是31和15,如果不使用分隔符,那么就是3115,读取出来就无法识别到底是哪两个数字。
 * 使用分隔符31@15能解决这个问题。
 * 第二种方式: 使用数据流DataOutputStream向文件连续写入两个数字,然后用DataInpuStream连续读取两个数字
 */
public class Test {
    private static File file = new File("D:\\xyz\\lol2.txt");
    //缓存流写数字
    public static void HuanCunWrite(){
        try(FileWriter fw = new FileWriter(file);
            PrintWriter pw = new PrintWriter(fw)) {
            pw.print(31+"@"+15);
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public static void HuanCunRead(){
        try (FileReader fr = new FileReader(file);
             BufferedReader br = new BufferedReader(fr)){
            //读取缓存流中的一行(在此是唯一的一行)
            String line = br.readLine();
            //以@为分隔的字符,分隔line,存入String数组中
            String[] ss = line.split("@");
            System.out.println(ss[0]+" "+ss[1]);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void ShuJuWrite(){
        try (FileOutputStream fos  = new FileOutputStream(file);
             DataOutputStream dos =new DataOutputStream(fos)){
            dos.writeInt(20);
            dos.writeInt(18);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void ShuJuRead(){
        try (FileInputStream fis = new FileInputStream(file);
        DataInputStream dis = new DataInputStream(fis)){
            int x = dis.readInt();
            int y = dis.readInt();
            System.out.println(x+" "+y);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        HuanCunWrite();
        HuanCunRead();
        ShuJuWrite();
        ShuJuRead();
    }

}
package homework;

import java.io.*;

/**
 * 创建一个Hero对象,设置其名称为garen。 
 * 把该对象序列化到一个文件garen.lol。
 * 然后再通过序列化把该文件转换为一个Hero对象
 *
 * 注:把一个对象序列化有一个前提是:这个对象的类,必须实现了Serializable接口
 */
public class TestStream {
    public static void main(String[] args) {
        //创建一个Hero garen
        //要把Hero对象直接保存在文件上,务必让Hero类实现Serializable接口
        Hero garen = new Hero();
        garen.name = "garen";
        garen.hp = 616;
        File file = new File("d:/garen.lol");
        try (//创建对象输出流
             FileOutputStream fos = new FileOutputStream(file);
             ObjectOutputStream oos =new ObjectOutputStream(fos);
             //创建对象输入流
             FileInputStream fis = new FileInputStream(file);
             ObjectInputStream ois =new ObjectInputStream(fis)){
            oos.writeObject(garen);
            Hero hero = (Hero)ois.readObject();
            System.out.println(hero.name);
            System.out.println(hero.hp);

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


}
package homework;

import java.io.*;

/**
 * 准备一个长度是10,类型是Hero的数组,使用10个Hero对象初始化该数组
 * 然后把该数组序列化到一个文件heros.lol
 * 接着使用ObjectInputStream 读取该文件,并转换为Hero数组,验证该数组中的内容,是否和序列化之前一样
 */
public class TestStream {
    public static void main(String[] args) {
        int MAX_VALUE = 10;
        //定义一个长度为10的Hero类型数组
        Hero[] heroes = new Hero[MAX_VALUE];
        //使用10个Hero对象初始化该数组
        System.out.println("序列化前的数组内容");
        for (int i = 0;i < MAX_VALUE;i++){
            heroes[i] = new Hero();
            heroes[i].name = "hero"+i;
            heroes[i].hp = (float)(500 + Math.random() * 100);
            System.out.println(heroes[i].name+" "+heroes[i].hp);
        }
        File file = new File("D:\\xyz\\garen.lol");
        try (//创建对象输出流,从内存中写东西到文件
             FileOutputStream fos = new FileOutputStream(file);
             ObjectOutputStream oos =new ObjectOutputStream(fos);
             //创建对象输入流,读内容到内存
             FileInputStream fis = new FileInputStream(file);
             ObjectInputStream ois =new ObjectInputStream(fis)){
            //把该Hero数组序列化到一个文件garen.lol
            oos.writeObject(heroes);
            Hero[] newHeros = new Hero[MAX_VALUE];
            newHeros = (Hero[]) ois.readObject();
            System.out.println("序列化后的数组内容");
            for (int i = 0;i < MAX_VALUE;i++){
                System.out.println(newHeros[i].name+" "+newHeros[i].hp);
            }

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }


    }
}
package homework;


import java.io.*;
import java.util.Scanner;

/**
 * 自动创建有一个属性的类文件。
 * 通过控制台,获取类名,属性名称,属性类型,根据一个模板文件,自动创建这个类文件,并且为属性提供setter和getter
 */
public class TestStream {
    public static void main(String[] args) {
        System.out.println("请输入类的名称");
        Scanner in = new Scanner(System.in);
        String className = in.nextLine();
        System.out.println("请输入属性的类型:");
        String type = in.nextLine();
        System.out.println("请输入属性的名称:");
        String property = in.nextLine();
        String Uproperty = toUpperFirstLetter(property);
        //读取模板文件
        File modelFile = new File("D:\\xyz\\Model.txt");
        //初始化模板文件内容
        String modelContent = null;
        //FileReader进行文件读取
        try (FileReader fr = new FileReader(modelFile)){
            char[] cs = new char[(int) modelFile.length()];
            // 以字符流的形式读取文件所有内容,存放到cs数组中
            fr.read(cs);
            modelContent = new String(cs);

        } catch (IOException e) {
            e.printStackTrace();
        }
        //进行替换
        String fileContent = null;
        fileContent = modelContent.replaceAll("@class@", className)
                .replaceAll("@type@", type)
                .replaceAll("@property@", property)
                .replaceAll("@Uproperty@", Uproperty);
        String fileName = className+".java";
        //替换后的内容
        System.out.println("替换后的内容:");
        System.out.println(fileContent);
        File file = new File("D:\\xyz",fileName);
        //写入新文件
        try(FileWriter fw =new FileWriter(file);){
            fw.write(fileContent);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //file.getAbsolutePath() 获取文件的地址
        System.out.println("文件保存在:" + file.getAbsolutePath());




    }
    //首字母大写
    public static String toUpperFirstLetter(String str){
        char upperCaseFirst =Character.toUpperCase(str.charAt(0));
        String rest = str.substring(1);
        return upperCaseFirst + rest;

    }

}