在Java SE中,对IO流与集合的操作在应用中比较重要。接下来,我以一个小型项目的形式,演示IO流、集合等知识点在实践中的运用。
该项目名称为“员工收录系统”,主要是通过输入员工的id、姓名信息,实现简单的增删改查功能。
该项目主要在DOS窗口的控制台或者Eclipse的控制台上进行操作。操作界面如下:
该项目的文件结构如下:
Step 1:
入口类SystemMain的代码为:
package empsystem;
import java.util.Scanner;
/**
* 主界面
* 一个Scanner录入对象
* Employ类
* 文件路径
* 查重SearchID
* @author 李章勇
*
*/
public class SystemMain {
private Scanner sc=new Scanner(System.in);
public SystemMain() {
showWelcome();
}
public void showWelcome(){
System.out.println("----员工收录系统");
System.out.println("1.增加员工功能");
System.out.println("2.查看员工功能");
System.out.println("3.修改员工功能");
System.out.println("4.删除员工功能");
System.out.println("5.退出系统");
String choice=sc.nextLine();
switch(choice){
case "1":
System.out.println("您选择了增加用户功能");
//Add
new Add();
break;
case "2":
System.out.println("您选择了查看用户功能");
//Search
new ShowEmp();
break;
case "3":
System.out.println("您选择了修改用户功能");
//Modify
new Modify();
break;
case "4":
System.out.println("您选择了删除用户功能");
//删除用户Delete
new Delete();
break;
case "5":
System.out.println("您选择了退出系统");
return;
default:
System.out.println("无此功能");
break;
}
showWelcome();
}
public static void main(String[] args) {
new SystemMain();
}
}
Step 2:
写文件路径FilePath接口。
package empsystem;
public interface FilePath {
public static final String PATH_NAME="emp.em";
}
Step 3:
写员工类Employ。
package empsystem;
import java.io.Serializable;
/**
* id,name
* @author 李章勇
*
*/
public class Employ implements Serializable{
private int id;
private String name;
public Employ() {
}
public Employ(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Employ [id=" + id + ", name=" + name + "]\n";
}
}
Step 4:
根据ID查找员工的类SearchID。
package empsystem;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;
/**
* 根据Id查找员工
* @author 李章勇
*
*/
public class SearchID {
private SearchID(){}
public static Employ searchId(int id){
File file=new File(FilePath.PATH_NAME);
if(file.exists()){
try {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
try {
ArrayList<Employ> ems=(ArrayList<Employ>) ois.readObject();
ois.close();
for(int i=0;i<ems.size();i++){
if(id==ems.get(i).getId()){
return ems.get(i);
}
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
return null;
}
return null;
}
}
Step 5:
接下来是增,查,改,删的类,分别是Add类,ShowEmp类, Modify类,Modify类。
(1)
package empsystem;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Scanner;
/**
* 一个输入器对象Scanner
* 文件
* 集合对象ArrayList
* @author 李章勇
*
*/
public class Add {
private Scanner sc=new Scanner(System.in);
private File file=new File(FilePath.PATH_NAME);
private ArrayList<Employ> ems;
public Add() {
if(file.exists()){
try {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
try {
ems=(ArrayList<Employ>) ois.readObject();
ois.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
ems=new ArrayList<Employ>();
}
if(ems!=null){
add();
}else{
System.out.println("系统内部问题,无法操作");
return;
}
}
public boolean checkNum(String idStr){
//检测输入格式
if(idStr==null || idStr.equals("")){
System.out.println("非法输入,重来");
return false;
}
char[] cs=idStr.toCharArray();
for(int i=0;i<cs.length;i++){
if(cs[i]<'0' || cs[i]>'9'){
System.out.println("输入非法,重来");
return false;
}
}
return true;
}
private String idStr;
public int getRightNum(){
idStr=sc.nextLine();
if(!checkNum(idStr)){
getRightNum();
}
int id=Integer.parseInt(idStr);
return id;
}
public void askGoOn(){
System.out.println("请问是否继续录入?Y/N");
String choice=sc.nextLine();
if("Y".equalsIgnoreCase(choice)){
add();
}else if("N".equalsIgnoreCase(choice)){
//保存到文件
saveToFile();
return;
}else{
System.out.println("无此命令,请重新选择!");
askGoOn();
}
}
public void saveToFile(){
try {
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(ems);
oos.close();
//测试打印查看
System.out.println("添加成功");
System.out.println(ems);
return;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void add(){
System.out.println("请输入用户ID:");
//返回整数
int id=getRightNum();
for(int i=0;i<ems.size();i++){
if(id==ems.get(i).getId()){
System.out.println("id已存在,请重新输入");
add();
}
}
System.out.println("请输入员工姓名:");
String name=sc.nextLine();
Employ em=new Employ(id,name);
ems.add(em);
//询问是否继续录入
askGoOn();
}
}
(2)
package empsystem;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Scanner;
/**
* 一个输入器对象Scanner
* 文件
* 集合对象ArrayList
* @author 李章勇
*
*/
public class ShowEmp {
private Scanner sc=new Scanner(System.in);
private File file=new File(FilePath.PATH_NAME);
private ArrayList<Employ> ems;
public ShowEmp() {
if(file.exists()){
try {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
try {
ems=(ArrayList<Employ>) ois.readObject();
ois.close();
if(ems!=null){
show();
}else{
System.out.println("系统内部问题,无法操作");
return;
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
System.out.println("数据文件不存在,无法查看");
return;
}
}
public boolean checkNum(String idStr){
//检测输入格式
if(idStr==null || idStr.equals("")){
System.out.println("非法输入,重来");
return false;
}
char[] cs=idStr.toCharArray();
for(int i=0;i<cs.length;i++){
if(cs[i]<'0' || cs[i]>'9'){
System.out.println("输入非法,重来");
return false;
}
}
return true;
}
private String idStr;
public int getRightNum(){
idStr=sc.nextLine();
if(!checkNum(idStr)){
getRightNum();
}
int id=Integer.parseInt(idStr);
return id;
}
public void show(){
System.out.println("查看全部员工输入Y,查看单个员工输入N");
String choice=sc.nextLine();
if("Y".equalsIgnoreCase(choice)){
System.out.println(ems);
return;
}else if("N".equalsIgnoreCase(choice)){
System.out.println("请输入要查询的员ID:");
int id=getRightNum();
if(SearchID.searchId(id)!=null){
System.out.println("您查找的员工信息为:\n"+SearchID.searchId(id));
return;
}else{
System.out.println("无此用户");
return;
}
}else{
System.out.println("无此命令,请重新选择!");
show();
}
}
}
(3)
package empsystem;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Scanner;
/**
* 一个输入器对象Scanner
* 文件
* 集合对象ArrayList
* @author 李章勇
*
*/
public class Modify {
private Scanner sc=new Scanner(System.in);
private File file=new File(FilePath.PATH_NAME);
private ArrayList<Employ> ems;
public Modify() {
if(file.exists()){
try {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
try {
ems=(ArrayList<Employ>) ois.readObject();
ois.close();
if(ems!=null){
modify();
}else{
System.out.println("系统内部问题,无法操作");
return;
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
System.out.println("数据文件不存在,无法查看");
return;
}
}
public boolean checkNum(String idStr){
//检测输入格式
if(idStr==null || idStr.equals("")){
System.out.println("非法输入,重来");
return false;
}
char[] cs=idStr.toCharArray();
for(int i=0;i<cs.length;i++){
if(cs[i]<'0' || cs[i]>'9'){
System.out.println("输入非法,重来");
return false;
}
}
return true;
}
private String idStr;
public int getRightNum(){
idStr=sc.nextLine();
if(!checkNum(idStr)){
getRightNum();
}
int id=Integer.parseInt(idStr);
return id;
}
public void saveToFile(){
try {
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(ems);
oos.close();
//测试打印查看
System.out.println("修改成功");
System.out.println(ems);
return;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void modify(){
System.out.println("请输入要修改的用户ID:");
int id=getRightNum();
if(SearchID.searchId(id)!=null){
System.out.println("修改前用户的姓名为:"+SearchID.searchId(id).getName());
System.out.println("请输入修改后的姓名:");
String name=sc.nextLine();
for(int i=0;i<ems.size();i++){
if(id==ems.get(i).getId()){
ems.get(i).setName(name);
saveToFile();
}
}
}else{
System.out.println("无此用户");
return;
}
}
}
(4)
package empsystem;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
/**
* 一个输入器对象Scanner
* 文件
* 集合对象ArrayList
* @author 李章勇
*
*/
public class Delete {
private Scanner sc=new Scanner(System.in);
private File file=new File(FilePath.PATH_NAME);
private ArrayList<Employ> ems;
public Delete() {
if(file.exists()){
try {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
try {
ems=(ArrayList<Employ>) ois.readObject();
ois.close();
if(ems!=null){
delete();
}else{
System.out.println("系统内部问题,无法操作");
return;
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
System.out.println("数据文件不存在,无法查看");
return;
}
}
public boolean checkNum(String idStr){
//检测输入格式
if(idStr==null || idStr.equals("")){
System.out.println("非法输入,重来");
return false;
}
char[] cs=idStr.toCharArray();
for(int i=0;i<cs.length;i++){
if(cs[i]<'0' || cs[i]>'9'){
System.out.println("输入非法,重来");
return false;
}
}
return true;
}
private String idStr;
public int getRightNum(){
idStr=sc.nextLine();
if(!checkNum(idStr)){
getRightNum();
}
int id=Integer.parseInt(idStr);
return id;
}
public void saveToFile(){
try {
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(ems);
oos.close();
System.out.println("删除成功");
//测试打印查看
System.out.println(ems);
return;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void delete(){
System.out.println("请输入要删除的员工ID:");
int id=getRightNum();
if(SearchID.searchId(id)!=null){
System.out.println("删除前用户的姓名为:"+SearchID.searchId(id).getName());
Iterator<Employ> it=ems.iterator();
while(it.hasNext()){
Employ em=it.next();
if(id==em.getId()){
it.remove();
saveToFile();
}
}
}else{
System.out.println("无此用户");
return;
}
}
}