Jingyu_zuo 2016-12-25 02:42 采纳率: 100%
浏览 2592
已采纳

通讯录中TXT文件读取问题

 package in;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;


public class Demo extends JFrame {

    /*
     * 界面设计
     * */
    public Demo(){
        Container c = getContentPane(); //定义一个顶级容器c
        setAlwaysOnTop(true); //窗体置顶
        JPanel jp = new JPanel();   //新建JPanel面板--jp
        JButton button1 = new JButton("新建联系人");
        JButton button2 = new JButton("删除联系人");
        JButton button3 = new JButton("编辑联系人");
        JButton button4 = new JButton("查找联系人");
        JButton button5 = new JButton("显示所有联系人");
        JButton button6 = new JButton("保存联系人到本地");
        JButton button7 = new JButton("读取本地联系人");
        jp.setLayout(new GridLayout(3,4,5,5));//新建网格布局管理器(行数,列数,组件间的水平垂直间距)
        jp.add(button1);
        jp.add(button2);
        jp.add(button3);
        jp.add(button4);
        jp.add(button5);
        jp.add(button6);
        jp.add(button7);
        c.add(jp);//将JPanel面板jp添加到顶级容器c中
        setSize(600,150);
        setTitle("*通 讯 录 管 理 系 统*");
        setVisible(true);
        setResizable(false);//窗体大小由程序员决定,用户不能自由改变大小
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


        /*
         *按键响应
         * 
         * */
        button1.addActionListener(new ActionListener(){//添加功能实现 

            public void actionPerformed(ActionEvent arg0){
                Infro.addFunction();
            }
        });
        button2.addActionListener(new ActionListener(){//删除功能实现
            public void actionPerformed(ActionEvent arg0){
                Infro.deleteFunction();
            }
        });
        button3.addActionListener(new ActionListener(){//修改功能实现
            public void actionPerformed(ActionEvent arg0){
                Infro.reditFunction();
            }
        });
        button4.addActionListener(new ActionListener(){//查询功能实现
            public void actionPerformed(ActionEvent arg0){
                try {
                    Infro.searchFunction();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        button5.addActionListener(new ActionListener(){//显示功能实现
            public void actionPerformed(ActionEvent arg0){
                Infro.showFunction();
            }
        });
        button6.addActionListener(new ActionListener(){//保存功能实现
            public void actionPerformed(ActionEvent arg0){
                try {
                    Infro.writeFunction();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        button7.addActionListener(new ActionListener(){//读取功能实现
            public void actionPerformed(ActionEvent arg0){
                try {
                    Infro.readFunction();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Demo();
        Infro a = new Infro("", "", "", "", "", "");
    }

}

package in;


import java.applet.Applet;
import java.awt.Graphics;
import java.io.BufferedReader;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

import javax.swing.JPanel;



class Infro {
    public String Name;
    public String Sex;
    public String Unit;
    public String Homephone;
    public String Telephone;
    public String E_mail;
    static int index = 0;
    static ArrayList<Infro> list = new ArrayList();
    static int len = list.size();
    //构造函数
    public Infro(String Name,String Sex,String Unit,String Homephone,String Telephone,String E_mail){
        this.Name = Name;
        this.Sex = Sex;
        this.Unit = Unit;
        this.Homephone = Homephone;
        this.Telephone = Telephone;
        this.E_mail = E_mail;
    }
    public String toString(){
        return "姓名:"+Name+"\t性别:"+Sex+"\t单位:"+Unit+"\t住宅电话:"+Homephone
                +"\t移动电话:"+Telephone+"\tE_mail:"+E_mail;
    }

    /*
                            * 添加功能
     * */
    public static void addFunction(){//添加功能
        Infro infro = new Infro("","","","","","");
        System.out.println("请输入添加的数据:");
        Scanner in = new Scanner(System.in);
        System.out.println("输入姓名:");
        infro.Name = in.next();
        System.out.println("输入性别:");
        infro.Sex = in.next();
        System.out.println("输入单位:");
        infro.Unit = in.next();
        System.out.println("输入住宅电话:");
        infro.Homephone = in.next();
        System.
        out.println("输入移动电话:");
        infro.Telephone = in.next();
        System.out.println("输入E_mail:");
        infro.E_mail = in.next();
        list.add(index,infro);
        index++;
        if(list.isEmpty()){
            System.out.println("数据添加失败啦");
        }else{
            System.out.println("数据添加成功啦");
            len++;//list集合长度加一
//          System.out.println(list.get(0).toString());
        }
        System.out.println("是否继续添加?1.是   2.否");
        Scanner e=new Scanner(System.in);
        int e1=e.nextInt();
        if(e1==1){
            Infro.addFunction();
        }else{
            return;
        }
    }

    /*
                            * 删除功能
     * */
     public static void deleteFunction(){
         System.out.println("输入要删除的联系人的姓名");
         Scanner in_2 = new Scanner(System.in);
         String d1 = in_2.nextLine();
         java.util.Iterator<Infro> it = list.iterator();
         while (it.hasNext()){
               Infro infro = it.next();
               if (infro.Name.equals(d1)){
                    it.remove();
                    --index;//一定要加这个,否则当做了删除操作再做添加操作的时候会出现异常(类似于指针,栈)
                    System.out.println("删除完毕"+"此时通讯录记录条数为:" + --len);
               }
            }
         System.out.println("是否继续删除?1.是   2.否");
         Scanner e=new Scanner(System.in);
         int e1=e.nextInt();
         if(e1==1){
            Infro.deleteFunction();
         }else{
            return;
        }
     }
     /*
      *                     修改功能
      * */
     public static void reditFunction(){
         System.out.println("输入要修改的通讯录的姓名");
         Scanner in_r = new Scanner(System.in);
         String r1 = in_r.nextLine();
         for(int a = 0; a < len;a++){
             if(r1.equals(list.get(a).Name)){
                 System.out.println("输入修改后的性别:");
                 String Sex_1 = in_r.next();
                 list.get(a).Sex = Sex_1;
                 System.out.println("输入修改后的单位:");
                 String Unit_1 = in_r.next();
                 list.get(a).Unit = Unit_1;
                 System.out.println("输入修改后的住宅电话:");
                 String Homephone_1 = in_r.next();
                 list.get(a).Homephone = Homephone_1;
                 System.out.println("输入修改后的移动电话:");
                 String Telephone_1 = in_r.next();
                 list.get(a).Telephone = Telephone_1;
                 System.out.println("输入修改后的E_mail:");
                 String E_mail_1 = in_r.next();
                 list.get(a).E_mail = E_mail_1;
                 System.out.println("数据修改完毕");
             }
         }
         System.out.println("是否继续修改?1.是   2.否");
         Scanner e=new Scanner(System.in);
         int e1=e.nextInt();
         if(e1==1){
            Infro.reditFunction();
         }else{
            return;
         }
     }
        /*
                                * 查询功能
        * */

     public static void searchFunction() throws Exception{//查询功能

        System.out.println("请选择查询方式: ");
        System.out.println("1.姓名    2.性别    3.单位   4.住宅电话   5.移动电话   6.E_mail ");
        Scanner in_0=new Scanner(System.in);
        int s0=in_0.nextInt();
        if(s0==1){
        System.out.println("请输入要查询的姓名:");
        Scanner in_1 = new Scanner(System.in);
        String s1=in_1.nextLine();
        for(int a= 0; a<len;a++){//切记,,这里不能用a<=list.size(),否则会数组越界异常
            if(s1.equals(list.get(a).Name)){
                System.out.println(list.get(a).toString());
            }
        }
        }
        else if(s0==2){
            System.out.println("请输入要查询的性别:");
            Scanner in_2 = new Scanner(System.in);
            String s2=in_2.nextLine();
            for(int a= 0; a<len;a++){//切记,,这里不能用a<=list.size(),否则会数组越界异常
                if(s2.equals(list.get(a).Sex)){
                    System.out.println(list.get(a).toString());
                }
            }
        }
        else if(s0==3){
            System.out.println("请输入要查询的单位:");
            Scanner in_3 = new Scanner(System.in);
            String s3=in_3.nextLine();
            for(int a= 0; a<len;a++){//切记,,这里不能用a<=list.size(),否则会数组越界异常
                if(s3.equals(list.get(a).Unit)){
                    System.out.println(list.get(a).toString());
                }
            }
        }
        if(s0==4){
            System.out.println("请输入要查询的住宅电话:");
            Scanner in_4 = new Scanner(System.in);
            String s4=in_4.nextLine();
            for(int a= 0; a<len;a++){//切记,,这里不能用a<=list.size(),否则会数组越界异常
                if(s4.equals(list.get(a).Homephone)){
                    System.out.println(list.get(a).toString());
                }
            }
        }
        else if(s0==5){
            System.out.println("请输入要查询的移动电话:");
            Scanner in_5 = new Scanner(System.in);
            String s5=in_5.nextLine();
            for(int a= 0; a<len;a++){//切记,,这里不能用a<=list.size(),否则会数组越界异常
                if(s5.equals(list.get(a).Telephone)){
                    System.out.println(list.get(a).toString());
                }
            }
        }
        else if(s0==6){
            System.out.println("请输入要查询的E_mail:");
            Scanner in_6 = new Scanner(System.in);
            String s6=in_6.nextLine();
            for(int a= 0; a<len;a++){//切记,,这里不能用a<=list.size(),否则会数组越界异常
                if(s6.equals(list.get(a).E_mail)){
                    System.out.println(list.get(a).toString());
                }
            }
        }
    }

    /*
                            * 显示功能
     * */
    public static void showFunction(){
        for(int i = 0 ;i<len;i++){
            System.out.println(list.get(i).toString());
        }
    }
    /*
     *                      保存功能
     * */
    public static void writeFunction() throws IOException{
        FileWriter writer = new FileWriter("通讯录管理.txt");
        for(int i = 0 ;i<len;i++){
            String []strwriter = new String[len];
            strwriter[i]=list.get(i).toString();
            writer.write(strwriter[i]);
            writer.write("\r\n");
            System.out.println("成功写入一行数据到 通讯录管理.txt 中");
        }
        writer.close();//关闭写入流,释放资源
    }
    /*
     *                      读取功能
     * */
    public static void readFunction() throws IOException{
        FileReader reader = new FileReader("通讯录管理.txt");
        BufferedReader br = new BufferedReader(reader);
        String str;
        while((str = br.readLine()) != null){//每次读取一行文本,判断是否到达文件尾
            System.out.println(str);
        }
        br.close();
    }
}

课程设计中遇到了问题… 在运行时输入,可以查询,编辑等操作。可是保存到本地后退出运行,第二次再运行时读取本地联系人,就没办法执行查询操作了。我改了好久还是不行,请各位哥哥姐姐帮忙看一下,拜托~因为还没有学数据库,只能用文件了。
超过20个分屏显示就不要了,因为貌似java没有分屏显示……其他的功能,按姓名排序也希望你们能给些思路,谢谢谢谢

这是题目:
3. 以本班同学的具体数据为背景,设计一个本班同学通讯录(3人)
通讯录要求存储姓名、性别、单位、住宅电话、移动电话、E-mail地址等内容。系统功能要求如下:
(1)通讯录记录按姓名排序存放,显示时每屏不超过20个记录,超过时分屏显示。
(2)增加某人的通讯录。
(3)修改某人的通讯录。
(4)删除某人的通讯录。
(5)按多种方式查询符合条件的信息。

  • 写回答

4条回答 默认 最新

  • miaoch 2016-12-26 01:54
    关注

    你重新打开新的程序 必须先执行加载功能 把文件里的数据加载到list中
    因为你的查询代码遍历的是list 如果你不执行加载 遍历一个空的list当然就没有数据了。
    并且 我发现 你再次打开一个 重新新增会把原来的文件覆盖掉。所以我觉得必须写一个static静态块,加载文件中内容对list进行初始化

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题