前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java中实现顺序IO

Java中实现顺序IO

作者头像
付威
发布2020-02-17 11:23:04
6K15
发布2020-02-17 11:23:04
举报
顺序IO和随机IO

对于磁盘的读写分为两种模式,顺序IO和随机IO。 随机IO存在一个寻址的过程,所以效率比较低。而顺序IO,相当于有一个物理索引,在读取的时候不需要寻找地址,效率很高。

网上盗了一个图(侵权删)

IO
IO

Java中的随机读写

在Java中读写文件的方式有很多种,先总结以下3种方法:

  1. FileWriter和FileReader
public static void fileWrite(String filePath, String content) {
            File file = new File(filePath);
            //创建FileWriter对象
            FileWriter writer = null;
            try {
                 //如果文件不存在,创建文件
                 if (!file.exists())
                      file.createNewFile();
                 writer = new FileWriter(file);
                 writer.write(content);//写入内容
                 writer.flush();
                 writer.close();
            } catch (IOException e) {
                 e.printStackTrace();
            }
       }
          
       public static void fileRead(String filePath) {
            File file = new File(filePath);
            if (file.exists()) {
                 try {
                      //创建FileReader对象,读取文件中的内容
                      FileReader reader = new FileReader(file);
                      char[] ch = new char[1];
                      while (reader.read(ch) != -1) {
                           System.out.print(ch);
                      }
                      reader.close();
                 } catch (IOException ex) {
                      ex.printStackTrace();
                 }
                    
            }
       } 

  1. BufferedReader和BufferedWriter BufferedReader和BufferedWriter与FileWriter和FileReader代码的写法一致,Buffer也多了一个读取一行字符的操作。
  public class BuffredRWHelper {
       public static void fileWrite(String filePath, String content) {
            File file = new File(filePath);
            //创建FileWriter对象
            BufferedWriter writer = null;
            try {
                 //如果文件不存在,创建文件
                 if (!file.exists())
                      file.createNewFile();
                 writer = new BufferedWriter(new FileWriter(file));
                 writer.write(content);//写入内容
                 writer.flush();
                 writer.close();
            } catch (IOException e) {
                 e.printStackTrace();
            }
       }
          
       public static void fileRead(String filePath) {
            File file = new File(filePath);
            if (file.exists()) {
                 try {
                      //创建FileReader对象,读取文件中的内容
                      BufferedReader reader = new BufferedReader(new FileReader(file));
                      String line;
                      while ((line = reader.readLine()) != null) {
                           System.out.print(line);
                      }
                      reader.close();
                 } catch (IOException ex) {
                      ex.printStackTrace();
                 }
                    
            }
       }
  } 

  1. FileInputStream和FileOutputStream 使用Stream的形式是最原始的方式,以字节数组为中间的中转缓解
public static void fileWrite(String filePath, String content) {
     FileOutputStream outputStream = null;
     try {
         File file = new File(filePath);
         boolean isCreate = file.createNewFile();//创建文件
         if (isCreate) {
             outputStream = new FileOutputStream(file);//形参里面可追加true参数,表示在原有文件末尾追加信息
             outputStream.write(content.getBytes());
         }else {
             outputStream = new FileOutputStream(file,true);//表示在原有文件末尾追加信息
             outputStream.write(content.getBytes());
         }
     } catch (Exception e) {
         e.printStackTrace();
     } finally {
         try {
             outputStream.close();
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
 }
	
 public static void fileRead(String filePath) {
     File file = new File(filePath);
     if (file.exists()) {
         try {
             //创建FileInputStream对象,读取文件内容
             FileInputStream fis = new FileInputStream(file);
             byte[] bys = new byte[1024];
             while (fis.read(bys, 0, bys.length) != -1) {
                 //将字节数组转换为字符串
                 System.out.print(new String(bys, StandardCharsets.UTF_8));
             }
         } catch (IOException ex) {
             ex.printStackTrace();
         }
			
     }
 } 
Java中的顺序读写

上面的对文件的读写都是随机读写,如果用来写比较小的日志文件还能满足要求,如果用来操作一个文件的读写,那可能带来很大的性能消耗。

顺序IO的读写在中间件使用的很频繁,尤其是在队列中。几乎所有的队列(kafka,qmq等使用文件存储消息)都采用了顺序IO读写。

与随机读写不同的是,顺序读写是优先分配一块文件空间,然后后续内容追加到对应空间内。

在使用顺序IO进行文件读写时候,需要知道上次写入的地方,所以需要维护一个索引或者轮询获得一个没有写入位置。

public static long fileWrite(String filePath, String content, int index) {
     File file = new File(filePath);
     RandomAccessFile randomAccessTargetFile;
     MappedByteBuffer map;
     try {
          randomAccessTargetFile = new RandomAccessFile(file, "rw");
          FileChannel targetFileChannel = randomAccessTargetFile.getChannel();
          map = targetFileChannel.map(FileChannel.MapMode.READ_WRITE, 0, (long) 1024 * 1024 * 1024);
          map.position(index);
          map.put(content.getBytes());
          return map.position();
     } catch (IOException e) {
          e.printStackTrace();
     } finally {
     }
     return 0L;
}

public static String fileRead(String filePath, long index) {
     File file = new File(filePath);
     RandomAccessFile randomAccessTargetFile;
     MappedByteBuffer map;
     try {
          randomAccessTargetFile = new RandomAccessFile(file, "rw");
          FileChannel targetFileChannel = randomAccessTargetFile.getChannel();
          map = targetFileChannel.map(FileChannel.MapMode.READ_WRITE, 0, index);
          byte[] byteArr = new byte[10 * 1024];
          map.get(byteArr, 0, (int) index);
          return new String(byteArr);
     } catch (IOException e) {
          e.printStackTrace();
     } finally {
     }
     return "";
}

(本文完)

作者:付威 博客地址:http://blog.laofu.online 如有任何知识产权、版权问题或理论错误,还请指正。 本文是付威的网络博客原创,自由转载-非商用-非衍生-保持署名,请遵循:创意共享3.0许可证

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-02-02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 顺序IO和随机IO
  • Java中的随机读写
  • Java中的顺序读写
相关产品与服务
文件存储
文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档