java從數據庫獲取指定url路徑,然后在本地新建


新手書寫,僅供參考

package com.lk.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Test {
  public static void main(String[] args) throws IOException{
    FileInputStream in = null;
    FileOutputStream out =null;
    try {
      Class.forName("oracle.jdbc.driver.OracleDriver");
      Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@192.168.15.66:1521:orcl","ztq_fj","ztq_fj");
      Statement st=conn.createStatement();
      ResultSet rs=st.executeQuery("select path,replace(path,'d:/ftp/ztq_fj/yjxx_qxfxzh','d:/qxfw') path2 from test");
      int i=0;
      while(rs.next()) {
        String path2=rs.getString("path2");
        System.out.print((++i)+"\t"+path2+"\t");

        File filed = new File(path2);
        System.out.println("...."+filed.getName());

        if(!filed.exists()){
          if(filed.getName().indexOf(".") != -1){
            //文件
            filed.getParentFile().mkdirs();
            filed.createNewFile();
            //讀取/寫入文件
            FileInputStream input = new FileInputStream(filed);
            ByteArrayOutputStream bos =new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length = -1;
            while((length = input.read(buffer)) != -1){
            bos.write(buffer,0,length);
          }
          bos.close();
          input.close();
        }else{
          //文件夾
          filed.mkdirs();
        }
      }else{
        System.out.println("文件不存在!");
      }
    }
    rs.close();
    st.close();

    //關閉數據庫
    conn.close();
    } catch (ClassNotFoundException | SQLException e) {
      // TODO 自動生成的 catch 塊
      e.printStackTrace();
    }
  }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM