List of usage examples for java.io RandomAccessFile readByte
public final byte readByte() throws IOException
From source file:Main.java
public static void main(String[] args) { try {/*from ww w.j a v a 2 s . c om*/ byte[] b1 = { 15, 8 }; RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); raf.writeByte(b1[0]); raf.seek(0); System.out.println(raf.readByte()); raf.seek(0); raf.writeByte(b1[1]); raf.seek(0); System.out.println(raf.readByte()); raf.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:ReverseFile.java
public static void main(String args[]) throws Exception { RandomAccessFile raf = new RandomAccessFile(args[0], "r"); long position = raf.length(); while (position > 0) { position -= 1;//from w ww .j a va 2s .co m raf.seek(position); byte b = raf.readByte(); System.out.print((char) b); } }
From source file:Main.java
public static void main(String[] args) { try {// w ww. java 2s. co m byte[] b = { 1 }; RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); raf.write(b); // set the file pointer at 0 position raf.seek(0); System.out.println(raf.readByte()); 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 a2 s .c o m int b1 = 15; int b2 = 20; RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); raf.write(b1); // set the file pointer at 0 position raf.seek(0); System.out.println(raf.readByte()); raf.write(b2); raf.seek(1); System.out.println(raf.readByte()); raf.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:MainClass.java
public static void main(String args[]) { try {/*from ww w . j av a 2 s .c om*/ RandomAccessFile raf = new RandomAccessFile(args[0], "r"); long position = raf.length(); while (position > 0) { position -= 1; raf.seek(position); byte b = raf.readByte(); System.out.print((char) b); } } catch (Exception e) { e.printStackTrace(); } }
From source file:Tail.java
public static void main(String args[]) throws Exception { RandomAccessFile raf = new RandomAccessFile(args[0], "r"); long count = 10; long position = raf.length(); position -= count;/*from w ww . j av a2s. co m*/ if (position < 0) position = 0; raf.seek(position); while (true) { try { byte b = raf.readByte(); System.out.print((char) b); } catch (EOFException eofe) { break; } } }
From source file:Main.java
public static void main(String[] args) { try {// ww w . j a va2 s.c om byte[] b = { 1, 2, 3, 4, 5, 6 }; RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); // write 2 bytes in the file raf.write(b, 2, 2); // set the file pointer at 0 position raf.seek(0); // print the two bytes we wrote System.out.println(raf.readByte()); System.out.println(raf.readByte()); raf.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) { try {//from ww w .j a v a2s . co m RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); // write something in the file raf.writeUTF("Hello World from java2s.com"); // set the file pointer at 0 position raf.seek(0); // read byte System.out.println(raf.readByte()); // set the file pointer at 0 position raf.seek(0); // write 0 at the start raf.write(0); // read byte System.out.println(raf.readByte()); raf.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:au.org.ala.layers.grid.GridCacheBuilder.java
static public float getNextValue(RandomAccessFile raf, Grid g) { float f = Float.NaN; try {//from w w w.j av a2 s. co m int size; byte[] b; if (g.datatype.charAt(0) == 'B') {//"BYTE")) { f = raf.readByte(); } else if (g.datatype.charAt(0) == 'U') {//equalsIgnoreCase("UBYTE")) { f = raf.readByte(); if (f < 0) { f += 256; } } else if (g.datatype.charAt(0) == 'S') {//equalsIgnoreCase("SHORT")) { size = 2; b = new byte[size]; raf.read(b); if (g.byteorderLSB) { f = (short) (((0xFF & b[1]) << 8) | (b[0] & 0xFF)); } else { f = (short) (((0xFF & b[0]) << 8) | (b[1] & 0xFF)); } } else if (g.datatype.charAt(0) == 'I') {//equalsIgnoreCase("INT")) { size = 4; b = new byte[size]; raf.read(b); if (g.byteorderLSB) { f = ((0xFF & b[3]) << 24) | ((0xFF & b[2]) << 16) + ((0xFF & b[1]) << 8) + (b[0] & 0xFF); } else { f = ((0xFF & b[0]) << 24) | ((0xFF & b[1]) << 16) + ((0xFF & b[2]) << 8) + ((0xFF & b[3]) & 0xFF); } } else if (g.datatype.charAt(0) == 'L') {//equalsIgnoreCase("LONG")) { size = 8; b = new byte[size]; raf.read(b); if (g.byteorderLSB) { f = ((long) (0xFF & b[7]) << 56) + ((long) (0xFF & b[6]) << 48) + ((long) (0xFF & b[5]) << 40) + ((long) (0xFF & b[4]) << 32) + ((long) (0xFF & b[3]) << 24) + ((long) (0xFF & b[2]) << 16) + ((long) (0xFF & b[1]) << 8) + (0xFF & b[0]); } else { f = ((long) (0xFF & b[0]) << 56) + ((long) (0xFF & b[1]) << 48) + ((long) (0xFF & b[2]) << 40) + ((long) (0xFF & b[3]) << 32) + ((long) (0xFF & b[4]) << 24) + ((long) (0xFF & b[5]) << 16) + ((long) (0xFF & b[6]) << 8) + (0xFF & b[7]); } } else if (g.datatype.charAt(0) == 'F') {//.equalsIgnoreCase("FLOAT")) { size = 4; b = new byte[size]; raf.read(b); ByteBuffer bb = ByteBuffer.wrap(b); if (g.byteorderLSB) { bb.order(ByteOrder.LITTLE_ENDIAN); } f = bb.getFloat(); } else if (g.datatype.charAt(0) == 'D') {//.equalsIgnoreCase("DOUBLE")) { size = 8; b = new byte[8]; raf.read(b); ByteBuffer bb = ByteBuffer.wrap(b); if (g.byteorderLSB) { bb.order(ByteOrder.LITTLE_ENDIAN); } f = (float) bb.getDouble(); } //replace not a number if ((float) f == (float) g.nodatavalue) { f = Float.NaN; } } catch (Exception e) { } return f; }
From source file:com.sangupta.snowpack.SnowpackRecover.java
/** * Try and recover from a chunk.//ww w. j a va2 s.co m * * @param chunkID * @param chunkFile * @param metadataDB * @return * @throws IOException */ private static ChunkInfo recoverChunkInfo(final int chunkID, final File chunkFile, SnowpackMetadataDB metadataDB) throws IOException { // open the file for reading RandomAccessFile raf = new RandomAccessFile(chunkFile, "r"); // read the length first int nameLength, length, terminator, headerLength, numFiles = 0; long offset; List<FlakeMetadata> metas = new ArrayList<FlakeMetadata>(); try { while (raf.getFilePointer() < raf.length()) { offset = raf.getFilePointer(); nameLength = raf.readInt(); byte[] name = new byte[nameLength]; raf.readFully(name); length = raf.readInt(); raf.readLong(); raf.skipBytes((int) length); terminator = raf.readByte(); if (terminator != 0) { System.out.print(" invalid descriptor found..."); return null; } headerLength = 4 + name.length + 4 + 8; numFiles++; metas.add(new FlakeMetadata(new String(name), nameLength, chunkID, offset, headerLength)); } } finally { raf.close(); } // all clear for recovery // save all metadata in new DB for (FlakeMetadata meta : metas) { metadataDB.save(meta); } // return chunk info ChunkInfo info = new ChunkInfo(); info.chunkID = chunkID; info.numFiles = numFiles; info.writePointer = -1; return info; }