JAVA遍历路径下所有文件

本示例展示了一个Java应用程序如何通过GUI接收文件路径输入,并递归地获取该路径下所有文件名,随后将这些文件名批量插入到Oracle数据库中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目ORACLE的JDBC驱动

代码如下:

Java代码  收藏代码
  1. package com.gui.file;  
  2.   
  3.   
  4. import java.awt.FlowLayout;  
  5. import java.awt.TextField;  
  6. import java.awt.event.ActionEvent;  
  7. import java.awt.event.ActionListener;  
  8. import java.util.List;  
  9.   
  10. import javax.swing.JButton;  
  11. import javax.swing.JFrame;  
  12.   
  13. public class filenameSave {  
  14.     private static TextField text1;  
  15.     private static JButton send = new JButton("确定");  
  16.       
  17.     public static void main(String[] args) {  
  18.   
  19.         JFrame f = new JFrame();  
  20.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  21.         f.setLayout(new FlowLayout());  
  22.         f.setBounds(5050300200);  
  23.   
  24.         text1 = new TextField(""30);  
  25.         text1.setLocation(3030);  
  26.   
  27.           
  28.         f.add(text1);  
  29.         f.add(send);  
  30.   
  31.         // 在文本框中的事件是:输入文本并回车  
  32.         text1.addActionListener(new ActionListener() {  
  33.             public void actionPerformed(ActionEvent e) {  
  34.                 if (!text1.getText().equals("")) {  
  35.                     text1.setText("");  
  36.                 }  
  37.             }  
  38.         });  
  39.   
  40.         send.addActionListener(new ActionListener(){  
  41.             @Override  
  42.             public void actionPerformed(ActionEvent e) {  
  43.                 String strPath = text1.getText();  
  44.                 fileTraverse filenameList = new fileTraverse();  
  45.                 DataOperate ds = new DataOperate();  
  46.                 List<String> list = filenameList.refreshFileList(strPath);  
  47.                 if(list!=null){  
  48.                     for(String name :list){  
  49.                         String  sql = "insert into fileName (filename) values ('" + name+"')" ;  
  50.                         System.out.println(sql);  
  51.                         ds.saveDate(sql);  
  52.                     }  
  53.                 }  
  54.   
  55.             }});  
  56.           
  57.         f.setVisible(true);  
  58.     }  
  59.   
  60. }  

 

 

Java代码  收藏代码
  1. package com.gui.file;  
  2.   
  3. /* 
  4.  * To change this template, choose Tools | Templates 
  5.  * and open the template in the editor. 
  6.  */  
  7.   
  8. import java.sql.Connection;  
  9. import java.sql.DriverManager;  
  10. import java.sql.SQLException;  
  11. import java.sql.Statement;  
  12. import java.sql.ResultSet;  
  13.   
  14. /** 
  15.  *数据库操作类 
  16.  * @author zhuyuehua 
  17.  */  
  18. public class DataOperate {  
  19.     private String dbDriver = "oracle.jdbc.driver.OracleDriver";  
  20.     private String url = "jdbc:oracle:thin:@127.0.0.1:1521:test";  
  21.     private String username = "test";  
  22.     private String password = "test";  
  23.     private Connection con = null;  
  24.   
  25.   
  26.   
  27.     public void JDBConnection() {  
  28.         try {  
  29.             Class.forName(dbDriver).newInstance(); //加载数据库驱动  
  30.             System.out.println("数据库加载成功");  
  31.         }  
  32.         catch (Exception ex) {  
  33.             System.out.println("数据库加载失败");  
  34.         }  
  35.     }  
  36.   
  37.     //创建数据库连接  
  38.     public boolean creatConnection() {  
  39.         try {  
  40.            Class.forName(dbDriver);  
  41.            con = DriverManager.getConnection(url, username, password);  
  42.             System.out.println("连接数据库成功");  
  43.         }catch(ClassNotFoundException ce)  
  44.         {  
  45.               System.out.println(" 数据库连接失败!\n");  
  46.                ce.printStackTrace();  
  47.          }  
  48.         catch (SQLException e) {  
  49.             e.printStackTrace();  
  50.         }  
  51.     return true;  
  52.     }  
  53.   
  54.      //关闭数据库的操作  
  55.     public void closeConnection() {  
  56.         if (con != null) {  
  57.             try {  
  58.                 con.close();  
  59.             System.out.println("关闭数据库成功");  
  60.             }  
  61.             catch (SQLException e) {  
  62.                 e.printStackTrace();  
  63.             }  
  64.             finally {  
  65.                 con = null;  
  66.             }  
  67.         }  
  68.     }  
  69.   
  70.     //保存数据  
  71.     public boolean saveDate(String sql){  
  72.         if (con == null) {  
  73.             creatConnection();  
  74.         }  
  75.         try {  
  76.             Statement stmt = con.createStatement();  
  77.             stmt.executeUpdate(sql);  
  78.             stmt.close();  
  79.         }catch (SQLException e) {  
  80.             return false;  
  81.         }finally{  
  82.             closeConnection();  
  83.         }  
  84.         return true;  
  85.     }    
  86. }  

 

 

 

Java代码  收藏代码
  1. package com.gui.file;  
  2.   
  3. import java.io.File;  
  4.   
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7.   
  8. public class fileTraverse {  
  9.   
  10.     private List<String> list = new ArrayList<String>();  
  11.   
  12.     public   List<String> refreshFileList(String strPath) {  
  13.           
  14.         File dir = new File(strPath);  
  15.         File[] files = dir.listFiles();  
  16.   
  17.         if (files == null)  
  18.   
  19.             return null;  
  20.   
  21.         for (int i = 0; i < files.length; i++) {  
  22.   
  23.             if (files[i].isDirectory()) {  
  24.   
  25.                 refreshFileList(files[i].getAbsolutePath());  
  26.   
  27.             } else {  
  28.   
  29.                 //String strFileName = files[i].getAbsolutePath().toLowerCase();  
  30.                 String strFileName = files[i].getName();  
  31.   
  32.                 //System.out.println(strFileName);  
  33.                   
  34.                 list.add(strFileName);  
  35.             }  
  36.   
  37.         }  
  38.         return list;  
  39.     }  
  40.   
  41.   
  42. }  

 

http://zhuyuehua.iteye.com/blog/1181814
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值