java入门~第二十二天 字符流

字符流

1.字符流出现的原因及编码表概述和常见编码表

字符流出现的原因:由于字节流操作中文不是特别方便,所以,java就提供了字符流。

字符流: 字符流 = 字节流 + 编码表(只能对文本文件进行读写)

2.String类中的编码和解码问题

1.编码: 就是把字符串转换成字节数组。

如何把一个字符串转换成一个字节数组

public byte[] getBytes();使用平台的默认字符集将此 String编码为 byte 序列,并将结果存储到一个新的 byte 数组中。

public byte[] getBytes(String charsetName) 使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。

2.解码: 把字节数组转换成字符串

public String(byte[] bytes):通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。

public String(byte[] bytes, String charsetName) 通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String。

使用什么字符集进行编码,那么就是使用什么字符集进行解码

编码:把看得懂的变成看不懂的: String – byte[]
解码:把看不懂的变成看得懂的: byte[] – String

3.乱码:编解码没有采用同一张码表

public class MyTest {
 public static void main(String[]args)throws UnsupportedEncodingException {
        byte[] bytes = "皮卡丘".getBytes("GBK");
        for (byte aByte : bytes) {
            System.out.println(aByte);//-58
									  //-92
									  //-65
									  //-88
									  //-57
									  //-16
        }
        System.out.println("=================================");
        String s = new String(bytes,"GBK");
        System.out.println(s);//皮卡丘
    }
}

3.转换流的使用

3.01OutputStreamWriter

OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的 码表 将要写入流中的字符编码成字节。它使用的字符集可以由名称指定或显式给定,否则将接受平台默认的字符集。

1.OutputStreamWriter的构造方法
OutputStreamWriter(OutputStream out):根据默认编码(GBK)把字节流的数据转换为字符流
OutputStreamWriter(OutputStream out,String charsetName):根据指定编码把字节流数据转换为字符流

3.02字符流的5种写数据的方式

方法概述:
public void write(int c) 写一个字符
public void write(char[] cbuf) 写一个字符数组
public void write(char[] cbuf,int off,int len) 写一个字符数组的 一部分
public void write(String str) 写一个字符串
public void write(String str,int off,int len) 写一个字符串的一部分

public class MyTest3 {
    public static void main(String[] args) throws IOException {
        //创建使用默认字符编码的 OutputStreamWriter。
        //输出流所关联的文件,如果不存在,会自动创建
        OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("b.txt"), "UTF-8");
        writer.write("江畔何人初见月,江月何年初照人。");
        writer.flush();
        writer.close();

    }
}

字符流记得刷新

java.io.OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("a.txt",true));
//一次写一个字符
writer.write('a');
writer.write('a');
writer.flush();
writer.write('a');
writer.write('a');
writer.flush();
writer.write('a');
writer.flush();
writer.write("家有小女初长成,养在深闺人未识");
writer.write("\r\n");
writer.write(new char[]{'好','好','学','习'});
writer.write("\r\n");
writer.write("天生丽质难自弃,一朝选在君王侧,从此君王不早朝",0,7);
writer.write("\r\n");
writer.write(new char[]{'好', '好', '学', '习'},0,2);
writer.flush(); //字符流记得刷新一下
writer.close(); //刷新并关闭。

3.03转换流InputStreamReader的使用

InputStreamReader字节流通向字符流的桥梁。

InputStreamReader的构造方法:
InputStreamReader(InputStream is):用默认的编码(GBK)读取数据。

​ InputStreamReader(InputStream is,String charsetName):用指定的编码读取数据。

3.04字符流的2种读数据的方式

方法概述:
public int read() 一次读取一个字符,如果没有读到 返回-1
public int read(char[] cbuf) 一次读取一个字符数组 如果没有读到 返回-1

public class MyTest {
    public static void main(String[] args) throws IOException {
       /* InputStreamReader 是字节流通向字符流的桥梁:
        它使用指定的 charset 读取字节并将其解码为字符。
        它使用的字符集可以由名称指定或显式给定,或者可以接受平台默认的字符集。*/
        //输入流,所关联的文件,如果不存在,就会报错。
        InputStreamReader reader = new InputStreamReader(new FileInputStream("c.txt"));
        int ch = reader.read();
        System.out.println(ch);
        //如果读取不到有效字符,返回 -1 我们使用 -1 来判断文件是否读取完。
        ch = reader.read();
        System.out.println(ch);
    }
}
public static void main(String[] args) throws IOException {
    InputStreamReader reader = new InputStreamReader(new FileInputStream("c.txt"));
    char[] chars = new char[1000];
    //一次读取一个字符数组,返回值,返回的是读取到的有效的字符个数
      // int len = reader.read(chars);
    int len = reader.read(chars,0,3);
    System.out.println(len);
    for (char aChar : chars) {
        System.out.println(aChar);
    }

    reader.close();
}

3.05字符流复制文本文件

public static void main(String[] args) throws IOException {
    //一次读取一个字符,写一个字符来复制文本文件
    InputStreamReader in = new InputStreamReader(new FileInputStream("MyTest.java"));
    OutputStreamWriter out= new OutputStreamWriter(new FileOutputStream("C:\\Users\\S\\Desktop\\MyTest.java"));

    int ch=0;
    while ((ch=in.read())!=-1){
        out.write(ch);
        out.flush();
    }

    in.close();
    out.close();
}

方法2:

public static void main(String[] args) throws IOException {
    //一次读写一个字符数组,来复制
    //一次读取一个字符,写一个字符来复制文本文件
    InputStreamReader in = new InputStreamReader(new FileInputStream("MyTest.java"));
    OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("C:\\Users\\ShenMouMou\\Desktop\\MyTest.java"));

    char[] chars = new char[1000];
    int len=0;
    while ((len=in.read(chars))!=-1){
        // System.out.println("循环次数");
        out.write(chars,0,len);
        out.flush();
    }
    in.close();
    out.close();
}

4.FileWriter和FileReader复制文本文件

FileReader和FileWriter的出现:
转换流的名字比较长,而我们常见的操作都是按照本地默认编码实现的,所以,为了简化我们的书写,转换流提供了对应的子类。 FileWriter FileReader

字符流便捷类: 因为转换流的名字太长了,并且在一般情况下我们不需要制定字符集,
于是java就给我们提供转换流对应的便捷类
转换流 便捷类

  • OutputStreamWriter ------- FileWriter

  • InputStreamReader ------- FileReader

    public class MyTest {
        public static void main(String[] args)  {
            FileReader in=null;
            FileWriter out=null;
    
            try {
                in = new FileReader("C:\\Users\\S\\Desktop\\MyTest.java");
                out = new FileWriter("MyTest.java");
    
                char[] chars = new char[1000];
                int len = 0;
                while ((len = in.read(chars)) != -1) {
                    // System.out.println("循环次数");
                    out.write(chars, 0, len);
                    out.flush();
                }
    
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                    if (out != null) {
                        out.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

5.字符缓冲流

5.01字符缓冲流的基本使用

BufferedWriter写出数据 高效的字符输出流
BufferedReader读取数据 高效的字符输入流

高效的字符流
高效的字符输出流: BufferedWriter
构造方法: public BufferedWriter(Writer w)
高效的字符输入流: BufferedReader
构造方法: public BufferedReader(Reader e)

5.02字符缓冲流复制文本文件

public class MyTest {
    public static void main(String[] args) throws IOException {
      /*  BufferedWriter(Writer out)
        创建一个使用默认大小输出缓冲区的缓冲字符输出流。*/
        BufferedWriter out = new BufferedWriter(new FileWriter("d.txt"));
       // BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("e.txt")));
        BufferedReader  in= new BufferedReader(new FileReader("MyTest.java"));

        char[] chars = new char[1000];
        int len = 0;
        while ((len = in.read(chars)) != -1) {
            // System.out.println("循环次数");
            out.write(chars, 0, len);
            out.flush();
        }

        out.close();
        in.close();
    }
}

5.03字符缓冲流的特殊功能

高效的字符流,特有的方法 void newLine () 写入一个行分隔符

String readLine () 读取一个文本行

BufferedWriter out = new BufferedWriter(里面可以传字节流的子类也可以放字符流以及它的子类)

5.04字符缓冲流的特殊功能复制文本文件

public class MyTest2 {
    public static void main(String[] args) throws IOException {
        //高效的字符流,特有的方法
      /*  void newLine ()
        写入一个行分隔符。*/

       /* String readLine ()
        读取一个文本行。*/

        BufferedWriter out = new BufferedWriter(new FileWriter("ff.txt"));
        // BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("e.txt")));

        BufferedReader in = new BufferedReader(new FileReader("MyTest.java"));

        //读取一行,写入一行,来复制文件
        String line=null;
        while ((line=in.readLine())!=null){
            out.write(line);
            out.newLine();
            out.flush();
        }

        in.close();
        out.close();
    }
}

6.把集合中的数据存储到文本文件

public class MyTest {
    public static void main(String[] args) throws IOException {
        ArrayList<String> list = new ArrayList<>();
        list.add("贾宝玉");
        list.add("林黛玉");
        list.add("袭人");
        list.add("晴雯");

        BufferedWriter writer = new BufferedWriter(new FileWriter("hong.txt"));
        for (String name : list) {
            writer.write(name);
            writer.newLine();
            writer.flush();
        }
        writer.close();
    }//贾宝玉
 	 //林黛玉
 	 //袭人
 	 //晴雯

}

7.把文本文件中的数据存储到集合中

public static void main(String[] args) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader("hong.txt"));
    ArrayList<String> list = new ArrayList<>();
    while (true){
        String name = reader.readLine();
        if (name != null) {
            list.add(name);
        }else{
            break;
        }
    }

    System.out.println(list);
}

8.随机获取文本文件中的姓名

public class MyTest2 {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader("hong.txt"));
        ArrayList<String> list = new ArrayList<>();
        while (true){
            String name = reader.readLine();
            if (name != null) {
                list.add(name);
            }else{
                break;
            }
        }

        System.out.println(list);
        //随机取一个人
        Random random = new Random();
        int index = random.nextInt(list.size());
        //list.size()列表元素中的个数
        String s = list.get(index);
        System.out.println(s);
        //这种设计,是要把数据和程序解耦

    }
}

9.复制单级文件夹

需求: 复制D:\course这文件夹到E:\course

  • 分析:

  • a: 封装D:\course为一个File对象

  • b: 封装E:\course为一个File对象,然后判断是否存在,如果不存在就是创建一个目录

  • c: 获取a中的File对应的路径下所有的文件对应的File数组

  • d: 遍历数组,获取每一个元素,进行复制

  • e: 释放资源

    public class Mytest {
        public static void main(String[] args) throws IOException {
            File srcFile = new File("C:\\Users\\Administrator\\Desktop\\作业");
            File desFile = new File("D:\\作业");
            if(!desFile.exists()){
                desFile.mkdirs();
            }
    
            copyFile( srcFile,desFile);
            System.out.println("复制完成");
        }
    
        private static void copyFile(File srcFile, File desFile) throws IOException {
            //list()和listFiles()的区别:
            /*1.返回值类型不同:前者为String数组,后者为File对象数组
            2.数组中的元素内容不同:前者为String类型的[文件名](包含后缀名),
            后者为File对象类型的[完整路径]。
            所以,遍历文件夹中所有的文件,包括子文件夹的文件时,必须用listFiles()方法。*/
            File[] files = srcFile.listFiles();
            for (File f : files) {
                if(f.isFile() ){
                    copyFiles(f,desFile);
                }else {
    
                }
            }
        }
    
        private static void copyFiles(File f, File desFile) throws IOException {
            FileInputStream fileIn= new FileInputStream(f);
            FileOutputStream fileOut = new FileOutputStream(new File(desFile,f.getName()));
            byte[] bytes = new byte[1024 * 4];
            int len=0;
            while ((len=fileIn.read(bytes))!=-1){
                fileOut.write(bytes,0,len);
                fileOut.flush();
            }
            fileIn.close();
            fileOut.close();
        }
    }
    

10.复制指定目录下指定后缀名的文件并修改名称

需求: 复制D:\demo目录下所有以.java结尾的文件到E:\demo .并且将其后缀名更改文.jad

