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

📄 employeefileio.java

📁 CMU_ ssd3_第三单元答案 绝对正确
💻 JAVA
字号:
import java.util.*;
import java.io.*;

/**
 * This class provides two file I/O methods for handling employee data.
 *
 * @author  LUOTING
 * @version  1.0.0
 * @see  Employee
 */
public class  EmployeeFileIO {
    private final static String DELIM = "_";
	private static Employee e ;
	/**
	 * Creates an <code>ArrayList</code> of <code>Employee</code> objects
	 * from a file that contains employee data.
	 * <p>
	 * Every line in the specified file should contain three fields: ID, name,
	 * and salary of an employee in the following format: ID_name_salary
	 * </p>
	 *
	 * @param filename  the name of a file containing employee data.
	 * @return  an <code>ArrayList</code> of <code>Employee</code> objects.
	 * @throws FileNotFoundException  if the specified file does not exist.
	 * @throws IOException  if an I/O error occurs.
	 * @throws NoSuchElementException  if the data in the file is incomplete.
	 * @throws NumberFormatException  if the file contains invalid numbers.
	 */
	public static Vector read(String filename)
		throws FileNotFoundException,
			IOException,
			NoSuchElementException,
			NumberFormatException  {


		Vector  list = new Vector();
        BufferedReader reader =
            new BufferedReader(new FileReader(filename));

        String line =  reader.readLine();

		while(line != null){
        StringTokenizer tokenizer = new StringTokenizer(line, DELIM);

        e = new  Employee (Integer.parseInt(tokenizer.nextToken()),
                                  tokenizer.nextToken(),
                                  Double.parseDouble(tokenizer.nextToken()));

		list.add(e);

		line =  reader.readLine();
		}



		return list; // REMOVE; USED SO THIS FILE COMPILES
	}

	/**
	 * Creates a file of employee data from an <code>ArrayList</code> of
	 * <code>Employee</code> objects.
	 * <p>
	 * Every line in the file should contain the ID, name, and salary of an
	 * employee separated by an underscore: ID_name_salary
	 * </p>
	 *
	 * @param filename  the name of the file that will store the employee data.
	 * @param arrayList  an <code>ArrayList</code> of <code>Employee</code>
	 *                   objects.
	 * @throws IOException  if an I/O error occurs.
	 */
	public static void write(String filename, Vector arrayList)
		throws IOException  {

		String out = "";
        PrintWriter fileOut =
            new PrintWriter(new FileWriter(filename));
        
		for(int i = 0; i < arrayList.size() ; i++)
			out += (Employee)arrayList.get(i);         ////将默认调用toString()
        fileOut.println(out);

        fileOut.close();


	}
}

⌨️ 快捷键说明

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