讀取FTP上的E文件並進行解析

  1. Long currentDate=System.currentTimeMillis();
  2. File outFile=new File("D:\\Upload\\"+currentDate+".txt");
  3. Ftp ftp =  new Ftp("ip",port,"name","password");
  4. ftp.cd("/wuys");
  5. ftp.download("/wuys","eFile.txt",outFile);
  6. String node = "T_MONITOR_STATUS";//monitor status狀態監視器
  7. String beginDate= DateUtil.now();
  8. List<List<String>> nodeDatas = EfileReader.readEFile(outFile, node);
  9. for (int i = 0; i < nodeDatas.size(); i++) {
  10.   List<String> lineDatas = nodeDatas.get(i);
  11.   for (int j = 0; j < lineDatas.size(); j++) {
  12.     System.out.print(lineDatas.get(j)+"\t");
  13.   }
  14.   System.out.println();
  15. }
  16. String endDate=DateUtil.now();
  17. System.out.println("開始時間:"+beginDate+"           結束時間:"+endDate);
  18. ftp.close();
  19. ----------------readEFile方法如下
  20. public static List<List<String>> readEFile(File eFilePath, String node){
  21. try {
  22. int startIndex = 0;
  23. int thisIndex = 0;
  24. int endIndex = 0;
  25. boolean flag = false;
  26. BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(eFilePath),"UTF-8"));
  27. String line = reader.readLine();
  28. List<List<String>> listDatas = new ArrayList<List<String>>();
  29. while ((line = reader.readLine()) != null && flag == false) {
  30. thisIndex++;
  31. if(line.startsWith("<"+node)){
  32. startIndex = thisIndex;
  33. }
  34. else if(line.startsWith("</"+node)){
  35. endIndex = thisIndex;
  36. flag = true;
  37. }
  38. else if(startIndex != 0){
  39. String[] split = line.split("\\s+");
  40. List<String> lineDatas = new ArrayList<String>(Arrays.asList(split));
  41. lineDatas.remove(0);//刪除第一個元素,比如:@、#
  42. listDatas.add(lineDatas);
  43. }
  44. }
  45. //System.err.println(node+"節點標籤在第"+startIndex+"-"+endIndex);
  46. reader.close();
  47. return listDatas;
  48. } catch (FileNotFoundException e) {
  49. e.printStackTrace();
  50. } catch (IOException e) {
  51. e.printStackTrace();
  52. }
  53. return null;
  54. }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章