A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 小石姐姐 于 2019-5-17 11:12 编辑

个人笔记一、集合到文件数据排序改进版
[Java] 纯文本查看 复制代码
//学生类
package bufferedwriterreader;

public class Student {
    private String name;
    private int chinese;
    private int math;
    private int english;

    public Student() {
    }

    public Student(String name, int chinese, int math, int english) {
        this.name = name;
        this.chinese = chinese;
        this.math = math;
        this.english = english;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getChinese() {
        return chinese;
    }

    public void setChinese(int chinese) {
        this.chinese = chinese;
    }

    public int getMath() {
        return math;
    }

    public void setMath(int math) {
        this.math = math;
    }

    public int getEnglish() {
        return english;
    }

    public void setEnglish(int english) {
        this.english = english;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", chinese=" + chinese +
                ", math=" + math +
                ", english=" + english +
                '}';
    }

    public int getSum() {
        return chinese + math + english;
    }
}

package bufferedwriterreader;
//把学生信息写入到文件中(用字符缓冲输出流BufferedWriter),按照总成绩从高到低的顺序写入(使用TreeSet集合排序)
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

public class SetToTXT {
    public static void main(String[] args) throws IOException {
        TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                //按照总成绩从高到低
                int num1 = s2.getSum() - s1.getSum();
                //按照语文成绩,比较是否相同
                int num2 = num1 == 0 ? s2.getChinese() - s1.getChinese() : num1;
                //按照数学成绩,比较是否相同
                int num3 = num2 == 0 ? s2.getMath() - s1.getMath() : num2;
                //按照学生姓名,比较是否相同
                int num4 = num3 == 0 ? s2.getName().compareTo(s1.getName()) : num3;
                return num4;
            }
        });
        //录入4个学生信息
        for (int i = 0; i < 4; i++) {
            //键盘录入学生信息
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入第"+(i+1)+"个学生信息:");
            System.out.println("姓名:");
            String name = sc.nextLine();
            System.out.println("语文成绩:");
            int chinese = sc.nextInt();
            System.out.println("数学成绩:");
            int math = sc.nextInt();
            System.out.println("英语成绩:");
            int english = sc.nextInt();
            //创建学生对象,存储学生信息
            Student s = new Student(name,chinese,math,english);
            //把每个学生对象存储到Set集合
            ts.add(s);
        }
        //创建字符缓冲输出流,把学生信息写入到文件中
        BufferedWriter bw = new BufferedWriter(new FileWriter("day10\\students.txt"));
        for (Student s : ts){
            //把每个学生信息,拼接成指定格式
            StringBuilder sb = new StringBuilder();
            sb.append(s.getName()).append(",").append(s.getChinese()).append(",").append(s.getMath()).append(",").append(s.getEnglish()).append(",").append(s.getSum());
            //把指定格式的学生信息,写入到指定文件中
            bw.write(sb.toString());
            //换行
            bw.newLine();
            //刷新缓冲区
            bw.flush();
        }
        //释放资源
        bw.close();
    }
}

二、复制单级文件夹
[Java] 纯文本查看 复制代码
package copybufferedstream;
//复制单级文件夹
import java.io.*;

//复制单级文件夹
public class CopyFolder {
    public static void main(String[] args) throws IOException  {
        //创建数据源File对象,指向指定的目录路径,用来复制该文件夹下的所有文件
        File srcFolder = new File("F:\\itcast");
        //创建目的地File对象,
        File destFolder = new File("day10");
        //获取数据源File对象的目录的名称,添加到新的目的地File对象中
        String srcFolderName = srcFolder.getName();
        File newFolder = new File(destFolder,srcFolderName);
        //判断在目的地的目录下有没有该文件夹,没有就创建该文件夹
        if (!newFolder.exists()){
            newFolder.mkdir();
        }
        //获取数据源File对象的文件夹下的所有文件,遍历File对象数组,
        // 把每个文件都复制到新目的地File对象的文件夹下
        File[] files = srcFolder.listFiles();
        for (File file : files){
            copyFile(file,new File(newFolder,file.getName()));
        }
    }
    //因为文件夹下包含视频和图片,使用字节缓冲流来复制文件
    public static void copyFile(File srcFile,File destFile) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));

        byte[] bys = new byte[1024];
        int len;
        while((len = bis.read(bys))!=-1){
            bos.write(bys,0,len);
        }


        bis.close();
        bos.close();
    }
}

三、复制多级文件夹
[Java] 纯文本查看 复制代码
package copybufferedstream;
//复制多级文件夹
import java.io.*;

