⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileoperate.java

📁 学校的学生和工人管理系统 可以完成增删改查的功能
💻 JAVA
字号:
/*
 * 创建日期 2005-9-21
 *
 * TODO 要更改此生成的文件的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
package cn.itcareers.lxh.exercise.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.util.HashMap;

import cn.itcareers.lxh.exercise.exception.AddPersonException;
import cn.itcareers.lxh.exercise.interfaces.Person;

/**
 * @author 白涛
 * 
 * 定义文件操作类
 */
public class FileOperate {
	/**
	 * 
	 * @param filename 要使用的文件名称
	 * @return 从文件中读取所有人员信息
	 */
	public static HashMap readFile(String filename) {
		filename = subString(filename) + ".info";
		File f = new File(filename);
		HashMap hm = null;
		if (!f.exists()) {
			writeFile(new HashMap(), f);
			hm = new HashMap();
		} else {
			ObjectInputStream objin = null;
			try {
				objin = new ObjectInputStream(new FileInputStream(f));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			Object o = null;
			try {
				o = objin.readObject();
			} catch (IOException e1) {
				e1.printStackTrace();
			} catch (ClassNotFoundException e1) {
				e1.printStackTrace();
			}
			hm = (HashMap) o;
			try {
				objin.close();
			} catch (IOException e2) {
				e2.printStackTrace();
			}
		}

		return hm;
	}
	
	/**
	 * 
	 * @param hm 操作完的HashMap
	 * @param f 所使用的文件
	 * @return 是否写入成功
	 */
	
	public static boolean writeFile(HashMap hm, File f) {
		boolean temp = true;
		ObjectOutputStream objout = null;
		try {
			objout = new ObjectOutputStream(new FileOutputStream(f));
		} catch (FileNotFoundException e) {
			temp = false;
		} catch (IOException e) {
			temp = false;
		}
		try {
			objout.writeObject(hm);
		} catch (IOException e1) {
			temp = false;
		}
		try {
			objout.close();
		} catch (IOException e2) {
			temp = false;
		}
		return temp;
	}
	
	/**
	 * 
	 * @param p Person实例化对象,主要用来取得文件名
	 * @return 返回流水ID号
	 * @throws Exception 此方法可能抛出的异常
	 */
	public static String readId(Person p) throws Exception {
		String filename = p.getClass().getName();
		filename = subString(filename) + ".id";
		File f = new File(filename);
		String num = "000";
		if (!f.exists()) {
			writeId(num, f);
		} else {
			BufferedReader in = null;
			try {
				in = new BufferedReader(new InputStreamReader(
						new FileInputStream(f)));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
			try {
				num = in.readLine();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			num = fillIn(increaseId(num));
			writeId(num, f);
			try {
				in.close();
			} catch (IOException e2) {
				e2.printStackTrace();
			}
		}

		return num;
	}

	/**
	 * 
	 * @param id 要写入的ID号
	 * @param f 操作的文件
	 */
	private static void writeId(String id, File f) {
		PrintStream ps = null;
		try {
			ps = new PrintStream(new FileOutputStream(f));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		ps.print(id);
	}

	/**
	 * 
	 * @param str 传入的ID号,有可能不足三位
	 * @return 返回一三位的流水号
	 */
	private static String fillIn(String str) {
		while (str.length() < 3)
			str = "0" + str;

		// 如果增加的人数超过999,则抛出异常!!
		return str;
	}

	/**
	 * 
	 * @param name 类完整名称
	 * @return 类名称
	 */
	private static String subString(String name) {
		if(name==null)
			return null;
		else
			return name.substring(name.lastIndexOf(".") + 1);
	}

	/**
	 * 
	 * @param id 原有的ID
	 * @return 改变之后的ID
	 * @throws Exception 可能抛出的异常
	 */
	private static String increaseId(String id) throws Exception {
		int temp = 0;
		try {
			temp = Integer.parseInt(id);
		} catch (Exception e) {
		}
		temp++;
		if (temp > 999)
			throw new AddPersonException("不能再增加新的人员了!");
		return Integer.toString(temp);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -