hibernate批量儲(chǔ)存文件夾txt文件內(nèi)容
此方法并不好,因?yàn)橹白约阂呀?jīng)做好了jdbc調(diào)用sql的存儲(chǔ)。
對于數(shù)據(jù)量大的建議不要采用這種方法。
自我感覺代碼寫得一點(diǎn)也不好,如果能將那個(gè)數(shù)組部分優(yōu)化就好了。










代碼:
1
package com.duduli.li.lucene;
2
3
import java.io.BufferedReader;
4
import java.io.File;
5
import java.io.FileReader;
6
import java.io.IOException;
7
import java.util.Date;
8
9
import org.hibernate.Session;
10
import org.hibernate.Transaction;
11
12
import com.duduli.li.lucene.HibernateSessionFactory;
13
14
public 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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

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

2

3

4

5

6

7

8

9

10

11

12