public class CopyFolders {
    public static void main(String[] args) throws IOException {
        //创建数据源File对象,
        File srcFolder = new File("E:\\itcast");
        //创建目的地File对象
        File destFolder = new File("F:\\");
        CopyFolder(srcFolder, destFolder);
    }
    //复制文件夹
    private static void CopyFolder(File srcFolder, File destFolder) throws IOException {
        //判断shujuyuanFile对象是否是文件夹
        if (srcFolder.isDirectory()) {
            //是,就把数据源File对象的文件夹名称,复制到新的目的地File对象(目的地File对象的上级目录下面)
            String srcFolderName = srcFolder.getName();
            File newFolder = new File(destFolder, srcFolderName);
            //判断新的目的地File对象的文件夹是否已经存在,不存在就创建文件夹
            if (!newFolder.exists()) {
                newFolder.mkdir();
            }
            //创建数据源File对象的File数组
            File[] files = srcFolder.listFiles();
            //获取数据源File对象的File数组的每个子File对象
            for (File file : files) {
                //使用递归,调用本方法CopyFolder(),把每个文件夹地下的子文件夹都复制到目的地的File对象的上级目录下面
                CopyFolder(file, newFolder);
            }
            //不是,就把数据源File对象的文件复制到目的地File对象的上级目录下面
        } else {
            copyFile(srcFolder, new File(destFolder, srcFolder.getName()));
        }
    }
    //复制文件方法
    private static void copyFile(File srcFolder, File file) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFolder));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));

        byte[] bys = new byte[1024];
        int len;
        while ((len = bis.read()) != -1) {
            bos.write(bys, 0, len);
        }

        bos.close();
        bis.close();
    }
}

四、复制文件的异常处理
[Java] 纯文本查看 复制代码
package copyfileioexception;

import java.io.*;

public class CopyFile  {
    public static void main(String[] args)  {

    }
    //JDK9处理异常方案
    public static void copy3() throws FileNotFoundException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("day09\\students.txt"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day09\\copystudents.txt"));
        try (bis;bos) {
            byte[] bys = new byte[1024];
            int len;
            while ((len = bis.read(bys)) != -1) {
                bos.write(bys, 0, len);
            }
        }catch (IOException ioe){
            ioe.printStackTrace();
        }
    }

    //JDK7的处理异常方案
    public static void copy2() {
        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("day09\\students.txt"));
             BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day09\\copystudents.txt"));) {

            byte[] bys = new byte[1024];
            int len;
            while ((len = bis.read(bys)) != -1) {
                bos.write(bys, 0, len);
            }
        }catch (IOException ioe){
            ioe.printStackTrace();
        }
    }

    //JDK最原始处理异常标准方案
    public static void copy1() {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream("day09\\students.txt"));
            bos = new BufferedOutputStream(new FileOutputStream("day09\\copystudents.txt"));

            byte[] bys = new byte[1024];
            int len;
            while ((len = bis.read(bys)) != -1) {
                bos.write(bys, 0, len);
            }

        } catch (IOException ioe){
            ioe.printStackTrace();
        } finally {
            if (bis != null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bos != null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //抛出异常
    public static void copy() throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("day09\\students.txt"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day09\\copystudents.txt"));

        byte[] bys = new byte[1024];
        int len;
        while ((len=bis.read(bys)) != -1){
            bos.write(bys,0,len);
        }

        bis.close();
        bos.close();
    }




}

五、标准输入流
[Java] 纯文本查看 复制代码
package system;

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

/*
标准输入流的使用步骤
     1.我们可以自己实现键盘录入输入。
        BufferedReader(InputStreamReader(System.in));
        调用readtLine()方法接收数据
     2.但是上面的方式太麻烦了,所以我们直接使用键盘输入的类
        Scanner sc = new Scanner(System.in);

 */
public class SystemIn {
    public static void main(String[] args) throws IOException {
        //InputStream is = System.in;
        //一次读取一个字节,用标准输入流
        /*int by;
        while((by=is.read())!=-1){
            System.out.print((char)by);
        }*/
        //一次读取一个字节数组
        /*byte[] bys = new byte[1024];
        int len;
        while((len=is.read(bys))!= -1){
            System.out.print(new String(bys,0,len));
        }*/
        //使用转换流--字符缓冲输入流,为了让键盘录入能写中文
        /*InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);*/
        /*BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String line;
        //br.readLine(),放回读取一行的字符串,不会读取没有换行
        while((line = br.readLine())!=null){
            System.out.println(line);
        }*/
        //但是上面的方式太麻烦了,所以我们直接使用键盘输入的类
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        System.out.println(s);
    }
}

六、标准输出流
[Java] 纯文本查看 复制代码
package system;

import java.io.PrintStream;

/*
标准输出流
      输出语句本质就是一个标准输出流
      创建标准输出流对象:PrintStream ps = System.out;
      调用print()方法可以输出不带换行效果的数据
      调用println()方法可以输出带换行效果的数据

 */
public class SystemOut {
    public static void main(String[] args) {
        //System.out,的print()方法把数据打印在控制台
        PrintStream ps = System.out;
        ps.print("a");
        ps.println("bbb");
    }
}

七、字节打印流
[Java] 纯文本查看 复制代码
package print;

import java.io.IOException;
import java.io.PrintStream;

/*
字节打印流的使用
      1.创建字节打印流对象  PrintStream ps =new PrintStream(文件路径);
      2.调用字节流写出数据的方法:write()写数据。会根据编码转换
      3.调用print()方法写出数据,不会进行转码,不会换行
      4.调用println()方法写出数据,不会进行转码,会换行

 */
public class PrintStreamTest {
    public static void main(String[] args) throws IOException {
        //1.创建字节打印流对象  PrintStream ps =new PrintStream(文件路径);
        PrintStream ps = new PrintStream("day10\\ps.txt");
        //因为字节打印流printStream,本质是字节输出流
        // 调用其write方法会写入字节
        ps.write(97);
        //print(),里面必须写数据
        ps.print("97");
        ps.println(97);
        ps.close();
    }
}

八、字符打印流
[Java] 纯文本查看 复制代码
package print;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/*
字符打印流的使用
      1.创建字符打印流对象: PrintWriter ps = new PrintWrtier(文件路径);
      2.调用write()方法,写出数据。并写出换行。
      3.调用flush()方法,将数据刷新到文件中。
      4.创建字符打印流对象:PrintWriter pw = new PrintWriter(文件路径,true);
      5.调用println()方法,写出数据

 */
public class PrintWriterTest {
    public static void main(String[] args) throws IOException {
        //1.创建字符打印流对象: PrintWriter ps = new PrintWrtier(文件路径);
        /*PrintWriter pw = new PrintWriter("day10\\pws.txt");
        //2.调用write()方法,写出数据。并写出换行。
        pw.write("sad");
        pw.write("\r\n");
        pw.write("12312");
        //3.调用flush()方法,将数据刷新到文件中。
        pw.flush();
        pw.close();
        */

        //4.创建字符打印流对象:PrintWriter pw = new PrintWriter(文件路径,true);
        PrintWriter pw = new PrintWriter(new FileWriter("day10\\pwss.txt"),true);
        // 5.调用println()方法,写出数据
        pw.println("asdasdas");
        pw.println("asdasdas");

        pw.close();
    }
}

九、复制Java文件打印流改进版
[Java] 纯文本查看 复制代码
package print;
//使用字符打印流,复制文件
/*
打印流复制java文件的重要步骤
       1.创建高效字符输入流对象,关键读取的文件
       2.创建字符打印流对象PrintWriter(文件路径,true)
       3.循环读写文件数据
       4.释放资源

 */
import java.io.*;

public class CopyFile {
    public static void main(String[] args) throws IOException {
        //使用字符缓冲流,复制文件
        /*BufferedReader br = new BufferedReader(new FileReader("day10\\src\\bufferedwriterreader\\Student.java"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("day10\\pw.txt"));
        
        String line;
        while((line = br.readLine())!=null){
            bw.write(line);
            bw.newLine();
            bw.flush();
        }
        
        br.close();
        bw.close();*/
        
        //使用字符缓冲输入流读取数据,再用字符打印流,来写入数据
        //1.创建高效字符输入流对象,关键读取的文件
        BufferedReader br = new BufferedReader(new FileReader("day10\\src\\bufferedwriterreader\\Student.java"));
        //2.创建字符打印流对象PrintWriter(文件路径,true)
        PrintWriter pw = new PrintWriter(new FileWriter("day10\\pw.txt"),true);
        //3.循环读写文件数据
        String line;
        while((line = br.readLine())!= null){
            pw.println(line);
        }
        //4.释放资源
        br.close();
        pw.close();
    }
}

十、对象序列化流
用处:用来把对象写入文本中,会生成serialVersionUID,
[Java] 纯文本查看 复制代码
package objectserializable;

import java.io.Serializable;

/*
1.定义一个学生类,提供name和age两个成员变量。
         并实现Serializable接口
 */
public class Student implements Serializable {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    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;
    }
}

package objectserializable;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

/*
对象序列化的基本使用
      1.定义一个学生类,提供name和age两个成员变量。
         并实现Serializable接口
      2.创建对象输出流:ObjectOutputStream
      3.创建一个学生对象
      4.调用writeObject()方法,将学生对象写出
      5.释放资源

 */
public class ObjectOutputStreamTest {
    public static void main(String[] args) throws IOException {
        //2.创建对象输出流:ObjectOutputStream
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day10\\oos.txt"));
        //3.创建一个学生对象
        Student s = new Student("张三",21);
        //4.调用writeObject()方法,将学生对象写出
        oos.writeObject(s);
        //5.释放资源
        oos.close();
    }
}
十一、对象反序列化流
用处:用来把文本中的对象信息读取出来,存储到对象,会根据文本中的serialVersionUID来读取文本
[Java] 纯文本查看 复制代码
package objectserializable;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

/*
对象反序列化流的基本使用
      1.创建对象反序列化流:ObjectInputStream()
      2.调用readObject()方法。读取对象数据
      3.将读取的对象向下转型
      4.输出对象中的内容
      5.释放资源

 */
public class ObjectInputStreamTest {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //1.创建对象反序列化流:ObjectInputStream()
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day10\\oos.txt"));
        //2.调用readObject()方法。读取对象数据
        Object o = ois.readObject();
        //3.将读取的对象向下转型
        Student s = (Student)o;
        //4.输出对象中的内容
        System.out.println(s.getName()+","+s.getAge());
        //5.释放资源
        ois.close();
    }
}
十二、serialVersionUID&transientserialVersionUID
序列化号可以解决类文件不一致的问题
transient
如果一个成员不想参与序列化操作,可以使用该关键字
[Java] 纯文本查看 复制代码
package objectserializable;

import java.io.Serializable;

/*
1.定义一个学生类,提供name和age两个成员变量。
         并实现Serializable接口
         定义一个学生类,提供姓名和年龄两个属性
    生成序列化id号。使用transient瞬态关键字修饰某个属性
 */
public class Student implements Serializable {
    private static final Long serialVersionUID = 42L;
    private String name;
    private transient int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    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;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}


package objectserializable;

import java.io.*;

/*
定义一个学生类,提供姓名和年龄两个属性
    生成序列化id号。使用transient修饰某个属性
创建学生对象
创建序列化流对象
进行读写对象操作
释放资源

 */
public class ObjectStreamTest {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //创建学生对象
        Student s = new Student("张三",20);
        //创建序列化流对象
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day10\\ooss.txt"));
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day10\\ooss.txt"));
        //进行读写对象操作
        oos.writeObject(s);
        Object o = ois.readObject();
        Student stu =(Student)o;

        System.out.println(s.getName()+","+s.getAge());

        //释放资源
        oos.close();
        ois.close();
    }
}
十三、Properties作为Map集合的使用
[Java] 纯文本查看 复制代码
package properties;

import java.util.Properties;
import java.util.Set;

/*
Properties作为Map集合的基本使用
      1.创建一个Properties集合
      2.调用put()方法,添加一些键值对数据
      3.通过keySet()方式遍历集合
      4.输出键和值数据

 */
public class PropertiesMap {
    public static void main(String[] args) {
        //1.创建一个Properties集合,因为他的类没有加泛型,所以不需要加<>
        Properties pps = new Properties();
        //2.调用put()方法,添加一些键值对数据
        pps.put("001","张三");
        pps.put("002","李四");
        pps.put("003","王五");
        //3.通过keySet()方式遍历集合
        Set<Object> keySet = pps.keySet();
        for (Object key : keySet){
            Object value = pps.get(key);
            //4.输出键和值数据
            System.out.println(keySet+","+value);
        }
    }
}

十四、Properties作为Map集合的特有方法
[Java] 纯文本查看 复制代码
package properties;

import java.util.Properties;
import java.util.Set;

/*
Properties集合常用方法
     setProperty(String k,String v)        向集合添加键值对数据
     getProperty(String k)                     根据键获取值
     stringPropertyNames()                  获取所有键的集合

 */
