12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- package com.toolkit.file;
- import java.io.File;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- public class ReadFolder{
- /**
- * 读取整个目录的文件名
- *
- * @param path
- * 准备读取的目录
- * @return
- */
- public void readFolder(String path, Connection con) {
- try {
- File a = new File(path);
- String[] file = a.list();
- File temp = null;
- for (int i = 0; i < file.length; i++) {
- if (path.endsWith(File.separator)) {
- temp = new File(path + file[i]);
- } else {
- temp = new File(path + File.separator + file[i]);
- }
- if (temp.isFile()) {
- String sql = "insert into imgUrl values('" + path + temp.getName().toString()
- + "')";
- PreparedStatement ps = con.prepareStatement(sql);
- ps.execute();
- ps.close();
- // WriteToFile(path + temp.getName().toString(),
- // "D:/name.txt");
- // FileInputStream input = new FileInputStream(temp);
- // FileOutputStream output = new FileOutputStream(newPath +
- // "/" +
- // (temp.getName()).toString());
- // byte[] b = new byte[1024 * 5];
- // int len;
- // while ((len = input.read(b)) != -1) {
- // output.write(b, 0, len);
- // }
- // output.flush();
- // output.close();
- // input.close();
- }
- if (temp.isDirectory()) {// 如果是子文件夹
- readFolder(path + "/" + file[i], con);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- Connection connection = null;
- if (connection == null) {
- try {
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- connection = DriverManager
- .getConnection("jdbc:sqlserver://192.168.100.183;databaseName=TrueMapDB;user=sa;password=sinosoft183");
- ReadFolder rf = new ReadFolder();
- rf.readFolder("G:/宁波数据发布/Image/NormalImage/000261/2008", connection);
- connection.close();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
|