New understanding of Java-I / O streams

  Record about some interesting problems encountered at work, before the system is studied Java IO streams, but not used for a long time at work leads to be thrown off balance in a demand in these days, and the search for a while data delayed for some time, well ado, here to talk about the simple demand.

  Demand is such that there is a database BLOB field in a data table stored in a excel file, I need to put this file server specified folder. The idea is this: 1, in the specified folder below to create excel file. 2, jdbc remove files in the database. 3, input and output streams are each converted to a file to import data from the database into the specified file. Here is the code:

  

package helloTest;

import com.fr.base.FRContext;
import com.fr.data.AbstractTableData;
import com.fr.general.data.TableDataException;
import com.fr.third.org.apache.poi.hssf.usermodel.HSSFSheet;
import com.fr.third.org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class ReadFile {
    private static PreparedStatement ps;
    

    /**
     * Read the file stored in the database server to the specified folder 
     * @auther Sunj 20,190,912 
     * @param ID 
     * @return java.io.File
      * / 

    public File the getFile (String ID) { 
        String SQL = "SELECT Filedata the WHERE the above mentioned id = credit.filedata from "? ; 
        FRContext.getLogger () info. ( " SQL Query of ParamTableDataDemo: \ the n-"+ SQL); 
        Connection conn = getConnection (); 
        File File = new new File (" D: \\ root Data.xls the EFS \\ \\ " );
         the try { 

            the OutputStream the outputStream = new new FileOutputStream(file);
            ps = conn.prepareStatement(sql);
            ps.setString(1, id);
            ResultSet rs = ps.executeQuery();
            InputStream in = null;//文件数据输入流
            while (rs.next()) {
                in = rs.getBlob(1).getBinaryStream();
            }
            byte[] b = new byte[1024];
            int len = 0;
            while ( (len = in.read(b)) != -1) {
                outputStream.write(b, 0 , len); // data is written to the specified file 
            } 
            outputStream.close (); 
            in.close (); 
        } the catch (Exception E) { 
            e.printStackTrace (); 
        } 
        return File; 
    } 

    / ** 
     * reads file server. 
     @Auther Sunj 20,190,912 * 
     * @return Boolean
      * / 
    public  Boolean inputDataToDb (String filePath) { 
        filePath = "D: \\ \\ Data.xls the EFS the root \\" ;
         the try { 
            the InputStream IS = new new the FileInputStream (filePath); 
            HSSFWorkbook Excel= new HSSFWorkbook(is);
            for (int numSheet = 0; numSheet < excel.getNumberOfSheets(); numSheet++) {
                HSSFSheet sheet = excel.getSheetAt(numSheet);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 建立数据库连接
     * @auther SunJ 20190912
     * @return java.sql.Connection
     */
    public Connection getConnection() {

        String driverName = "oracle.jdbc.driver.OracleDriver";
        String url = "jdbc:oracle:thin:@10.116.50.123:1521/test";
        String username = "";
        String password = "";
        Connection con;
        try {
            Class.forName(driverName);
            con = DriverManager.getConnection(url, username, password);

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return con;
    }

    public static void main(String[] args) {
        ReadFile ReadFile = new ReadFile();
        File file = ReadFile.getFile("b3448e077f3f4b229f30a86c604e23ab");
        System.out.println(file);
    }
}

 

Guess you like

Origin www.cnblogs.com/sjjava/p/11518768.html