public class Mytest1 {
        public static void main(String[] args) throws IOException {
            File file = new File("C:\\Users\\Administrator\\Desktop\\作业");
            File file2 = new File("C:\\Users\\Administrator\\Desktop\\作业1");
            if(!file2.exists()){
                file2.mkdirs();
            }
            复制(file, file2);
        }
        private static void 复制(File file, File file2) throws IOException {
            File[] files = file.listFiles();
            for (File file1 : files) {
                if (file1.isFile()) {
                    if (file1.getAbsolutePath().endsWith(".jpg")) {
                        String s = file1.getAbsolutePath().substring(0, file1.getAbsolutePath().lastIndexOf("."));
                        File newfile = new File(s + ".png");
                        //file1.renameTo(newfile);
                        FileInputStream in = new FileInputStream(file1);
                        FileOutputStream out = new FileOutputStream(new File(file2.getAbsolutePath()+"\\"+newfile.getName()));
                        byte[] bytes = new byte[1024 * 8];
                        int len = 0;
                        while ((len = in.read(bytes)) != -1) {
                            out.write(bytes, 0, len);
                        }
                        in.close();
                        out.close();

                    } else {
                        FileInputStream in = new FileInputStream(file1);
                        FileOutputStream out = new FileOutputStream(new File(file2.getAbsolutePath()+"\\"+file1.getName()));
                        byte[] bytes = new byte[1024 * 8];
                        int len = 0;
                        while ((len = in.read(bytes)) != -1) {
                            out.write(bytes, 0, len);
                        }
                        in.close();
                        out.close();
                    }
                } else {
                    if (file1.isDirectory()) {

                        File file3 = new File(file2.getAbsolutePath()+"\\"+file1.getName());
                        file3.mkdirs();
                        复制(file1, file3);
                    }
                }
            }
        }
}

11.键盘录入学生信息按照总分排序并写入文本文件

需求:键盘录入3个学生信息(姓名,语文成绩(chineseScore),数学成绩(mathScore),英语成绩(englishScore)),按照总分从高到低存入文本文件

  • 分析:
  • a: 创建一个学生类: 姓名,语文成绩(chineseScore),数学成绩(mathScore),英语成绩(englishScore)
  • b: 因为要排序,所以需要选择TreeSet进行存储学生对象
  • c: 键盘录入学生信息,把学生信息封装成一个学生对象,在把学生对象添加到集合中
  • d: 创建一个高效的字符输出流对象
  • e: 遍历集合,把学生的信息写入到指定的文本文件中
  • f: 释放资源
public class MyTest {
    public static void main(String[] args) throws IOException {
        TreeSet<Student> treeSet = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                int num = s1.getTotalScore() - s2.getTotalScore() == 0 ? s1.getName().compareTo(s2.getName()) : s1.getTotalScore() - s2.getTotalScore();
                return -num;
            }
        });
        for (int i = 1; i <= 3; i++) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入第" + i + "个学生的名字");
            String name = scanner.nextLine();
            Student student = new Student();
            student.setName(name);
            System.out.println("请输入第" + i + "个学生的语文成绩");
            int yw = scanner.nextInt();
            student.setChineseScore(yw);

            System.out.println("请输入第" + i + "个学生的数学成绩");
            int sx = scanner.nextInt();
            student.setMathScore(sx);

            System.out.println("请输入第" + i + "个学生的英语成绩");
            int yy = scanner.nextInt();
            student.setEnglishScore(yy);

            //把学生添加集合中
            treeSet.add(student);

        }
        //把学生成绩,存到文本文件中
         // BufferedWriter bfw = new BufferedWriter(new FileWriter("score.txt",true));
        long time = System.currentTimeMillis();
        String timeStr = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒").format(new Date(time));
        String fileName= timeStr+"-score.txt";
        BufferedWriter bfw = new BufferedWriter(new FileWriter(fileName));
        bfw.write("序号\t姓名\t语文\t数学\t英语\t总分");
        bfw.newLine();
        bfw.flush();
        int i=1;
        int j=1;
        for (Student student : treeSet) {
            System.out.println((j++) + "\t" + student.getName() + "\t" + student.getChineseScore() + "\t" + student.getMathScore() + "\t" + student.getEnglishScore() + "\t" + student.getTotalScore());
           bfw.write((i++)+"\t"+student.getName()+"\t"+student.getChineseScore()+"\t"+student.getMathScore()+"\t"+student.getEnglishScore()+"\t"+student.getTotalScore());
            bfw.newLine();
            bfw.flush();
        }

        bfw.close();
    }
}
//序号	姓名	语文	数学	英语	总分
//1	     张三	  89	98	 78	 265
//2		 小小	  78	99	 88	 265
//3		 小熊	  86	79	 96	 261

