📄 fileoperation.java
字号:
/**
*******************************************************************************
* FileOperation.java
*
* (c) Copyright 2008 Hewlett-Packard Development Company, L.P.
*
*<Program Content>
* System Name : Students Management System
*<Summarize>
* The file includes a FileOperation class and the class main function is to
* operate the file.
*<Update Record>
* 2009-4-17 1.00 zhangliy
*******************************************************************************
*/
package com.hp.eds.zhangliyuan.stuMan.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import com.hp.eds.zhangliyuan.stuMan.entity.UserInfo;
import com.hp.eds.zhangliyuan.stuMan.util.StringTool;
/**
* The FileOperation class read data from the file and write the data to the
* file.
*
* @author zhangliy
* @version 1.0
*/
public class FileOperation {
/**
* read all information from CSV file and store them in a map.
*
* @param fileName
* -the name of the CSV file which contents will be fetched
* @return Map:the map that includes all information of the file;if the file
* doesn't exist,return null
*/
public static Map<String, UserInfo> readFile(String fileName) {
if (StringTool.isStringEmpty(fileName)) {
// the fileName is empty
return null;
}
// declare a FileReader object
FileReader fileR = null;
// declare a FileReader object
BufferedReader bufferedR = null;
// create a Map object to receive one property from the file
Map<String, UserInfo> map = new HashMap<String, UserInfo>();
try {
// create a actual FileReader object
fileR = new FileReader(fileName);
// create a actual BufferedReader object
bufferedR = new BufferedReader(fileR);
bufferedR.readLine();
// read a line from the file
String string = bufferedR.readLine();
while (string != null) {
// the line is not null
String[] strings = string.split(",", -1);
// create a user object
UserInfo user = new UserInfo();
user.setStuId(strings[0]);
user.setStuName(strings[1]);
user.setPassword(strings[2]);
user.setHometown(strings[3]);
user.setSex(strings[4]);
user.setTelephone(strings[5]);
user.setEmail(strings[6]);
// put key and value into the map
map.put(strings[1], user);
// read a new line
string = bufferedR.readLine();
}
if (map == null || map.size() == 0) {
// the map is empty
return null;
}
// the map is not empty,return list
return map;
} catch (FileNotFoundException fileExc) {
// display the exception informaiton
System.out.println("Catch FileNotFoundException");
// print the information about the FileNotFoundException
fileExc.printStackTrace();
} catch (IOException ioExc) {
// display the exception informaiton
System.out.println("Catch IOException");
// print the information about the IOException
ioExc.printStackTrace();
} finally {
try {
if (bufferedR != null) {
// bufferedR is not null,close the file
bufferedR.close();
}
} catch (IOException ioExc) {
// display the exception informaiton
System.out.println("Catch IOException");
// print the information about the IOException
ioExc.printStackTrace();
}
}
// there is something wrong with the program,return null
return null;
}
/**
* according the key to fetch the value of the property from the file
*
* @param fileName
* -the file name
*
* @param key
* -the key of an user in the file
*
* @return User the value of the map;if there is no user whose key is equal
* to the key which has been given,return null
*/
public static UserInfo fetchValue(String fileName, String key) {
Map map = readFile(fileName);
if (map == null) {
// the list is empty
return null;
}
Set set = map.keySet();
Iterator iter = set.iterator();
while (iter.hasNext()) {
String keyValue = (String) iter.next();
if (keyValue.equals(key)) {
// he key of current map is equal to the key given,return the
// value of the current
UserInfo user = (UserInfo) map.get(keyValue);
return user;
} else {
// the key of current map doesn't equal to the key given,go on
// searching
continue;
}
}
// there is no property whose key is equal to the key which has been
// given,return null
return null;
}
/**
* write information to a CSV file.
*
* @param map
* -the map includes all information need to write
*
* @param fileName
* -the file where imformation will be writed
*/
public static void writeFile(Map<String, UserInfo> map, String fileName) {
// declare a file
File file = null;
// declare a FileWriter
FileWriter fileWrite = null;
// declare a BufferedWriter
BufferedWriter bufferedWrite = null;
// crate a new object of User
UserInfo user = new UserInfo();
try {
Set set = map.keySet();
// the iterator of the dogs List
Iterator iter = set.iterator();
// create an actual file
file = new File(fileName);
// create a actual FileWriter object
fileWrite = new FileWriter(file);
// create a BufferedWriter object
bufferedWrite = new BufferedWriter(fileWrite);
// write the user's properties into the file
bufferedWrite
.write("stuId,stuName,password,hometown,sex,telephone,email");
// write a new line
bufferedWrite.newLine();
// write information to the file
while (iter.hasNext()) {
// get current Dog object
String key = (String) iter.next();
user = (UserInfo) map.get(key);
// write the user'a information to the file
bufferedWrite.write(user.getStuId() + "," + user.getStuName()
+ "," + user.getPassword() + "," + user.getHometown()
+ "," + user.getSex() + "," + user.getTelephone() + ","
+ user.getEmail());
bufferedWrite.newLine();
}
// flush before closing
bufferedWrite.flush();
} catch (IOException ioe) {
// display the exception informaiton
System.out.println("Catch IOException");
// print the information about the IOException
ioe.printStackTrace();
} finally {
try {
// close file when done
bufferedWrite.close();
} catch (IOException ioe) {
// display the exception informaiton
System.out.println("It can not be closed!");
// print the information about the IOException
ioe.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -