java学习之java高级特性------File类输入输出流(补充)

3 篇文章 0 订阅
1 篇文章 0 订阅

相对路径,某个文件或文件夹相对于另一个文件或文件夹的路径

绝对路径:某个文件或文件夹相对于磁盘的路径,

 

Windows:使用(\)表示目录的分隔符

Linux:(/)

pathSeparator:指的是分隔连续多个路径字符串的分隔符,

separator:分隔同一个路径字串的目录

 //列出指定目录的所有文件
        File f = new File("d:"+File.separator+"Tools");
//        String []s = f.list();
//        for (String t:s
//        ) {
//            System.out.println(t);
//        }
//列出指定目录的所有文件内容需要用到递归调用
递归调用;自己调用自己
public void print(File file,int p){
        if(file==null) return;
        if(file.isFile())
        {
            for(int j = 0;j<p;j++){
                System.out.print("->");
            }
            System.out.println(file.getName());}
        else if(file.isDirectory()){
            for(int j = 0;j<p;j++){
                System.out.print("->");
            }
            System.out.println(file.getName());
            File f[] = file.listFiles();
            for(int i = 0;i<f.length;i++){

                print(f[i],p+1);
            }
        }
        }

小型学生管理系统(集合,File类以及对象输入输出流类进行磁盘读写)

package c0804test;

import java.io.Serializable;

public class Student implements Serializable {
    String id;
    String name;
    int age;

    public Student() {
    }

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

    public String getId() {
        return id;
    }

    @Override
    public String toString() {
        return this.id+"\t\t\t"+this.name+"\t\t\t"+this.age;
    }

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

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


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

public class Manage {

    HashMap<String,Student> stu;
    public void add() {
        Scanner input = new Scanner(System.in);
        String tid;
        String tname;
        int tage;
        System.out.println("请输入学号");
        tid = input.next();
        System.out.println("请输入姓名");
        tname = input.next();
        System.out.println("请输入年龄");
        tage = input.nextInt();
        stu.put(tid, new Student(tid, tname, tage));
    }

    public void find(){
        Scanner input = new Scanner(System.in);
        int choice;
        String tid;
        System.out.println("请选择功能:1---查询所有 2---按学号查询  ");
       choice = input.nextInt();
       if(choice==1){
           System.out.println("学号\t\t\t姓名\t\t\t年龄\t\t\t");
           Iterator<String> iterator = stu.keySet().iterator();
           while (iterator.hasNext()){
               System.out.println(stu.get(iterator.next()));
           }
       }
       if (choice==2){
           System.out.println("学号\t\t\t姓名\t\t\t年龄\t\t\t");
           System.out.println("输入要查询的学号");
           tid = input.next();
           if(stu.containsKey(tid)){
               System.out.println(stu.get(tid));
           }
           else {
               System.out.println("学号不存在");
           }
       }
    }

    public void revise(){
        Scanner input = new Scanner(System.in);
        int choice;
        String isornot;
        String tid;
        String tname;
        int tage;
        System.out.println("输入要修改的学生的学号");
        tid = input.next();
        if(stu.containsKey(tid)){
            Student s = stu.get(tid);
            System.out.println("输入修改后的姓名");
            tname = input.next();
            System.out.println("输入修改后的年龄");
            tage  = input.nextInt();
            System.out.println("确定修改吗?确定-Y   否-N");
            isornot = input.next();
            if("Y".equalsIgnoreCase(isornot)){
                s.setAge(tage);
                s.setName(tname);
            }
            else if("N".equalsIgnoreCase(isornot)){
                System.out.println("修改失败");
                return;
            }

        }
        else {
            System.out.println("学号不存在");
        }
    }


    public void deleStu(){
        Scanner input = new Scanner(System.in);
        String isornot;
        String tid;
        int i;
        System.out.println("输入要删除的学生的学号");
        tid = input.next();
        if(stu.containsKey(tid)){
            System.out.println("确定删除吗?确定-Y   否-N");
            isornot = input.next();
            if("Y".equalsIgnoreCase(isornot)){

                stu.remove(tid);
            }
            else if("N".equalsIgnoreCase(isornot)){

                System.out.println("删除失败");
            }

        }
        else {
            System.out.println("学号不存在");
        }


    }
       public void readFile(String address) throws IOException, ClassNotFoundException {
            File file = new File(address);

            Object o = null;
            if(file.exists()){
                ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(address)));
                    while (true) {
                        o = ois.readObject();
                        if (o == null){
                            break;
                        }
                        if (o instanceof Student) {
                            Student t = (Student) o;
                            stu.put(t.getId(), t);
                        }
                    }
                ois.close();
            }
            else {
                file.createNewFile();
            }



    }
    public void saveFile(String address) throws IOException {

        ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(address)));
        Iterator<String> iterator = stu.keySet().iterator();
        while (iterator.hasNext()){
            oos.writeObject(stu.get(iterator.next()));
        }
        oos.writeObject(null);
        oos.close();
    }
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Manage m1 = new Manage();
        m1.stu = new HashMap<String, Student>();
        Scanner input = new Scanner(System.in);
        int choice;
        m1.readFile("d:\\\\stu.txt");
        do {
            System.out.println("************");
            System.out.println("学生管理系统");
            System.out.println("************");
            System.out.println("1--添加学生信息");
            System.out.println("2--查询学生信息");
            System.out.println("3--修改学生信息");
            System.out.println("4--删除学生信息");
            System.out.println("5--退出系统");
            choice = input.nextInt();
            switch (choice){
                case 1: m1.add();break;
                case 2: m1.find();break;
                case 3: m1.revise();break;
                case 4: m1.deleStu();break;
                case 5:break;
            }
        }while(choice!=5);
       m1.saveFile("d:\\\\stu.txt");
//        System.out.println("退出成功,数据已保存至磁盘");
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值