练习: 1.给了一个文本文件是GBK编码。你把这个文件中的4句,复制到另外一个文本文件,这个文件的编码是UTF-8,而且四句诗要反转。

public class Mytest4 {
    public static void main(String[] args) throws IOException {
        BufferedReader a1 = new BufferedReader(new InputStreamReader(new FileInputStream("诗.txt"),"gbk"));
        PrintWriter w1 = new PrintWriter(new OutputStreamWriter(new FileOutputStream("诗2.txt"),"utf-8"));
        List<String> list = new ArrayList<>();
        while(true) {
            String line = a1.readLine();
            if(line == null) {
                break;
            }
            list.add(line);
        }
        Collections.reverse(list);
        for (String line : list) {
            w1.println(line);
        }
        w1.close();
        a1.close();
    }
}
//床前明月光                 //低头思故乡
//疑是地上霜					//举头望明月
//举头望明月					//疑是地上霜
//低头思故乡					//床前明月光


​ 2.武力值排名前三的值是多少

​ 3.求武力值最高3人名字

​ 4.统计各个地区的武将

​ 5.统计各个年龄段的武将 比如:

​ 10—19岁,20-29岁,30~39岁 40~49岁 50~59岁 60岁以上
​ 6.统计各个年龄段的武将男女比例

​ 7统计平均年龄

​ 8.统计平均年龄最高的地区

public class Mytest5 {
    public static void main(String[] args) throws IOException {

        List<Person> list = question1();
        /*for (Person person : list) {
            System.out.println(person);
        }*/
        System.out.println("=========================== 武力前三");
        question2(list);
        System.out.println("=========================== 武力排名前三");
        question3(list);
        System.out.println("=========================== 统计各个地区的武将");
        Map<String, List<String>> locationMap = question4(list);
        System.out.println(locationMap.get("平原"));

        System.out.println("=========================== 男女比例");
        Map<String, List<Person>> sexMap = question5(list);
        System.out.println(sexMap.get("男").size() +":" +sexMap.get("女").size());

        System.out.println("=========================== 统计各个年龄段的武将");
        Map<String, List<String>> ageMap = question6(list);
        for (Map.Entry<String, List<String>> entry : ageMap.entrySet()) {
            System.out.println(entry.getKey());
            System.out.println("\t"+entry.getValue());
        }

        System.out.println("=========================== 统计各个年龄段的武将男女比例");
        Map<String,Map<String,List<String>>> ageMap2 = question7(list);
        for (Map.Entry<String, Map<String, List<String>>> entry : ageMap2.entrySet()) {
            System.out.print(entry.getKey());
            System.out.println("\t"+entry.getValue().get("男").size()+":"+entry.getValue().get("女").size());
        }

        System.out.println("=========================== 统计平均年龄");
        System.out.println(avgAge(list));

        System.out.println("=========================== 统计平均年龄最高的地区");
        System.out.println(locationAvgAge(list));

    }

    private static String locationAvgAge(List<Person> list) {
        Map<String,List<Person>> locationMap = new HashMap<>();
        for (Person p : list) {
            List<Person> people = locationMap.get(p.getLocation());
            if(people==null) {
                people = new ArrayList<>();
                locationMap.put(p.getLocation(), people);
            }
            people.add(p);
        }
        TreeMap<Double,String> map = new TreeMap<>((a, b)->b.compareTo(a));
        for (Map.Entry<String, List<Person>> entry : locationMap.entrySet()) {
            List<Person> people = entry.getValue();
            double totalAge = 0;
            for (Person p : people) {
                totalAge += p.getAge();
            }
            map.put(totalAge/ people.size(),entry.getKey());
        }
        return map.firstEntry().getValue();
    }

    private static double avgAge(List<Person> list) {
        double totalAge = 0;
        for (Person p : list) {
            totalAge += p.getAge();
        }
        return totalAge/list.size();
    }

    private static Map<String,Map<String,List<String>>> question7(List<Person> list) {
        Map<String,Map<String,List<String>>> map = new TreeMap<>();
        for (int i = 10; i < 70; i+=10) {
            Map<String,List<String>> sexMap = new HashMap<>();
            sexMap.put("男", new ArrayList<>());
            sexMap.put("女", new ArrayList<>());
            map.put(ageRange(i), sexMap);
        }
        for (Person p : list) {
            List<String> people = map.get(ageRange(p.getAge())).get(p.getSex());
            if(people != null) {
                people.add(p.getName());
            }
        }
        return map;
    }

    private static Map<String, List<String>> question6(List<Person> list) {
        Map<String,List<String>> map = new TreeMap<>();
        for (int i = 10; i < 70; i+=10) {
            map.put(ageRange(i), new ArrayList<>());
        }
        for (Person p : list) {
            map.get(ageRange(p.getAge())).add(p.getName());
        }
        return map;
    }

    private static String ageRange(int age) {
        switch (age / 10) {
            case 1:
                return "10~19岁";
            case 2:
                return "20~29岁";
            case 3:
                return "30~39岁";
            case 4:
                return "40~49岁";
            case 5:
                return "50~59岁";
            default:
                return "60岁以上";
        }
    }

    private static Map<String, List<Person>> question5(List<Person> list) {
        Map<String,List<Person>> map = new HashMap<>();
        map.put("男", new ArrayList<>());
        map.put("女", new ArrayList<>());
        for (Person p : list) {
            map.get(p.getSex()).add(p);
        }
        return map;
    }

    private static Map<String, List<String>> question4(List<Person> list) {
        // 问题4
        Map<String,List<String>> map = new HashMap<>();
        for (Person p : list) {
            List<String> people = map.get(p.getLocation());
            if(people==null) {
                people = new ArrayList<>();
                map.put(p.getLocation(), people);
            }
            people.add(p.getName());
        }
        return map;
    }

    private static void question3(List<Person> list) {
        // 问题3
        // 先把所有武力值放入set,倒序,并去重
        TreeSet<Integer> set = new TreeSet<>((a, b)-> b-a);
        for(Person p: list){
            set.add(p.getForces());
        }
        // 取前三武力
        Set<Integer> top3 = new HashSet<>();
        for (int i = 0; i < 3; i++) {
            top3.add(set.pollFirst());
        }
        for(Person p : list) {
            if(top3.contains(p.getForces())){
                System.out.println(p.getName());
            }
        }
    }

    private static void question2(List<Person> list) {
        // 问题2, 求武力最高3人 比较器的lambda写法教给了学员
        Collections.sort(list, (p1, p2)-> p2.getForces()-p1.getForces());
        for (int i = 0; i < 3; i++) {
            System.out.println(list.get(i).getName());
        }
    }

    public static List<Person> question1() throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("C:\\Users\\lenovo\\Desktop\\output.txt")));
        ArrayList<Person> list = new ArrayList<>();
        while(true) {
            String line = reader.readLine();
            if(line == null) {
                break;
            }
            String[] split = line.split("\t");
            Person p = new Person();
            p.setId(Integer.parseInt(split[0]));
            p.setName(split[1]);
            p.setLocation(split[2]);
            p.setSex(split[3]);
            p.setBirth(Integer.parseInt(split[4]));
            p.setDeath(Integer.parseInt(split[5]));
            p.setForces(Integer.parseInt(split[6]));
            list.add(p);
        }
        reader.close();
        return list;
    }
}
class Person {

    private int id;
    private String name;
    private String location;
    private String sex;
    private int death;
    private int birth;
    private int forces;
    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
    public int getAge() {
        return death-birth;
    }

    public int getDeath() {
        return death;
    }

    public void setDeath(int death) {
        this.death = death;
    }

    public int getBirth() {
        return birth;
    }

    public void setBirth(int birth) {
        this.birth = birth;
    }

    public int getForces() {
        return forces;
    }

    public void setForces(int forces) {
        this.forces = forces;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", location='" + location + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + (death-birth) +
                ", forces=" + forces +
                '}';
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值