4858e06135e3ccc900e58ae313341bda2d6f3be8.svn-base 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. package com.toolkit.file;
  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.FileWriter;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.InputStreamReader;
  11. import java.io.PrintWriter;
  12. import java.io.StringWriter;
  13. import java.sql.Connection;
  14. import java.sql.DriverManager;
  15. import java.sql.PreparedStatement;
  16. import java.sql.SQLException;
  17. import java.util.Calendar;
  18. import javax.xml.transform.Transformer;
  19. import javax.xml.transform.TransformerException;
  20. import javax.xml.transform.TransformerFactory;
  21. import javax.xml.transform.dom.DOMSource;
  22. import javax.xml.transform.stream.StreamResult;
  23. import org.apache.log4j.Logger;
  24. import org.w3c.dom.Document;
  25. // TODO: Auto-generated Javadoc
  26. /**
  27. * The Class FileOperate.
  28. */
  29. public class FileOperate{
  30. /** The Constant log. */
  31. private final static Logger log = Logger.getLogger(FileOperate.class);
  32. /** The message. */
  33. private String message;
  34. /**
  35. * The Constructor.
  36. *
  37. * @author sun
  38. * @version 2009-7-17-14:04:29
  39. *
  40. * Instantiates a new file operate.
  41. */
  42. public FileOperate() {
  43. }
  44. /**
  45. * 读取文本文件内容
  46. *
  47. * @param filePathAndName
  48. * 带有完整绝对路径的文件名
  49. * @param encoding
  50. * 文本文件打开的编码方式
  51. * @return 返回文本文件的内容
  52. */
  53. @SuppressWarnings("resource")
  54. public String readTxt(String filePathAndName, String encoding) throws IOException {
  55. encoding = encoding.trim();
  56. StringBuffer str = new StringBuffer("");
  57. String st = "";
  58. try {
  59. FileInputStream fs = new FileInputStream(filePathAndName);
  60. InputStreamReader isr;
  61. if (encoding.equals("")) {
  62. isr = new InputStreamReader(fs);
  63. } else {
  64. isr = new InputStreamReader(fs, encoding);
  65. }
  66. BufferedReader br = new BufferedReader(isr);
  67. try {
  68. String data = "";
  69. while ((data = br.readLine()) != null) {
  70. str.append(data + " ");
  71. }
  72. } catch (Exception e) {
  73. str.append(e.toString());
  74. }
  75. st = str.toString();
  76. } catch (IOException es) {
  77. st = "";
  78. }
  79. return st;
  80. }
  81. /**
  82. * 新建文件
  83. *
  84. * @param filePathAndName
  85. * 文本文件完整绝对路径及文件名
  86. * @param fileContent
  87. * 文本文件内容
  88. * @return
  89. */
  90. public void createFile(String filePathAndName, String fileContent) {
  91. try {
  92. String filePath = filePathAndName;
  93. filePath = filePath.toString();
  94. File myFilePath = new File(filePath);
  95. if (!myFilePath.exists()) {
  96. myFilePath.createNewFile();
  97. }
  98. FileWriter resultFile = new FileWriter(myFilePath);
  99. PrintWriter myFile = new PrintWriter(resultFile);
  100. String strContent = fileContent;
  101. myFile.println(strContent);
  102. myFile.close();
  103. resultFile.close();
  104. } catch (Exception e) {
  105. message = "创建文件操作出错";
  106. }
  107. }
  108. /**
  109. * 有编码方式的文件创建
  110. *
  111. * @param filePathAndName
  112. * 文本文件完整绝对路径及文件名
  113. * @param fileContent
  114. * 文本文件内容
  115. * @param encoding
  116. * 编码方式 例如 GBK 或者 UTF-8
  117. * @return
  118. */
  119. public void createFile(String filePathAndName, String fileContent, String encoding) {
  120. try {
  121. String filePath = filePathAndName;
  122. filePath = filePath.toString();
  123. File myFilePath = new File(filePath);
  124. if (!myFilePath.exists()) {
  125. myFilePath.createNewFile();
  126. }
  127. PrintWriter myFile = new PrintWriter(myFilePath, encoding);
  128. String strContent = fileContent;
  129. myFile.println(strContent);
  130. myFile.close();
  131. } catch (Exception e) {
  132. message = "创建文件操作出错";
  133. }
  134. }
  135. /**
  136. * 删除文件
  137. *
  138. * @param filePathAndName
  139. * 文本文件完整绝对路径及文件名
  140. * @return Boolean 成功删除返回true遭遇异常返回false
  141. */
  142. public boolean delFile(String filePathAndName) {
  143. boolean bea = false;
  144. try {
  145. String filePath = filePathAndName;
  146. File myDelFile = new File(filePath);
  147. if (myDelFile.exists()) {
  148. myDelFile.delete();
  149. bea = true;
  150. } else {
  151. bea = false;
  152. message = (filePathAndName + "删除文件操作出错");
  153. }
  154. } catch (Exception e) {
  155. message = e.toString();
  156. }
  157. return bea;
  158. }
  159. /**
  160. * 删除文件夹
  161. *
  162. * @param folderPath
  163. * 文件夹完整绝对路径
  164. * @return
  165. */
  166. public void delFolder(String folderPath) {
  167. try {
  168. delAllFile(folderPath); // 删除完里面所有内容
  169. String filePath = folderPath;
  170. filePath = filePath.toString();
  171. java.io.File myFilePath = new java.io.File(filePath);
  172. myFilePath.delete(); // 删除空文件夹
  173. } catch (Exception e) {
  174. message = ("删除文件夹操作出错");
  175. }
  176. }
  177. /**
  178. * 删除指定文件夹下所有文件
  179. *
  180. * @param path
  181. * 文件夹完整绝对路径
  182. * @return
  183. * @return
  184. */
  185. public boolean delAllFile(String path) {
  186. boolean bea = false;
  187. File file = new File(path);
  188. if (!file.exists()) {
  189. return bea;
  190. }
  191. if (!file.isDirectory()) {
  192. return bea;
  193. }
  194. String[] tempList = file.list();
  195. File temp = null;
  196. for (int i = 0; i < tempList.length; i++) {
  197. if (path.endsWith(File.separator)) {
  198. temp = new File(path + tempList[i]);
  199. } else {
  200. temp = new File(path + File.separator + tempList[i]);
  201. }
  202. if (temp.isFile()) {
  203. temp.delete();
  204. }
  205. if (temp.isDirectory()) {
  206. delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
  207. delFolder(path + "/" + tempList[i]);// 再删除空文件夹
  208. bea = true;
  209. }
  210. }
  211. return bea;
  212. }
  213. /**
  214. * 复制单个文件
  215. *
  216. * @param oldPathFile
  217. * 准备复制的文件源
  218. * @param newPathFile
  219. * 拷贝到新绝对路径带文件名
  220. * @return
  221. */
  222. @SuppressWarnings("resource")
  223. public void copyFile(String oldPathFile, String newPathFile) {
  224. try {
  225. int bytesum = 0;
  226. int byteread = 0;
  227. File oldfile = new File(oldPathFile);
  228. if (oldfile.exists()) { // 文件存在时
  229. InputStream inStream = new FileInputStream(oldPathFile); // 读入原文件
  230. FileOutputStream fs = new FileOutputStream(newPathFile);
  231. byte[] buffer = new byte[1444];
  232. while ((byteread = inStream.read(buffer)) != -1) {
  233. bytesum += byteread; // 字节数 文件大小
  234. log.info(bytesum);
  235. fs.write(buffer, 0, byteread);
  236. }
  237. inStream.close();
  238. }
  239. } catch (Exception e) {
  240. message = ("复制单个文件操作出错");
  241. }
  242. }
  243. /**
  244. * 复制整个文件夹的内容
  245. *
  246. * @param oldPath
  247. * 准备拷贝的目录
  248. * @param newPath
  249. * 指定绝对路径的新目录
  250. * @return
  251. */
  252. public void copyFolder(String oldPath, String newPath) {
  253. try {
  254. new File(newPath).mkdirs(); // 如果文件夹不存在 则建立新文件夹
  255. File a = new File(oldPath);
  256. String[] file = a.list();
  257. File temp = null;
  258. for (int i = 0; i < file.length; i++) {
  259. if (oldPath.endsWith(File.separator)) {
  260. temp = new File(oldPath + file[i]);
  261. } else {
  262. temp = new File(oldPath + File.separator + file[i]);
  263. }
  264. if (temp.isFile()) {
  265. FileInputStream input = new FileInputStream(temp);
  266. FileOutputStream output = new FileOutputStream(newPath + "/"
  267. + (temp.getName()).toString());
  268. byte[] b = new byte[1024 * 5];
  269. int len;
  270. while ((len = input.read(b)) != -1) {
  271. output.write(b, 0, len);
  272. }
  273. output.flush();
  274. output.close();
  275. input.close();
  276. }
  277. if (temp.isDirectory()) {// 如果是子文件夹
  278. copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
  279. }
  280. }
  281. } catch (Exception e) {
  282. message = "复制整个文件夹内容操作出错";
  283. }
  284. }
  285. /**
  286. * 读取整个目录的文件名
  287. *
  288. * @param path
  289. * 准备读取的目录
  290. * @return
  291. */
  292. public static void readFolder(String path, Connection con) {
  293. try {
  294. File a = new File(path);
  295. String[] file = a.list();
  296. File temp = null;
  297. for (int i = 0; i < file.length; i++) {
  298. if (path.endsWith(File.separator)) {
  299. temp = new File(path + file[i]);
  300. } else {
  301. temp = new File(path + File.separator + file[i]);
  302. }
  303. if (temp.isFile()) {
  304. String sql = "insert into imgUrl values('" + path + "/"
  305. + temp.getName().toString() + "')";
  306. PreparedStatement ps = con.prepareStatement(sql);
  307. ps.execute();
  308. ps.close();
  309. // WriteToFile(path + temp.getName().toString(),
  310. // "D:/name.txt");
  311. // FileInputStream input = new FileInputStream(temp);
  312. // FileOutputStream output = new FileOutputStream(newPath +
  313. // "/" +
  314. // (temp.getName()).toString());
  315. // byte[] b = new byte[1024 * 5];
  316. // int len;
  317. // while ((len = input.read(b)) != -1) {
  318. // output.write(b, 0, len);
  319. // }
  320. // output.flush();
  321. // output.close();
  322. // input.close();
  323. }
  324. if (temp.isDirectory()) {// 如果是子文件夹
  325. readFolder(path + "/" + file[i], con);
  326. }
  327. }
  328. } catch (Exception e) {
  329. e.printStackTrace();
  330. }
  331. }
  332. /**
  333. * 移动文件
  334. *
  335. * @param oldPath
  336. * @param newPath
  337. * @return
  338. */
  339. public void moveFile(String oldPath, String newPath) {
  340. copyFile(oldPath, newPath);
  341. delFile(oldPath);
  342. }
  343. /**
  344. * 移动目录
  345. *
  346. * @param oldPath
  347. * @param newPath
  348. * @return
  349. */
  350. public void moveFolder(String oldPath, String newPath) {
  351. copyFolder(oldPath, newPath);
  352. delFolder(oldPath);
  353. }
  354. /**
  355. * 把从DOM中获得的DOCUMENT对象存放到制定的XML文件路径中
  356. *
  357. * @param doc
  358. * @param filePath
  359. * @return
  360. * @throws TransformerException
  361. */
  362. public boolean saveDocumentFromDomToXMLFile(Document doc, String filePath)
  363. throws TransformerException {
  364. /**
  365. * dom对象保存到制定的XML文件 filePath 中
  366. */
  367. StringWriter sw = new StringWriter();
  368. TransformerFactory transformerfactory = TransformerFactory.newInstance();
  369. Transformer transformer = transformerfactory.newTransformer();
  370. StreamResult result = new StreamResult(sw);
  371. DOMSource source = new DOMSource(doc.getDocumentElement());
  372. transformer.transform(source, result);
  373. String s = sw.toString();
  374. log.info(s);
  375. FileOutputStream fo = null;
  376. try {
  377. fo = new FileOutputStream(filePath);
  378. fo.write(s.getBytes("utf-8"));
  379. fo.close();// 关闭
  380. } catch (IOException e) {
  381. e.printStackTrace();
  382. }
  383. return true;
  384. }
  385. /**
  386. * 有编码方式的文件创建
  387. *
  388. * @param filePathAndName
  389. * 文本文件完整绝对路径及文件名
  390. * @param fileContent
  391. * 文本文件内容
  392. * @param encoding
  393. * 编码方式 例如 GBK 或者 UTF-8
  394. * @return
  395. */
  396. public boolean createFile(String fileName, String folderPath, String fileContent,
  397. String encoding, boolean bbakup) {
  398. try {
  399. /**
  400. * 第一步:先判断文件夹是否存在,不存在逐级创建文件夹
  401. */
  402. File myFilePath = new File(folderPath);
  403. log.info("folderPath = " + folderPath);
  404. if (!myFilePath.exists()) {
  405. createFolder(folderPath);
  406. }
  407. /**
  408. * 第二步:把文件写入到指定的文件中
  409. */
  410. String filePath = folderPath + "/" + fileName;
  411. filePath = filePath.toString();
  412. myFilePath = new File(filePath);
  413. if (!myFilePath.exists()) {
  414. myFilePath.createNewFile();
  415. } else {
  416. if (bbakup) {
  417. Calendar calendar = Calendar.getInstance();
  418. java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
  419. myFilePath.renameTo(new File(filePath.replace(".",
  420. df.format(calendar.getTime()) + ".")));
  421. }
  422. }
  423. PrintWriter myFile = new PrintWriter(myFilePath, encoding);
  424. String strContent = fileContent;
  425. myFile.println(strContent);
  426. myFile.close();
  427. return true;
  428. }
  429. catch (Exception e) {
  430. message = "创建文件操作出错";
  431. }
  432. return false;
  433. }
  434. /**
  435. * 新建目录
  436. *
  437. * @param folderPath
  438. * 目录
  439. * @return 返回目录创建后的路径
  440. */
  441. public static boolean createFolder(String folderPath) {
  442. try {
  443. log.info("folderPath = " + folderPath);
  444. File myFilePath = new File(folderPath);
  445. if (!myFilePath.exists()) {
  446. String[] s = folderPath.split("/");
  447. String tempForder = s[0];
  448. for (int i = 1; i < s.length; i++) {
  449. tempForder = tempForder + "/" + s[i];
  450. File forderFile = new File(tempForder);
  451. // log.info("folderPath = "+tempForder);
  452. if (!forderFile.exists()) {
  453. forderFile.mkdir();
  454. }
  455. }
  456. }
  457. } catch (Exception e) {
  458. log.error(e.toString());
  459. }
  460. return true;
  461. }
  462. /**
  463. * @return
  464. */
  465. public String getMessage() {
  466. return this.message;
  467. }
  468. /**
  469. * Write to file.
  470. *
  471. * @param str
  472. * 要写入的字符串
  473. * @param path
  474. * 要写入的文件路径和文件名字
  475. *
  476. * @return true, if successful
  477. * @author sun
  478. *
  479. * @version 2009-7-17-14:04:29
  480. *
  481. * Write to file.
  482. */
  483. public static boolean WriteToFile(String str, String path) {
  484. boolean result = false;
  485. try {
  486. FileWriter fw = new FileWriter(new File(path), true);
  487. BufferedWriter bw = new BufferedWriter(fw);
  488. bw.write(str, 0, str.length());
  489. bw.newLine();
  490. bw.close();
  491. result = true;
  492. } catch (IOException e) {
  493. log.info(e.getMessage(), e);
  494. }
  495. return result;
  496. }
  497. public static void main(String[] args) {
  498. // String fs = "D:/ws/bjglweb/WebContent/page/system/sun/ff/dd";
  499. // createFolder(fs);
  500. Connection connection = null;
  501. if (connection == null) {
  502. try {
  503. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  504. connection = DriverManager
  505. .getConnection("jdbc:sqlserver://192.168.100.183;databaseName=TrueMapDB;user=sa;password=sinosoft183");
  506. readFolder("\\\\192.168.100.178/g$/宁波数据发布/Image/NormalImage/000261/2008",
  507. connection);
  508. connection.close();
  509. } catch (ClassNotFoundException e) {
  510. log.error(e);
  511. } catch (SQLException e) {
  512. log.error(e);
  513. }
  514. }
  515. }
  516. }