oracle 插入大文本,字节转浮点数

前端之家收集整理的这篇文章主要介绍了oracle 插入大文本,字节转浮点数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

key: oracle插入超长文本;oracle 插入大数据
参考这篇 写得很好 不再累述
补充
建表语句

create table TESTCLOB ( id NUMBER(6),name VARCHAR2(20) not null,CLOBATTR CLOB not null ) 
package com.grid.test;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.sql.sqlException;

public class ReadFIleData {

    public static void main(String[] args) {
        // 读取目录下.data文件 类型 时间 rader_201304.data _ 分隔符
        String filePath = "F:/Maobo_Files/微气候细网格城市资源/最终数据/外网data/";
        File filepath = new File(filePath);
        String[] fname = filepath.list();
        for (String s : fname) {
            if (s.endsWith(".data")) {
                String type = s.split("_")[0];
                String datetime = s.split("_")[1];
                String dtime = datetime.substring(0,datetime.lastIndexOf("."));
                String fpath = filePath + s;
                try {
                    writeData(type,dtime,fpath);
                } catch (sqlException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }

    }

    private static void writeData(String type,String datetime,String fpath) throws sqlException,IOException {
        byte[] bys = null;
        StringBuffer sb = new StringBuffer();
        bys = toByteArray3(fpath);
        float[] qpe = new float[bys.length / 4];
        System.out.println("共计:" + qpe.length);
        for (int i = 0; i < qpe.length; i++) {
            float ft = byte2float(bys,i * 4);
            sb.append(ft < 0 ? 0 : ft);
            sb.append(",");
        }
        System.out.println("sb:" + sb.toString());
        // 入库
        TestClobIn.doinsert(sb.toString(),type,datetime);

    }

    /** * 字节转换为浮点 * * @param b * 字节(至少4个字节) * @param index * 开始位置 * @return */
    public static float byte2float(byte[] b,int index) {
        int fl;
        fl = b[index + 0];
        fl &= 0xff;
        fl |= ((long) b[index + 1] << 8);

        fl &= 0xffff;
        fl |= ((long) b[index + 2] << 16);
        fl &= 0xffffff;
        fl |= ((long) b[index + 3] << 24);
        return Float.intBitsToFloat(fl);
    }

    /** * Mapped File way MappedByteBuffer 可以在处理大文件时,提升性能 * * @param filename * @return * @throws IOException */
    public static byte[] toByteArray3(String filename) throws IOException {

        FileChannel fc = null;
        try {
            fc = new RandomAccessFile(filename,"r").getChannel();
            MappedByteBuffer byteBuffer = fc.map(MapMode.READ_ONLY,0,fc.size()).load();
            System.out.println(" is load: " + byteBuffer.isLoaded());
            byte[] result = new byte[(int) fc.size()];
            if (byteBuffer.remaining() > 0) {
                // System.out.println("remain");
                byteBuffer.get(result,byteBuffer.remaining());
            }
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } finally {
            try {
                fc.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
//部分源码来自网络,恕不能找到出处了
原文链接:https://www.f2er.com/oracle/212512.html

猜你在找的Oracle相关文章