java从数据库获取指定url路径,然后在本地新建

本文介绍了一个Java程序,该程序使用Oracle JDBC驱动连接到特定的Oracle数据库,执行SQL查询来获取文件路径,并进行路径替换。然后,程序检查替换后的路径是否存在,如果不存在则创建相应的文件或目录。

新手书写,仅供参考

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();
    }
  }
}

转载于:https://www.cnblogs.com/linsjz/p/9810748.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值