Example usage for java.io RandomAccessFile readFloat

List of usage examples for java.io RandomAccessFile readFloat

Introduction

In this page you can find the example usage for java.io RandomAccessFile readFloat.

Prototype

public final float readFloat() throws IOException 

Source Link

Document

Reads a float from this file.

Usage

From source file:Main.java

public static void main(String[] args) {
    try {/*from  ww  w  .  j  a  va  2s  .c om*/
        float f = 1234.5678f;

        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        raf.writeFloat(f);

        raf.seek(0);

        System.out.println(raf.readFloat());

        raf.seek(0);

        raf.writeFloat(123.4567f);

        raf.seek(0);

        System.out.println(raf.readFloat());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {//from w w w.j  a v a 2  s . c  o m
        float f = 1234.56f;

        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        // write something in the file
        raf.writeFloat(987.654f);

        // set the file pointer at 0 position
        raf.seek(0);

        // read float
        System.out.println(raf.readFloat());

        // set the file pointer at 0 position
        raf.seek(0);

        // write a float
        raf.writeFloat(f);

        // set the file pointer at 0 position
        raf.seek(0);

        // read float
        System.out.println(raf.readFloat());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:au.org.ala.layers.dao.ObjectDAOImpl.java

private HashMap<String, Object> getGridIndexEntry(String path, String objectId) {
    HashMap<String, Object> map = new HashMap<String, Object>();
    RandomAccessFile raf = null;
    try {//from w ww  .  j  ava2s  .  co m
        raf = new RandomAccessFile(path + ".wkt.index.dat", "r");

        int s2 = Integer.parseInt(objectId);

        // it is all in order, seek to the record
        int recordSize = 4 * 7; // 2 int + 5 float
        int start = raf.readInt();
        raf.seek(recordSize * (s2 - start));

        map.put("gn", raf.readInt());
        map.put("charoffset", raf.readInt());
        map.put("minx", raf.readFloat());
        map.put("miny", raf.readFloat());
        map.put("maxx", raf.readFloat());
        map.put("maxy", raf.readFloat());
        map.put("area", raf.readFloat());
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        try {
            if (raf != null) {
                raf.close();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    return map;
}

From source file:au.org.ala.layers.dao.ObjectDAOImpl.java

@Override
public List<Objects> getObjectsById(String id, int start, int pageSize) {
    logger.info("Getting object info for fid = " + id);
    String limit_offset = " limit " + (pageSize < 0 ? "all" : pageSize) + " offset " + start;
    String sql = "select o.pid as pid, o.id as id, o.name as name, o.desc as description, "
            + "o.fid as fid, f.name as fieldname, o.bbox, o.area_km, "
            + "ST_AsText(ST_Centroid(o.the_geom)) as centroid,"
            + "GeometryType(o.the_geom) as featureType from objects o, fields f "
            + "where o.fid = ? and o.fid = f.id order by o.pid " + limit_offset;
    List<Objects> objects = jdbcTemplate.query(sql,
            ParameterizedBeanPropertyRowMapper.newInstance(Objects.class), id);

    updateObjectWms(objects);//from www  .j av a2 s .  c  o  m

    // get grid classes
    if (objects == null || objects.isEmpty()) {
        objects = new ArrayList<Objects>();
        IntersectionFile f = layerIntersectDao.getConfig().getIntersectionFile(id);
        if (f != null && f.getClasses() != null) {
            //shape position
            int pos = 0;

            for (Entry<Integer, GridClass> c : f.getClasses().entrySet()) {
                File file = new File(f.getFilePath() + File.separator + c.getKey() + ".wkt.index.dat");
                if (f.getType().equals("a") || !file.exists()) { // class pid
                    if (pageSize == -1 || (pos >= start && pos - start < pageSize)) {
                        Objects o = new Objects();
                        o.setPid(f.getLayerPid() + ":" + c.getKey());
                        o.setId(f.getLayerPid() + ":" + c.getKey());
                        o.setName(c.getValue().getName());
                        o.setFid(f.getFieldId());
                        o.setFieldname(f.getFieldName());
                        o.setBbox(c.getValue().getBbox());
                        o.setArea_km(c.getValue().getArea_km());
                        o.setWmsurl(getGridClassWms(f.getLayerName(), c.getValue()));
                        objects.add(o);
                    }
                    pos++;

                    if (pageSize != -1 && pos >= start + pageSize) {
                        break;
                    }
                } else { // polygon pid
                    RandomAccessFile raf = null;
                    try {
                        raf = new RandomAccessFile(file, "r");
                        long itemSize = (4 + 4 + 4 * 4 + 4);
                        long len = raf.length() / itemSize; // group

                        if (pageSize != -1 && pos + len < start) {
                            pos += len;
                        } else {
                            // number,
                            // character
                            // offset,
                            // minx,
                            // miny,
                            // maxx,
                            // maxy,
                            // area
                            // sq
                            // km
                            int i = 0;
                            if (pageSize != -1 && pos < start) {
                                //the first object requested is in this file, seek to the start
                                i = start - pos;
                                pos += i;
                                raf.seek(i * itemSize);
                            }
                            for (; i < len; i++) {
                                int n = raf.readInt();
                                /* int charoffset = */
                                raf.readInt();
                                float minx = raf.readFloat();
                                float miny = raf.readFloat();
                                float maxx = raf.readFloat();
                                float maxy = raf.readFloat();
                                float area = raf.readFloat();

                                if (pageSize == -1 || (pos >= start && pos - start < pageSize)) {
                                    Objects o = new Objects();
                                    o.setPid(f.getLayerPid() + ":" + c.getKey() + ":" + n);
                                    o.setId(f.getLayerPid() + ":" + c.getKey() + ":" + n);
                                    o.setName(c.getValue().getName());
                                    o.setFid(f.getFieldId());
                                    o.setFieldname(f.getFieldName());

                                    o.setBbox("POLYGON((" + minx + " " + miny + "," + minx + " " + maxy + ","
                                            + +maxx + " " + maxy + "," + +maxx + " " + miny + "," + +minx + " "
                                            + miny + "))");
                                    o.setArea_km(1.0 * area);

                                    o.setWmsurl(getGridPolygonWms(f.getLayerName(), n));

                                    objects.add(o);
                                }

                                pos++;

                                if (pageSize != -1 && pos >= start + pageSize) {
                                    break;
                                }
                            }
                        }
                    } catch (Exception e) {
                        logger.error(e.getMessage(), e);
                    } finally {
                        if (raf != null) {
                            try {
                                raf.close();
                            } catch (Exception e) {
                                logger.error(e.getMessage(), e);
                            }
                        }
                    }

                    if (pageSize != -1 && pos >= start + pageSize) {
                        break;
                    }
                }
            }
        }
    }
    return objects;
}