FileOutputStream与FileInputStream的随机访问文件读写
try (FileInputStream fis = new FileInputStream("/tmp/test/test.log")) { int byteData = fis.read(); // 返回值取值范围:[-1,255] if (byteData == -1) { return; // 读取到文件尾了 } byte data = (byte) byteData; // data为读取到的字节数据 } } } 至于读取到的字节数据如何使用就需要看你文件中存储的是什么数据了。 如果整个文件存储的是一张图片,那么需要将整个文件读取完,再按格式解析成图片,而如果整个文件是配置文件,则可以一行一行读取,遇到n换行符则为一行,代码如下。 public class FileInputStreamStu{ @Test public void testRead() throws IOException { try (FileInputStream fis = new FileInputStream("/tmp/test/test.log")) { ByteBuffer buffer = ByteBuffer.allocate(1024); int byteData; while ((byteData = fis.read()) != -1) { if (byteData == 'n') { buffer.flip(); (编辑:ASP站长) 【免责声明】本站内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。 |
-
无相关信息