List of usage examples for java.io RandomAccessFile RandomAccessFile
public RandomAccessFile(File file, String mode) throws FileNotFoundException
From source file:Main.java
public static void WriteTxtFile(Context context, String strcontent) { String strContent = strcontent + "\n"; try {//from www .j av a2 s .com File file = new File(context.getCacheDir() + File.separator + "wanjia.txt"); if (!file.exists()) { file.createNewFile(); } RandomAccessFile raf = new RandomAccessFile(file, "rw"); raf.seek(file.length()); raf.write(strContent.getBytes()); raf.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static byte[] getAsBinary(String key) { RandomAccessFile RAFile = null; boolean removeFile = false; try {/*from w ww . j ava2 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(); } } } }
From source file:Main.java
public static float getTotalRAM() { RandomAccessFile reader = null; String load = null;/*from w w w . j ava2 s . com*/ DecimalFormat twoDecimalForm = new DecimalFormat("#.##"); double totRam = 0; float lastValue = 0; try { reader = new RandomAccessFile("/proc/meminfo", "r"); load = reader.readLine(); // Get the Number value from the string Pattern p = Pattern.compile("(\\d+)"); Matcher m = p.matcher(load); String value = ""; while (m.find()) { value = m.group(1); // System.out.println("Ram : " + value); } reader.close(); totRam = Double.parseDouble(value); // totRam = totRam / 1024; float mb = (float) (totRam / 1024.0f); float gb = (float) (totRam / 1048576.0f); float tb = (float) (totRam / 1073741824.0f); lastValue = gb; } catch (IOException ex) { ex.printStackTrace(); } finally { // Streams.close(reader); } return lastValue; }
From source file:Main.java
public static void append(String fileName, byte[] bytes) throws Exception { File f = new File(fileName); long fileLength = f.length(); RandomAccessFile raf = new RandomAccessFile(f, "rw"); raf.seek(fileLength);/*from w ww . j a v a 2 s . c o m*/ raf.write(bytes); raf.close(); }
From source file:Main.java
public static boolean saveApk(File apk, String savePath) { FileInputStream in = null;/*from w ww. j a v a2s .co m*/ RandomAccessFile accessFile = null; try { in = new FileInputStream(apk); byte[] buf = new byte[1024 * 4]; int len; File file = new File(savePath); accessFile = new RandomAccessFile(file, "rw"); FileDescriptor fd = accessFile.getFD(); while ((len = in.read(buf)) != -1) { accessFile.write(buf, 0, len); } fd.sync(); accessFile.close(); in.close(); return true; } catch (Exception e) { e.printStackTrace(); try { if (in != null) { in.close(); } if (accessFile != null) { accessFile.close(); } } catch (IOException e1) { e1.printStackTrace(); } return false; } }
From source file:Main.java
public static byte[] readFromFile(String fileName, int offset, int len) { if (fileName == null) { return null; }//w w w. ja v a2s . c om File file = new File(fileName); if (!file.exists()) { //WLog.i(FileUtils.class, "readFromFile: file not found"); return null; } if (len == -1) { len = (int) file.length(); } if (offset < 0) { //WLog.e(FileUtils.class, "readFromFile invalid offset:" + offset); return null; } if (len <= 0) { //WLog.e(FileUtils.class, "readFromFile invalid len:" + len); return null; } if (offset + len > (int) file.length()) { //WLog.e(FileUtils.class, "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) { // WLog.e(FileUtils.class, "readFromFile : errMsg = " + e.getMessage()); e.printStackTrace(); } return b; }
From source file:Main.java
public static void initialWrite(String fileName) throws IOException { RandomAccessFile raf = new RandomAccessFile(fileName, "rw"); raf.writeInt(0);//from w w w.j a v a 2 s . co m raf.writeUTF("Hello world!"); raf.close(); }
From source file:Main.java
public static long truncateFile(String fileName, long size) throws FileNotFoundException, IOException { RandomAccessFile raf = new RandomAccessFile(fileName, "rw"); if (raf.length() >= size) { FileChannel channel = raf.getChannel(); channel.truncate(size);/*w w w . j a v a 2 s . com*/ return size; } return raf.length(); }
From source file:Main.java
public static byte[] readFromFile(String fileName, int offset, int len) { if (fileName == null) { return null; }//from ww w . ja v a 2s . c o m 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[] readFromFile(String fileName, int offset, int len) { if (fileName == null) { return null; }/*from w w w.j a v a2 s.c o m*/ 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; }