码迷,mamicode.com
首页 > 其他好文 > 详细

Io流Scanner

时间:2018-07-27 01:12:16      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:bre   generate   turn   imp   code   sys   集合   equal   finally   

package scanner;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.TreeSet;

public class Demo {
    public static void main(String[] args) {
        //定义学生的集合
        TreeSet<Student> ts = new TreeSet<Student>();
        
        //引入Scanner对象
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生的数量");
        int count = sc.nextInt();
        for(int i=0;i<count;i++) {
            sc = new Scanner(System.in);
            System.out.println("请输入学生的姓名");
            //学生的姓名
            String name = sc.nextLine();
            //学生的年龄
            System.out.println("请输入学生的年龄");
            Integer age = sc.nextInt();
            System.out.println("请输入学生的ID");
            Integer id = sc.nextInt();
            Student s = new Student();
            s.setId(id);
            s.setAge(age);
            s.setName(name);
            ts.add(s);
        }
        
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter("f.txt"));
            for(Student s:ts) {
            bw.write(s.getName()+"---------"+s.getAge()+"---------"+s.getId());
                bw.newLine();
            }
            bw.flush();
        }catch(IOException e) {
            e.printStackTrace();
        }finally {
            if(bw != null)
            {
                try {
                    bw.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        System.out.println("ojbk");
    }
}
package scanner;

public class Student implements Comparable<Student>{
    private Integer Id;
    
    private String name;
    
    private Integer age;

    public Integer getId() {
        return Id;
    }

    public void setId(Integer id) {
        Id = id;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public int compareTo(Student o) {
        int val  = this.Id-o.getId();
        if(val==0)
        {
            val = this.age - o.getAge();
            if(val==0) {
            val = this.name.compareTo(o.getName());
            }
        }
        return val;
    }
    
}
package scanner1;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class ScannerDemo {
    public static void main(String[] args) {
        
        System.out.println("请输入");
        Scanner sc = new Scanner(System.in);
        BufferedWriter bw = null;
        try {
                bw = new BufferedWriter(new FileWriter("g.txt"));
                String line = null;
                
                while((line = sc.nextLine()) != null ) {
                    if("exit".equals(line)) {
                        break;
                    }
                    bw.write(line);
                    bw.newLine();
                    bw.flush();
                }
                
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}
package scanner1;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class ScannerDemo2 {
    public static void main(String[] args) {
        
        System.out.println("请输入");
        Scanner sc = new Scanner(System.in);
        BufferedWriter bw = null;
        try {
                bw = new BufferedWriter(new FileWriter("a.txt"));
                String line = null;
                while((line = sc.nextLine())!=null) {
                    if("exit".equals(line)) {
                        break;
                    }
                    bw.write(line);
                    bw.newLine();
                    bw.flush();
                }
                
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

 

Io流Scanner

标签:bre   generate   turn   imp   code   sys   集合   equal   finally   

原文地址:https://www.cnblogs.com/java-jiangtao-home/p/9374949.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!