java学习笔记——jsp简单方法读取txt文本数据

 该方法不需要数据库和excel插件,程序简单,实现速度快。

 

目标:如下面的txt文档有200多个,每个txt文档都有20条不规则记录,需要将每个文档中的每条数据保存到excel中。

         这些txt是从网站中保存下来的,由于一些网站要验证session和ip,所以不是很好实现网上抓取,就对下载下来的文本文件进行处理,以后再研究网上抓取的过程。

 

文本片段例子:

 

      HIGHLY CITED PAPERS FOR (PEOPLES R CHINA) 

            Sorted by:  Citations Publication Year Journal Title           

 

             881 - 900 (of 6910)   [ 41 | 42 | 43 | 44 | 45  | 46 | 47 | 

                  48 | 49 | 50 ]  

            Page 45 of 346  

 

            881    Citations: 3    

            Title:GEVREY HYPOELLIPTICITY FOR A CLASS OF KINETIC EQUATIONS 

             

            Authors:CHEN H;  LI WX;  XU CJ   

             

            Source:COMMUN PART DIFF EQUAT 36 (4): 693-728 2011 

             

            Addresses:Wuhan Univ, Sch Math & Stat, Wuhan 430072, Peoples R 

China.

            Univ Rouen, CNRS, UMR 6085, St Etienne, France.

 

             

            Field:MATHEMATICS 

 

 

            882    Citations: 3    

            Title:HIGHER AUSLANDER ALGEBRAS ADMITTING TRIVIAL MAXIMAL ORTHOGONAL 

            SUBCATEGORIES 

             

            Authors:HUANG ZY;  ZHANG XJ   

             

            Source:J ALGEBRA 330 (1): 375-387 MAR 15 2011 

             

            Addresses:Nanjing Univ, Dept Math, Nanjing 210093, Jiangsu Prov, 

            Peoples R China.

            Nanjing Univ Informat Sci & Technol, Coll Math & Phys, Nanjing 

            210044, Jiangsu Prov, Peoples R China.

 

             

            Field:MATHEMATICS 

 

 

            883    Citations: 3   

            Title:PREDATOR-PREY SYSTEM WITH STRONG ALLEE EFFECT IN PREY 

 例子中红色字体为需要抽取的字段,遇到的问题有:

  • 每一页都有多条记录,如何区分各个记录;
  • 每个字段的行数都不一样,如何确定其行数。

解决方法:

用两次读取来解决问题

1、第一次读取,用readline()方法读取文本,用startWith()方法判断开头字符,如果为需要抽取的字段,则将其行号记录到该字段的数组中;

2、第二次读取,用for循环控制,每一次循环对于一条记录,判断行号是否和该字段数组中的记录相等,如果相等则读取文本,读到下一个字段的开头结束。

 

