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

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

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

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

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

@H_301_9@public @H_301_9@class ReadFIleData {

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

            }
        }

    }

    @H_301_9@private @H_301_9@static @H_301_9@void writeData(String type,String datetime,String fpath) @H_301_9@throws sqlException,IOException {
        @H_301_9@byte[] bys = @H_301_9@null;
        StringBuffer sb = @H_301_9@new StringBuffer();
        bys = toByteArray3(fpath);
        @H_301_9@float[] qpe = @H_301_9@new @H_301_9@float[bys.length / 4];
        System.out.println("共计:" + qpe.length);
        @H_301_9@for (@H_301_9@int i = 0; i < qpe.length; i++) {
            @H_301_9@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 */
    @H_301_9@public @H_301_9@static @H_301_9@float byte2float(@H_301_9@byte[] b,@H_301_9@int index) {
        @H_301_9@int fl;
        fl = b[index + 0];
        fl &= 0xff;
        fl |= ((@H_301_9@long) b[index + 1] << 8);

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

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

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

}
//部分源码来自网络,恕不能找到出处了

猜你在找的Oracle相关文章