Java读取文件的最后n位
2016-05-04 17:51
656 查看
以读取文件的最后6位为例:
public void readLastLine(String fileString) throws IOException { File file = new File(fileString); if (!file.exists() || file.isDirectory() || !file.canRead()) { return; } RandomAccessFile randomFile = null; try { System.out.println("随机读取一段文件内容:"); // 打开一个随机访问文件流,按只读方式 randomFile = new RandomAccessFile(file, "r"); // 文件长度,字节数 long start = randomFile.length() - 6; // 将读文件的开始位置移到beginIndex位置。 randomFile.seek(start); byte[] bytes = new byte[6]; int byteread = 0; // 一次读6个字节,如果文件内容不足6个字节,则读剩下的字节。 // 将一次读取的字节数赋给byteread while ((byteread = randomFile.read(bytes)) != -1) { for (int i = 0; i < bytes.length; i++) { Log.d("4345345", "byte" + i + "========="+ bytes[i]); } } } catch (IOException e) { e.printStackTrace(); } finally { if (randomFile != null) { try { randomFile.close(); } catch (IOException e1) { } } } }
相关文章推荐
- Java Calendar 日历类的基本使用
- Java中Vector与ArrayList的区别详解
- RxJava Retrofit2 链式请求(第N个请求的结果是第N+1个请求的参数)
- Demo 基于 Retrofit_RxJava, 并且采用了 MVP 模式
- 堆排序算法的讲解及Java版实现
- Java 位运算(移位、位与、或、异或、非)
- GreenDao在eclipse上的配置
- jdk的动态代理---学习笔记
- (转载)JavaWeb学习总结(五十三)——Web应用中使用JavaMail发送邮件
- 深入浅出RxJava(一:基础篇)
- (转载)JavaWeb学习总结(五十二)——使用JavaMail创建邮件和发送邮件
- JAVA获取同一路径下所有子类或接口实现类
- 为什么JAVA中的属性不能覆盖而方法却可以覆盖
- jmeter 打不开 提示“Not able to find Java executable or version”的解决办法
- (转载)JavaWeb学习总结(五十一)——邮件的发送与接收原理
- Java加密技术(十二)——*.PFX(*.p12)&个人信息交换文件
- 4、Java的继承
- Java加密技术(十一)——双向认证
- Java中集合框架辅助类Iteratror、Collections、Arrays
- java中的移位运算符与正负数转换