?? fileoperate.java
字號(hào):
/*
* 創(chuàng)建日期 2005-9-21
*
* TODO 要更改此生成的文件的模板,請(qǐng)轉(zhuǎn)至
* 窗口 - 首選項(xiàng) - 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實(shí)例化對(duì)象,主要用來(lái)取得文件名
* @return 返回流水ID號(hào)
* @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號(hào)
* @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號(hào),有可能不足三位
* @return 返回一三位的流水號(hào)
*/
private static String fillIn(String str) {
while (str.length() < 3)
str = "0" + str;
// 如果增加的人數(shù)超過(guò)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);
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -