| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 431 人关注过本帖
标题:新人求教关于 文件的读写
只看楼主 加入收藏
Edwardwang03
Rank: 2
来 自:西安
等 级:论坛游民
帖 子:45
专家分:32
注 册:2012-9-18
结帖率:50%
收藏
已结贴  问题点数:10 回复次数:4 
新人求教关于 文件的读写
就是想练习一下hashtable的使用  最后的代码想要保存在txt中在读出来  但是能写进入  读不出来不知道  为什么
   还有那个hashcode方法的重载真的不太会会不会是哪里有问题?
谢谢大家  很急用的
package Test;

import *;
import java.util.*;

class Student{
    String No, Name, Sex, BirthDate, Member, Address;
    FileOutputStream out;
    ObjectOutputStream objectOut;
    FileInputStream in;
    ObjectInputStream objectIn;

    public Student(String No, String Name,String Sex, String BirthDate,
            String Member, String Address){
        this.No = No;
        this.Name = Name;
        this.Sex = Sex;
        this.BirthDate = BirthDate;
        this.Member = Member;
        this.Address = Address;
    }
    public String getNo(){
        return No;
    }
    public String getName(){
        return Name;
    }
    public String getSex(){
        return Sex;
    }
    public String getBirthDate(){
        return BirthDate;
    }
    public String getMember(){
        return Member;
    }
    public String getAddress(){
        return Address;
    }
    //重写
    public boolean equals(Object o){
        if( (o == null) || !(o instanceof Student)){
            return false;
        } else if(No.equals( ((Student)o).getNo() ) ){
            return true;
        }
        return false;
    }
    public int hashcode(){
        int hash = 5;
        hash = 97 * hash +(No != null? No.hashCode() : 0);
        return hash;
    }
    public String toString(){
        return (No+" "+Name+" "+Sex+" "+BirthDate+" "+Member+" "+Address);
    }
}

public class TestHashTable {

    public static void main(String[] args) throws ClassNotFoundException {
        // TODO Auto-generated method stub
        Map<String, Student> map = new Hashtable<String,Student>();
        Student s1 = new Student("001", "王宇", "男", "1992-12-29", "是", "西安");
        Student s2 = new Student("002", "陈诗涵", "女", "1992-04-02", "是", "锦州");
        Student s3 = new Student("003", "闫飞", "男", "1992-12-29", "是", "周至");
        Student s4 = new Student("004", "李瑶瑶", "女", "1991-12-29", "是", "咸阳");

        map.put(s1.getNo(), s1);
        map.put(s2.getNo(), s2);
        map.put(s3.getNo(), s3);
        map.put(s4.getNo(), s4);

        //遍历hashtable
        for( Iterator it = map.keySet().iterator(); it.hasNext();){
            String key = (String)it.next();
            System.out.println(map.get(key));
        }
        System.out.println("********************************");
        if(map.containsKey("王宇")){
            System.out.println(map.get("wangyu"));
        }

        System.out.println("********************************");
        Student s5 = new Student("005", "程龙", "男", "1991-12-29", "是", "西安");
        map.put("005",  s5);
        //遍历hashtable
        
        for( Iterator it = map.keySet().iterator(); it.hasNext();){
            String key = (String)it.next();
            System.out.println(map.get(key));
        }
        System.out.println("********************************");
        
        map.remove("005");
        for( Iterator it = map.keySet().iterator(); it.hasNext();){
            String key = (String)it.next();
            System.out.println(map.get(key));
        }
        System.out.println("********************************");

        File file = new File("D://testInfo.txt");
        if(!file.exists()){
            try{
                FileOutputStream out = new FileOutputStream(file);
                ObjectOutputStream objectOut = new ObjectOutputStream(out);
                objectOut.writeObject(map);
                objectOut.close();
                out.close();
            } catch(IOException e){
               
            }
        }
        
        System.out.println("********************************");
        System.out.println("***************写入完成*************");
        Map<String, Student> hash = new Hashtable<String,Student>();
        try{
            FileInputStream in = new FileInputStream(file);
            ObjectInputStream objectIn = new ObjectInputStream(in);

            hash = (Hashtable)objectIn.readObject();
            objectIn.close();
            in.close();
        } catch(IOException e){
            
        }
   
        if(hash.containsKey("001")){
            System.out.println("yes");
        } else System.out.println("no");
        
//        for( Iterator it1 = hash.keySet().iterator(); it1.hasNext();){
//            String key = (String)it1.next();
//            System.out.println(hash.get(key));
    //    }
        System.out.println("*************读取完成***************");

    }

}
搜索更多相关主题的帖子: package public import 
2013-11-29 11:12
Edwardwang03
Rank: 2
来 自:西安
等 级:论坛游民
帖 子:45
专家分:32
注 册:2012-9-18
收藏
得分:0 
我就是不明白为什么写进去读不出来  大神求救呀
2013-11-29 11:27
gmh0421
Rank: 4
等 级:业余侠客
威 望:2
帖 子:58
专家分:215
注 册:2013-10-22
收藏
得分:5 
Map<String, Student> hash = new Hashtable<String,Student>();
        try{
            FileInputStream in = new FileInputStream(file);
            ObjectInputStream objectIn = new ObjectInputStream(in);

            hash = (Hashtable)objectIn.readObject();
            objectIn.close();
            in.close();
        } catch(IOException e){
            
        }
