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

📄 employeefileio.java

📁 SSD3Pra9的答案
💻 JAVA
字号:
import java.util.*;
import java.io.*;

/**
 * This class provides two file I/O methods for handling employee data.
 *
 * @author  author name
 * @version  1.0.0
 * @see  Employee
 */
public class  EmployeeFileIO {
	/**
	 * 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 ArrayList<Employee> read(String filename)
		throws FileNotFoundException,
			IOException,
			NoSuchElementException,
			NumberFormatException  {


		/* PLACE YOUR CODE HERE */
		
		int i=0;
		ArrayList<Employee> array=new ArrayList<Employee>();
		BufferedReader buffer=new BufferedReader(new FileReader(filename));
		String read;
	   for(read=buffer.readLine();read!=null;read=buffer.readLine()){
	//	String a[]=new String[3];
		StringTokenizer stringtokenizer=new StringTokenizer(read,"_");
	/*	while(stringtokenizer.hasMoreElements()){
		     a[i]=stringtokenizer.nextToken();
			 i++;
		}*/
		int initialId=Integer.parseInt(stringtokenizer.nextToken());
		String initialName= stringtokenizer.nextToken();
		double initialSalary=Double.parseDouble(stringtokenizer.nextToken());
		Employee employee=new Employee(initialId,initialName,initialSalary);
		array.add(employee);
		}

		return array; // 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, ArrayList<Employee> arrayList)
		throws IOException  {
		   
    	 PrintWriter out=new PrintWriter(new FileWriter(filename));
		
		
		if(arrayList.isEmpty())
		return; 
		
		Employee e=arrayList.get(0);
		
		out.print(e.toString());
		out.flush();
		for(int i=1;i<arrayList.size();i++)
		{
		e=arrayList.get(i);	
		out.print("\n"+e.toString());
		out.flush();
		}
	
    		
    	  
    	
    		
     

		/* PLACE YOUR CODE HERE */

	}
}

⌨️ 快捷键说明

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