程序实现:

 

  1 <%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
  2 <%@ page import="java.util.*,java.io.*,java.util.regex.*" %>
  3 
  4 <html>
  5 <body>
  6 
  7 <font size="4" face="arial" color="black">
  8 <%
  9 
 10 try{
 11 
 12   // 读取的文件数dataNum
 13  for(int dataNum =45; dataNum<=262; dataNum++){
 14 
 15    String txtpath=this.getClass().getResource("/").getPath()+dataNum+".txt";
 16    //out.println(txtpath);
 17    
 18 
 19    
 20    int lineNum=0;  //记录行数
 21    
 22    int [] citationsNum= new int[20];  //记录citations字段所在的行数
 23    int [] titleNum = new int[20];     //记录title字段所在的行数
 24    int [] authorNum= new int[20];     //记录author字段所在的行数
 25    int [] sourceNum  = new  int[20];   //记录source字段所在的行数
 26    int [] addressesNum = new int[20];   //记录address字段所在的行数
 27    int [] fieldNum   = new int[20];    //记录field字段所在的行数
 28 
 29    //记录上述每个字段数组的序号
 30    int   cNum=0;  
 31    int   tNum=0;
 32    int   aNum=0;
 33    int   sNum=0;
 34    int   adNum=0;
 35    int   fNum=0;
 36 
 37    Pattern p = Pattern.compile("Citations:");    //由于Citations不是开头,需要用正则表达式判断
 38    
 39    //以行读取文件内容
 40    BufferedReader br = new BufferedReader(new FileReader(txtpath));
 41 
 42      String record = new String();
 43      String inputText = null;
 44      while ((record = br.readLine()) != null) {  
 45          
 46               lineNum++; //读取一行行号+1
 47               
 48               //记录citations行号
 49               Matcher m = p.matcher(record);
 50               if(m.find()){
 51                 citationsNum [cNum++]= lineNum;
 52               }
 53               
 54               //记录title行号
 55               if((record.replace(" ","")).startsWith("Title:")){
 56                 titleNum [tNum++]= lineNum;
 57               }
 58               
 59               //记录Author行号
 60               if((record.replace(" ","")).startsWith("Authors:")){
 61                 authorNum [aNum++]= lineNum;
 62               }
 63               
 64               //记录Source行号
 65               if((record.replace(" ","")).startsWith("Source:")){
 66                 sourceNum [sNum++]= lineNum;
 67               }
 68               
 69               //记录Address行号
 70               if((record.replace(" ","")).startsWith("Addresses:")){
 71                 addressesNum [adNum++]= lineNum;
 72               }
 73               
 74               //记录Field行号
 75               if((record.replace(" ","")).startsWith("Field:")){
 76                 fieldNum [fNum++]= lineNum;
 77               }
 78  
 79      } //第一次读取结束,每个字段的行号已经记录到数组中
 80      
 81  
 82     
 83     //第二次读取开始,并输出表格
 84     
 85                String CitationsValue = new String(); //记录读取的字段的内容,下同
 86                String TitleValue = new String();
 87                String AuthorValue =new String();
 88                String SourceValue = new String();
 89                String AddressValue = new String();
 90                String fieldValue = new String();
 91                int lineNum2;//第二次读取时的行号
 92                
 93          BufferedReader br2 =null; 
 94 %>
 95             
 96             <table border=1 cellspacing="0">
 97                 
 98 <%
 99                       //因为每一个txt文件有20条记录
100                       for(int i=0; i < 20; i++){  
101                            lineNum2=0;  //开始读取并初始化
102                            TitleValue = "";
103                                        AuthorValue ="";
104                                        SourceValue = "";
105                                        AddressValue ="";
106                                        fieldValue = "";
107                                        CitationsValue = "";
108                                        fieldValue = "";
109                                        
110                              br2 = new BufferedReader(new FileReader(txtpath));
111                              while ((record = br2.readLine()) != null) { 
112                              
113                                   lineNum2++;
114                                
115                                   if( lineNum2>= citationsNum[i] && lineNum2<titleNum[i]){  //根据citationsNum所处的位置读取
116                             
117                              CitationsValue = CitationsValue+record;     //读取的值存到字段中                    
118                             }
119 
120                             if( lineNum2>= titleNum[i] && lineNum2<authorNum[i]){
121                             
122                              TitleValue = TitleValue+record;                             
123                             }
124                             
125                             if( lineNum2>= authorNum[i] && lineNum2<sourceNum[i]){
126                             
127                              AuthorValue = AuthorValue+record;                             
128                             }
129                             
130                             if( lineNum2>= sourceNum[i] && lineNum2<addressesNum[i]){
131                             
132                              SourceValue = SourceValue+record;                             
133                             }
134                             
135                              if( lineNum2 >= addressesNum[i]&& lineNum2<fieldNum[i]) {
136                             
137                              AddressValue = AddressValue+record;                             
138                             }
139                             
140                             if( lineNum2 == fieldNum[i]) {
141                             
142                              fieldValue = fieldValue+record;                           
143                             }
144                   
145                         } //每一次循环都输出一次表格
146  %>
147 
148                             <tr>
149                                 <td><%=CitationsValue%></td>
150                                 <td><%=TitleValue%></td>
151                                 <td><%=AuthorValue%></td>
152                                 <td><%=SourceValue%></td>
153                                 <td><%=AddressValue%></td>
154                                 <td><%=fieldValue%></td>
155                             </tr>
156 
157 <%
158     
159                } //读取结束,表格输出完毕
160                    
161 %>                 
162                         </table>
163 <%
164                
165         br.close();
166         br2.close();
167         
168  } // 所有文件的for循环结束
169  
170 }catch(Exception e){
171     e.printStackTrace();
172 }
173 
174 %>
175 
176 </font></p>
177 
178 </body>
179 </html>

运行结果:

 

 

将上面表格内容复制到excel即可。

 
posted @ 2012-04-21 19:58  todoit  阅读(4203)  评论(0编辑  收藏  举报