这边抛出了ioexception,你没有处理,被你忽略了

本人纯属菜鸟,如有不当,请各位大虾指正
希望能在不断的学习中,提高水平
2013-11-29 16:12
gmh0421
Rank: 4
等 级:业余侠客
威 望:2
帖 子:58
专家分:215
注 册:2013-10-22
收藏
得分:5 
studnet类要有Serializable接口才可以被序列化
import *;
import java.util.*;

class Student implements Serializable{
    String No, Name, Sex, BirthDate, Member, Address;
    FileOutputStream out;
    ObjectOutputStream objectOut;
    FileInputStream in;
    ObjectInputStream objectIn;

    public Student(String No, String Name,String Sex, String BirthDate,
            String Member, String Address){
        this.No = No;
        this.Name = Name;
        this.Sex = Sex;
        this.BirthDate = BirthDate;
        this.Member = Member;
        this.Address = Address;
    }
    public String getNo(){
        return No;
    }
    public String getName(){
        return Name;
    }
    public String getSex(){
        return Sex;
    }
    public String getBirthDate(){
        return BirthDate;
    }
    public String getMember(){
        return Member;
    }
    public String getAddress(){
        return Address;
    }
    //重写
    public boolean equals(Object o){
        if( (o == null) || !(o instanceof Student)){
            return false;
        } else if(No.equals( ((Student)o).getNo() ) ){
            return true;
        }
        return false;
    }
    public int hashcode(){
        int hash = 5;
        hash = 97 * hash +(No != null? No.hashCode() : 0);
        return hash;
    }
    public String toString(){
        return (No+" "+Name+" "+Sex+" "+BirthDate+" "+Member+" "+Address);
    }
}

public class TestHashTable {

    public static void main(String[] args) throws ClassNotFoundException {
        // TODO Auto-generated method stub
        Map<String, Student> map = new Hashtable<String,Student>();
        Student s1 = new Student("001", "王宇", "男", "1992-12-29", "是", "西安");
        Student s2 = new Student("002", "陈诗涵", "女", "1992-04-02", "是", "锦州");
        Student s3 = new Student("003", "闫飞", "男", "1992-12-29", "是", "周至");
        Student s4 = new Student("004", "李瑶瑶", "女", "1991-12-29", "是", "咸阳");

        map.put(s1.getNo(), s1);
        map.put(s2.getNo(), s2);
        map.put(s3.getNo(), s3);
        map.put(s4.getNo(), s4);

        //遍历hashtable
        for( Iterator it = map.keySet().iterator(); it.hasNext();){
            String key = (String)it.next();
            System.out.println(map.get(key));
        }
        System.out.println("********************************");
        if(map.containsKey("001")){
            System.out.println(map.get("001"));
        }

        System.out.println("********************************");
        Student s5 = new Student("005", "程龙", "男", "1991-12-29", "是", "西安");
        map.put("005",  s5);
        //遍历hashtable
        
        for( Iterator it = map.keySet().iterator(); it.hasNext();){
            String key = (String)it.next();
            System.out.println(map.get(key));
        }
        System.out.println("********************************");
        
        map.remove("005");
        for( Iterator it = map.keySet().iterator(); it.hasNext();){
            String key = (String)it.next();
            System.out.println(map.get(key));
        }
        System.out.println("********************************");

        File file = new File("D://testInfo.txt");
        if(!file.exists()){
            try{
                FileOutputStream out = new FileOutputStream(file);
                ObjectOutputStream objectOut = new ObjectOutputStream(out);
                objectOut.writeObject(map);
                objectOut.close();
                out.close();
            } catch(IOException e){
                e.printStackTrace();
            }
        }
        
        System.out.println("********************************");
        System.out.println("***************写入完成*************");
        Map<String, Student> hash = new Hashtable<String,Student>();
        try{
            FileInputStream in = new FileInputStream(file);
            ObjectInputStream objectIn = new ObjectInputStream(in);
        
            hash = (Map)objectIn.readObject();
            System.out.println("********************************");
            System.out.println(hash);
            objectIn.close();
            in.close();
        } catch(IOException e){
            e.printStackTrace();
            
        }
   
        if(hash.containsKey("001")){
            System.out.println("yes");
        } else System.out.println("no");
        
       for( Iterator it1 = hash.keySet().iterator(); it1.hasNext();){
           String key = (String)it1.next();
            System.out.println(hash.get(key));
       }
        System.out.println("*************读取完成***************");

    }

}


[ 本帖最后由 gmh0421 于 2013-11-29 16:44 编辑 ]

本人纯属菜鸟,如有不当,请各位大虾指正
希望能在不断的学习中,提高水平
2013-11-29 16:42
Edwardwang03
Rank: 2
来 自:西安
等 级:论坛游民
帖 子:45
专家分:32
注 册:2012-9-18
收藏
得分:0 
回复 4楼 gmh0421
thanks for ur answers
2013-12-01 21:17
快速回复:新人求教关于 文件的读写
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023078 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved