首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

java作业-通讯录,无数据库,写入数据时出现乱码解决方法

2012-03-15 
java作业-通讯录,无数据库,写入数据时出现乱码[code]import java.util.*import java.io.*import java.la

java作业-通讯录,无数据库,写入数据时出现乱码
[code]import java.util.*;
import java.io.*;
import java.lang.*;

/**
 *
 * @author Administrator
 */
 
public class Person implements Serializable{
  private String name;
  private int age;
  private long phone;
  private long id;
  Person(String name,int age,long phone,long id){
  this.name=name;
  this.age=age;
  this.phone=phone;
  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;
  }
  public long getPhone(){
  return phone;
  }
  public void setName(long phone){
  this.phone=phone;
  }
  public long getId(){
  return id;
  }
  public void setId(long id){
  this.id=id;
  }
public static void main(String[] args) {
  Map map = new HashMap();
  exit:
  try{
  FileOutputStream fos=new FileOutputStream("C:\\Person.txt");
  ObjectOutputStream obs=new ObjectOutputStream(fos);
  obs.writeObject(map);
  for(;;)
  {
  System.out.println("1、保存 2、查询3、删除 4、修改 5、退出");
  Scanner scanner1 = new Scanner(System.in);//扫描器,接受控制台的输入信息
  String str=scanner1.nextLine();//取出控制台的一行信息,也就是你输入的信息
  char[] choose=str.toCharArray();//把取道的字符串变成一个char数组
  FileInputStream fis=new FileInputStream("C:\\Person.txt");
  ObjectInputStream ois=new ObjectInputStream(fis);
  ois.readObject();
  if(choose.length>1)
  {
  System.out.println("只能输入1-5的数字");
  }else {
  switch (choose[0]) {
  case '1':
  FileInputStream fix=new FileInputStream("C:\\Person.txt");
  ObjectInputStream oix=new ObjectInputStream(fix);
  map=(HashMap)oix.readObject();
  System.out.print("请输入学号:");
  Scanner scanner2 = new Scanner(System.in);
  String id = scanner2.nextLine();
  System.out.print("请输入姓名:");
  Scanner scanner3 = new Scanner(System.in);
  String name = scanner3.nextLine();
  System.out.print("请输入电话号码:");
  Scanner scanner4 = new Scanner(System.in);
  String phone = scanner4.nextLine();
  System.out.print("请输入年龄:");
  Scanner scanner5 = new Scanner(System.in);
  String age= scanner5.nextLine();
 
  map.put(id,map);
  System.out.println("保存成功");
  break;
  case '2':
  FileInputStream fib=new FileInputStream("C:\\Person.txt");
  ObjectInputStream oib=new ObjectInputStream(fib);
  oib.readObject();
  if(map.size()==0)
  {
  System.out.println("通讯录为空,请先添加");
  }else {
  Iterator it = map.keySet().iterator();//map.keySet()返回一个对象实例,调用iterator()方法,返回Iterator类型赋给it。实现遍历输出
  while(it.hasNext()){
  String key = (String) it.next();
  String value = (String) map.get(key);
  System.out.println(key+" "+value);
  }
  }
  break;
  case '3':


  FileInputStream fid=new FileInputStream("C:\\Person.txt");
  ObjectInputStream oid=new ObjectInputStream(fid);
  oid.readObject();
  System.out.print("请输入要删除的名字:");
  Scanner scanner6 = new Scanner(System.in);
  String deletName = scanner6.nextLine();
  Iterator it = map.keySet().iterator();
  while(it.hasNext()){
  String key = (String) it.next();
  if(deletName.equals(key))
  {
  map.remove(key);
  System.out.println("删除成功");
  }
  }
   
  break;
  case '4':
  // map.remove()
  FileInputStream fif=new FileInputStream("C:\\Person.txt");
  ObjectInputStream oif=new ObjectInputStream(fif);
  oif.readObject();
  System.out.print("请输入你要修改的通讯人的名字:");  
  Scanner scanner7 = new Scanner(System.in);
  String updateName = scanner7.nextLine();
  Iterator it1 = map.keySet().iterator();
  while(it1.hasNext()){
  String key = (String) it1.next();
  if(updateName.equals(key))
  {
  map.remove(key);
  }
   
  }
  System.out.print("请输入修改后的通讯人的名字:");
  Scanner scanner8 = new Scanner(System.in);
  String name1 = scanner8.nextLine();
  System.out.print("请输入修改后的通讯人的学号:");
  Scanner scanner9 = new Scanner(System.in);
  String id1 = scanner9.nextLine();
  System.out.print("请输入修改后的通讯人的电话号码:");
  Scanner scanner10 = new Scanner(System.in);
  String phone1 = scanner10.nextLine();
  System.out.print("请输入修改后的通讯人的年龄:");
  Scanner scanner11 = new Scanner(System.in);
  String age1 = scanner11.nextLine();
  map.put(id1,map);
  System.out.println("保存成功");
  break;
  case '5':
   
  break exit;
  default:
  break;
  }
  }
  }
}catch(Exception e){
  e.printStackTrace();
  }
}
}
请各位指教,将.txt文件改为其它编码方式也是一样出现乱码

[解决办法]
需要指定编码格式!
UTF-8 OR GBK OR GB2312
[解决办法]
在JAVA中读取文件乱码的解决办法

public static String encodin = "UTF8" 
public static String encodout = "UTF8" 
static void writeOutput(String str) { 
try { 
FileOutputStream fos = new FileOutputStream("test.txt"); 
Writer out = new OutputStreamWriter(fos, encodout); 
out.write(str); 
out.close(); 
} catch (IOException e) { 
e.printStackTrace(); 


static String readInput() { 
StringBuffer buffer = new StringBuffer(); 
try { 
FileInputStream fis = new FileInputStream("test.txt"); 
InputStreamReader isr = new InputStreamReader(fis, encodin); 
Reader in = new BufferedReader(isr); 
int ch; 
while ((ch = in.read()) > -1) { 
buffer.append((char)ch); 

in.close(); 
return buffer.toString(); 
} catch (IOException e) { 
e.printStackTrace(); 
return null; 

}

------解决方案--------------------


Java code
import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;public class TestObjectIO {    /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub                try {                                                FileOutputStream fos = new FileOutputStream("F:\\test.txt",true);            ObjectOutputStream oos = new ObjectOutputStream(fos);            oos.writeObject(new Test(12,"aaa"));            oos.writeObject(new Test(13,"maijunjin"));                        FileInputStream fis = new FileInputStream("F:\\test.txt");            ObjectInputStream ois = new ObjectInputStream(fis);            while(ois.readObject() !=null)    //        System.out.println(ois.available());            System.out.println((Test)ois.readObject());        } catch (Exception e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally{                    }            }}class Test implements Serializable{    private int testID;    private String testName;    public Test(int testID, String testName) {        super();        this.testID = testID;        this.testName = testName;    }    @Override    public String toString() {        // TODO Auto-generated method stub        return "testID:" + testID + " " + "testName:" + testName;    }    public int getTestID() {        return testID;    }    public void setTestID(int testID) {        this.testID = testID;    }    public String getTestName() {        return testName;    }    public void setTestName(String testName) {        this.testName = testName;    }            } 

热点排行