List of usage examples for java.io RandomAccessFile close
public void close() throws IOException
From source file:Cat.java
public static void concatenate(String fileName) { RandomAccessFile file = null; String line = null;/*from w ww. ja v a2s .c o m*/ try { file = new RandomAccessFile(fileName, "r"); while ((line = file.readLine()) != null) { System.out.println(line); } return; } catch (FileNotFoundException fnf) { System.err.println("File: " + fileName + " not found."); } catch (Exception e) { System.err.println(e.toString()); } finally { if (file != null) { try { file.close(); } catch (IOException io) { } } } }
From source file:Main.java
private static byte[] readFileToBuffer(String filePath) { File inFile = new File(filePath); if (!inFile.exists()) { Log.d(TAG, "<readFileToBuffer> " + filePath + " not exists!!!"); return null; }//w w w. j a v a2 s . c o m RandomAccessFile rafIn = null; try { rafIn = new RandomAccessFile(inFile, "r"); int len = (int) inFile.length(); byte[] buffer = new byte[len]; rafIn.read(buffer); return buffer; } catch (IOException e) { Log.e(TAG, "<readFileToBuffer> Exception ", e); return null; } finally { try { if (rafIn != null) { rafIn.close(); rafIn = null; } } catch (IOException e) { Log.e(TAG, "<readFileToBuffer> close IOException ", e); } } }
From source file:org.apache.hadoop.dfs.TestDatanodeBlockScanner.java
private static void truncateReplica(String blockName, int dnIndex) throws IOException { File baseDir = new File(System.getProperty("test.build.data"), "dfs/data"); for (int i = dnIndex * 2; i < dnIndex * 2 + 2; i++) { File blockFile = new File(baseDir, "data" + (i + 1) + "/current/" + blockName); if (blockFile.exists()) { RandomAccessFile raFile = new RandomAccessFile(blockFile, "rw"); raFile.setLength(raFile.length() - 1); raFile.close(); break; }/*w w w .j a va 2 s .co m*/ } }
From source file:com.lightbox.android.bitmap.BitmapUtils.java
public static void writeBitmapInFile(File file, Bitmap bitmap, CompressFormat compressFormat, StringBuilder outMD5) throws IOException { // Ensure that the directory exist file.getParentFile().mkdirs();//from w w w . java 2 s. co m OutputStream outputStream = null; try { if (outMD5 != null) { // We want a MD5: writing JPEG into a byte array outputStream = new ByteArrayOutputStream(); } else { // Directly write to file try { outputStream = new BufferedOutputStream(new RandomAccessFileOutputStream(file), 65536); } catch (OutOfMemoryError e) { outputStream = new BufferedOutputStream(new RandomAccessFileOutputStream(file)); } } boolean success = bitmap.compress(compressFormat, FULL_QUALITY, outputStream); if (!success) { throw new IOException(String.format("Unable to save bitmap as a %s file: %s", (compressFormat == CompressFormat.JPEG) ? "jpeg" : "png", file.getAbsoluteFile().toString())); } else { if (outMD5 != null) { // Calculate MD5 and write the file to disk long time = System.currentTimeMillis(); byte[] jpeg = ((ByteArrayOutputStream) outputStream).toByteArray(); if (outMD5 != null) { outMD5.append(getMD5String(jpeg)); } DebugLog.d(TAG, "Time to calculate MD5: " + (System.currentTimeMillis() - time)); time = System.currentTimeMillis(); RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rwd"); randomAccessFile.write(jpeg); randomAccessFile.close(); DebugLog.d(TAG, "Time to write to file: " + (System.currentTimeMillis() - time)); } } } finally { IOUtils.closeQuietly(outputStream); } }
From source file:org.apache.hadoop.hdfs.TestDatanodeBlockScanner.java
/** * Change the length of a block at datanode dnIndex */// w ww .j a v a2 s. c o m static boolean changeReplicaLength(String blockName, int dnIndex, int lenDelta) throws IOException { File baseDir = new File(System.getProperty("test.build.data"), "dfs/data"); for (int i = dnIndex * 2; i < dnIndex * 2 + 2; i++) { File blockFile = new File(baseDir, "data" + (i + 1) + "/current/" + blockName); if (blockFile.exists()) { RandomAccessFile raFile = new RandomAccessFile(blockFile, "rw"); raFile.setLength(raFile.length() + lenDelta); raFile.close(); return true; } } return false; }
From source file:eu.delving.metadata.Hasher.java
public static String quickHash(File file) throws IOException { Hasher hasher = new Hasher(); RandomAccessFile raf = new RandomAccessFile(file, "r"); byte[] chunk = new byte[QUICK_SAMPLE_SIZE]; long length = raf.length() - chunk.length; long step = length / QUICK_SAMPLES; for (int walk = 0; walk < QUICK_SAMPLES; walk++) { raf.seek(step * walk);//from w w w. ja v a 2s. com raf.readFully(chunk); hasher.update(chunk, chunk.length); } raf.close(); return hasher.getHashString().substring(4, 14); }
From source file:org.apache.hadoop.hdfs.TestClientReportBadBlock.java
/** * Corrupt a block on a data node. Replace the block file content with * content/*www . j ava 2 s.c o m*/ * of 1, 2, ...BLOCK_SIZE. * * @param block * the ExtendedBlock to be corrupted * @param dn * the data node where the block needs to be corrupted * @throws FileNotFoundException * @throws IOException */ private static void corruptBlock(final ExtendedBlock block, final DataNode dn) throws FileNotFoundException, IOException { final File f = DataNodeTestUtils.getBlockFile(dn, block.getBlockPoolId(), block.getLocalBlock()); final RandomAccessFile raFile = new RandomAccessFile(f, "rw"); final byte[] bytes = new byte[(int) BLOCK_SIZE]; for (int i = 0; i < BLOCK_SIZE; i++) { bytes[i] = (byte) (i); } raFile.write(bytes); raFile.close(); }
From source file:Main.java
public static ByteBuffer fromFile(File file) throws IOException { RandomAccessFile raf = null; FileChannel channel = null;/*from w w w . j a va 2 s . c o m*/ try { raf = new RandomAccessFile(file, "r"); channel = raf.getChannel(); return channel.map(FileChannel.MapMode.READ_ONLY, 0, file.length()).load(); } finally { if (channel != null) { try { channel.close(); } catch (IOException e) { // Ignored. } } if (raf != null) { try { raf.close(); } catch (IOException e) { // Ignored. } } } }
From source file:com.geekandroid.sdk.pay.utils.Util.java
public static byte[] readFromFile(String fileName, int offset, int len) { if (fileName == null) { return null; }//from w w w. j a va 2s. c om File file = new File(fileName); if (!file.exists()) { Log.i(TAG, "readFromFile: file not found"); return null; } if (len == -1) { len = (int) file.length(); } Log.d(TAG, "readFromFile : offset = " + offset + " len = " + len + " offset + len = " + (offset + len)); if (offset < 0) { Log.e(TAG, "readFromFile invalid offset:" + offset); return null; } if (len <= 0) { Log.e(TAG, "readFromFile invalid len:" + len); return null; } if (offset + len > (int) file.length()) { Log.e(TAG, "readFromFile invalid file len:" + file.length()); return null; } byte[] b = null; try { RandomAccessFile in = new RandomAccessFile(fileName, "r"); b = new byte[len]; // ?? in.seek(offset); in.readFully(b); in.close(); } catch (Exception e) { Log.e(TAG, "readFromFile : errMsg = " + e.getMessage()); e.printStackTrace(); } return b; }
From source file:Main.java
public static byte[] getAsBinary(String key) { RandomAccessFile RAFile = null; boolean removeFile = false; try {//w ww. ja v a 2 s . c o m File file = new File(cacheDir, key); if (!file.exists()) return null; RAFile = new RandomAccessFile(file, "r"); byte[] byteArray = new byte[(int) RAFile.length()]; RAFile.read(byteArray); return byteArray; } catch (Exception e) { e.printStackTrace(); return null; } finally { if (RAFile != null) { try { RAFile.close(); } catch (IOException e) { e.printStackTrace(); } } } }