public class PropertiesMapTest {
    public static void main(String[] args) {
        Properties pps = new Properties();
        //setProperty(String k,String v)        向集合添加键值对数据
        pps.setProperty("001","张三");
        pps.setProperty("002","李四");
        pps.setProperty("003","王五");
        //getProperty(String k)                     根据键获取值
        String key = pps.getProperty("001");
        System.out.println(key);
        //stringPropertyNames()                  获取所有键的集合
        Set<String> propertyNames = pps.stringPropertyNames();
        for (String key1 : propertyNames){
            String value = pps.getProperty(key1);
            System.out.println(key1+","+value);
        }

        //System.out.println(pps);
    }
}

十五、Properties和IO流相结合的方法
[Java] 纯文本查看 复制代码
package properties;
/*
Properties和IO流相结合的方法
     load(Reader r)          通过字符流加载数据到集合中
     load(InputStream is) 通过字节流加载数据到集合中
     store(Writer w,String ms)   通过字符输出流将集合数据写出文件
     store(OutputStream os,String ms)  通过字节输出流将集合数据写出文件

 */
import java.io.*;
import java.util.Properties;

public class TXTToProperties {
    public static void main(String[] args) throws IOException {
        //myStore();
        //myStore1();
        //myLoad();
        myLoad1();
    }
    public static void myStore() throws IOException {
        Properties pps = new Properties();
        pps.setProperty("001","张三");
        pps.setProperty("002","李四");
        pps.setProperty("003","王五");
        //store(Writer w,String ms)   通过字符输出流将集合数据写出文件
        // 因为是字节流,索引在文件中写入的数据不好看,但是用load()在读取文件数据,输出在控制台是没问题的
        FileWriter fw = new FileWriter("day10\\fw.txt");

        pps.store(fw,"人物信息");
        fw.close();
    }
    public static void myStore1() throws IOException {
        Properties pps = new Properties();
        pps.setProperty("001","张三");
        pps.setProperty("002","李四");
        pps.setProperty("003","王五");
        //store(OutputStream os,String ms)  通过字节输出流将集合数据写出文件
        FileOutputStream fos = new FileOutputStream("day10\\fos.txt");
        pps.store(fos,"personMessege");
        fos.close();
    }

    public static void myLoad() throws IOException{
        Properties pps = new Properties();
        //load(Reader r)          通过字符流加载数据到集合中
        FileReader fr = new FileReader("day10\\fw.txt");
        pps.load(fr);
        fr.close();
        System.out.println(pps);
    }
    public static void myLoad1() throws IOException{
        Properties pps = new Properties();
        //load(InputStream is) 通过字节流加载数据到集合中
        FileInputStream fis = new FileInputStream("day10\\fos.txt");
        pps.load(fis);
        fis.close();
        System.out.println(pps);
    }
}

十六、游戏次数
[Java] 纯文本查看 复制代码
package properties;

import java.util.Random;
import java.util.Scanner;

/*
游戏次数的重要步骤
       1.准备一个玩游戏的类,提供猜数字游戏方法


 */
public class Game {
    public static void playGame(){
        Random r = new Random();
        int num = r.nextInt(100) + 1;
        Scanner sc = new Scanner(System.in);
        while (true){
            System.out.println("请输入一个数字:");
            int num1 = sc.nextInt();
            if (num1 > num){
                System.out.println("你输入的数字"+num1+"大了");
            }else if (num1 < num){
                System.out.println("你输入的数字"+num1+"小了");
            } else{
                System.out.println("恭喜你,猜中了");
                break;
            }
        }
    }
}


package properties;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

/*
        2.准备一个测试类,提供主方法
       3.在测试类中,通过Properties集合加载game.txt文件数据
       4.判断次数是否3次。
          如果是:提示试玩结束。想继续玩请充值
          如果不是:玩游戏。并将次数+1重新写回到game.txt文件中
 */
public class PropertiesTest {
    public static void main(String[] args) throws IOException {
        //在测试类中,通过Properties集合加载game.txt文件数据
        FileReader fr = new FileReader("day10\\game.txt");
        Properties pps = new Properties();
        pps.load(fr);
        int number = Integer.parseInt(pps.getProperty("count"));
        //判断次数是否3次。
        if (number >= 3){
            //如果是:提示试玩结束。想继续玩请充值
            System.out.println("试玩结束。想继续玩请充值");
        }else {
            //如果不是:玩游戏。并将次数+1重新写回到game.txt文件中
            Game.playGame();
            number++;
            pps.setProperty("count",String.valueOf(number));
            FileWriter fw = new FileWriter("day10\\game.txt");
            pps.store(fw,null);
        }
        fr.close();
    }
}













0 个回复

您需要登录后才可以回帖 登录 | 加入黑马