hibernate批量儲存文件夾txt文件內容

          此方法并不好,因為之前自己已經做好了jdbc調用sql的存儲。
          對于數據量大的建議不要采用這種方法。
          自我感覺代碼寫得一點也不好,如果能將那個數組部分優化就好了。

          CREATE DATABASE `lucene` /*!40100 DEFAULT CHARACTER SET utf8 */;
          USE `lucene`;
          CREATE TABLE `testsearch` (
            `id` 
          int(11NOT NULL auto_increment,
            `time` date 
          default NULL,
            `title` 
          varchar(50default NULL,
            `content` 
          text,
            `click` 
          int(11default NULL,
            
          PRIMARY KEY  (`id`)
          ) ENGINE
          =InnoDB DEFAULT CHARSET=utf8;


          代碼:

           1package com.duduli.li.lucene;
           2
           3import java.io.BufferedReader;
           4import java.io.File;
           5import java.io.FileReader;
           6import java.io.IOException;
           7import java.util.Date;
           8
           9import org.hibernate.Session;
          10import org.hibernate.Transaction;
          11
          12import com.duduli.li.lucene.HibernateSessionFactory;
          13
          14public class Client {
          15    public static String s;
          16    static String fileName;
          17
          18//    得到文件夾中文件名
          19    public static String getFileName(File file) {
          20        String filename = "";
          21        if (file != null{
          22            filename = file.getName();
          23            int i = filename.lastIndexOf('.');
          24            if (i > 0 && i < filename.length() - 1{
          25                return filename.substring(0, i);
          26            }

          27        }

          28        return filename;
          29    }

          30
          31//    讀取文件夾中文件,如果是文件則讀取,如果是文件夾則遞歸讀取
          32    public static void scan(String f) throws IOException {
          33        File file = new File(f);
          34
          35        if (file.isDirectory()) {
          36            String[] fileList = file.list();
          37            int j = fileList.length;
          38//    
          39            Testsearch [] ts;
          40            ts = new Testsearch[j];
          41            TestsearchDAO [] dao;
          42            dao = new TestsearchDAO[j];
          43            Session [] session;
          44            session = new Session[j];
          45            Transaction [] tran;
          46            tran = new Transaction[j];
          47
          48            for (int i = 0; i < j; i++{
          49                
          50                ts[i] = new Testsearch();
          51                dao[i] = new TestsearchDAO();
          52                session[i] = HibernateSessionFactory.getSession();
          53                tran[i] = session[i].beginTransaction();
          54                ts[i].setClick(0);
          55                ts[i].setTime(new Date());
          56                
          57                File fileReader = new File(f + "\\" + fileList[i]);
          58                fileName = fileReader.getAbsolutePath();
          59                if (!fileReader.isDirectory()) {
          60                    @SuppressWarnings("unused")
          61                    String getfilename = getFileName(fileReader);
          62                    ts[i].setTitle(getfilename);
          63                    }
           else if (fileReader.isDirectory()) {
          64                        scan(file + "\\" + fileList[i]);
          65                    }

          66                    BufferedReader br = new BufferedReader(new FileReader(fileName));
          67                    StringBuffer b = new StringBuffer();
          68                    String str;
          69                    while ((str = br.readLine()) != null{
          70                        b.append(str);
          71                    }

          72                    s = b.toString();
          73                    System.out.println(s);
          74
          75                    ts[i].setContent(s);
          76                    
          77                    dao[i].save(ts[i]);
          78                    tran[i].commit();
          79                }

          80            }

          81        }

          82    
          83    @SuppressWarnings("static-access")
          84    public static void main(String[] args) throws IOException {
          85        Client c = new Client();
          86        c.scan("C:\\Documents and Settings\\Administrator\\桌面\\T");    
          87    }

          88}

          89

          還有就是pojo:
           1package com.duduli.li.lucene;
           2
           3import java.util.Date;
           4
           5public class Testsearch {
           6    public int id;
           7    public Date time;
           8    public String content;
           9    public String title;
          10       //set和get方法
          11}

          12

          posted on 2009-03-13 13:28 duduli 閱讀(1191) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2009年3月>
          22232425262728
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          導航

          統計

          公告

          welcome to my place.

          常用鏈接

          留言簿(5)

          我參與的團隊

          隨筆分類

          隨筆檔案

          新聞分類

          石頭JAVA擺地攤兒

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          @duduli
          主站蜘蛛池模板: 工布江达县| 梅州市| 那坡县| 法库县| 湘阴县| 云浮市| 毕节市| 贡嘎县| 荥经县| 南岸区| 长沙市| 肥东县| 嘉义县| 新龙县| 漳平市| 耿马| 济源市| 原平市| 钦州市| 柯坪县| 宁波市| 黄龙县| 正蓝旗| 鄱阳县| 广元市| 博乐市| 磴口县| 宣城市| 赤壁市| 阳山县| 民丰县| 曲周县| 娱乐| 灵石县| 平定县| 策勒县| 靖边县| 枣阳市| 天长市| 曲麻莱县| 禄丰县|