加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-12 15:57:10 所属栏目:百科 来源:网络整理
导读: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.IO

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读