private static final Logger log = LoggerFactory.getLogger(FindEmployeeService.class); public String execute(HttpServletRequest req, HttpServletResponse res) {
String fileName = req.getParameter("fileName");
String path = req.getSession().getServletContext().getRealPath("txt") +"\\"+ fileName +".txt";
List<String> strList = new ArrayList<String>();
try {
strList = getFileContent(path);
System.out.println(strList.size());

} catch (FileNotFoundException e) {
log.info("BatchAddEmployeeService36:异常");
log.error("BatchAddEmployeeService37:异常");
e.printStackTrace();
}
return "right/batchAddSuccess.jsp";
} /**
 * 将文本读取到List中返回
 * */
public List<String> getFileContent(String path) throws FileNotFoundException {
List<String> strList = new ArrayList<String>();
InputStreamReader read = null;
BufferedReader reader = null;
try {
File file = new File(path);
read = new InputStreamReader(new FileInputStream(file), "UTF-8");
reader = new BufferedReader(read);
String line;
while ((line = reader.readLine()) != null) {
strList.add(line);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (read != null) {
try {
read.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return strList;
}
已经写到这了,自己会写还会贴出来问吗?

解决方案 »

  1.   

    建一个employee类,类中对应员工属性,读取文件时第一行由于是title,只启注释作用,所以不作处理,第二行开始,对获取到的每一行分别用split("|"),分割成数组,分别判断并且按顺序赋值,怎么校验就看需求了,校验通过后将数据存放在该实体类中,然后每一行对应一个employee,可以存放在数据库,或者list中
      

  2.   

    4楼正解,把你读取的每行数据解析到一个数组就好了,用split这个分割函数