【#文档大全网# 导语】以下是®文档大全网的小编为您整理的《简单通讯录代码》,欢迎阅读!
package com.soteem.trian.lp.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Enumeration; import java.util.Properties; import java.util.Scanner;
public class TestDemo { public static void print(){ String st=""; do{ System.out.println("********同学录欢迎你******"); System.out.println("请选择操作:1.创建 2.查询 3.修改 4.删除 "); Scanner sc1=new Scanner(System.in); String s=sc1.nextLine(); int ss=Integer.parseInt(s); switch(ss){ case 1: TestDemo.Create(); break; case 2: TestDemo.Read(); break; case 3: TestDemo.Update(); break; case 4: TestDemo.Delete(); break; } System.out.println("继续请按y,否则请按任意键退出!"); Scanner sc=new Scanner(System.in); st=sc.next(); }while(st.equals("y")); System.out.println("你已经退出!"); } public static void Create(){ try { System.out.println("你已经创建成功!");
File f=new File("c:/txl.properties"); //判断f是否存在,不存在就创建一个 if(!f.exists()){ f.createNewFile(); } if(f.canWrite()){ Properties ps=new Properties(); //我需要往文件了写入东西----输出流 FileOutputStream username=new FileOutputStream(f); ps.setProperty("zhang", "aaaa"); ps.setProperty("wang", "bbbb"); ps.setProperty("li", "ccccc"); ps.setProperty("liu", "1545"); ps.setProperty("he", "dddd"); ps.setProperty("wu", "hhhh"); ps.setProperty("ding", "gggg"); ps.setProperty("xie", "jfkdjk"); ps.store(username, "password"); username.close(); }else{ System.out.println("文件不支持写入"); } } catch (IOException e) { e.printStackTrace(); } } public static void Read(){ try { String path="c:/txl.properties"; FileInputStream student=new FileInputStream(path); Properties ps=new Properties(); ps.load(student); student.close(); Enumeration e=ps.propertyNames(); while(e.hasMoreElements()){ String name=(String)e.nextElement(); String phone=ps.getProperty(name); System.out.println(name+"="+phone); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
} public static void Update(){ try { System.out.println("请输入你要修改的姓名:"); Scanner sc=new Scanner(System.in); String name=sc.next(); System.out.println("请输入你要修改的电话号:"); String phone=sc.next(); String path="c:/txl.properties"; FileInputStream fs=new FileInputStream(path); Properties ps=new Properties(); ps.setProperty(name, phone); ps.load(fs); fs.close(); ps.setProperty(name, phone); FileOutputStream fos = new FileOutputStream(path); ps.store(fos, " "); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void Delete(){ File f=new File("c" +":/txl.properties"); if(!f.exists()){ boolean fag=f.delete(); if(fag){ System.out.println("文件已经删除!"); }else{ System.out.println("文件未删除!"); } } }
public static void main(String[] args) {
TestDemo td=new TestDemo(); td.print(); } }
本文来源:https://www.wddqxz.cn/ce0e4e7e8e9951e79b892780.html