广告
返回顶部
首页 > 资讯 > 精选 >Java中常见的IO读写效率对比
  • 670
分享到

Java中常见的IO读写效率对比

2023-06-17 10:06:52 670人浏览 安东尼
摘要

这篇文章主要介绍“Java中常见的io读写效率对比”,在日常操作中,相信很多人在Java中常见的IO读写效率对比问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Java中常见的IO读写效率对比”的疑惑有所帮助!

这篇文章主要介绍“Java中常见的io读写效率对比”,在日常操作中,相信很多人在Java中常见的IO读写效率对比问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Java中常见的IO读写效率对比”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

Java中的IO的类库非常的庞大,选择性非常的多,当面临一个问题时,往往不知道如何下手!

更具我现在的理解,在效率不是非常重要的情况下,一般情况下可能只需要考虑两种情况,即想按照字节去读取,还是想按照行去读取,而一般情况无论采取什么方式去读取,***的方式都莫过于用Buffered...去包装要是用的类,而如果效率要求比较高则可以考虑是用FileChannel 或者是 file map,其中file Map是读写效率***的一种方式,如果读取的文件非常的大这种方式是***,下面的例子是对常见几种读文件方式的效率比较,通过一个动态代理的模式来统计每个方法的执行时间,测试文件是100多兆的数据文件。

package com.eric.io;   import java.io.BufferedInputStream;  import java.io.BufferedOutputStream;  import java.io.BufferedReader;  import java.io.BufferedWriter;  import java.io.ByteArrayInputStream;  import java.io.DatainputStream;  import java.io.DataOutputStream;  import java.io.File;  import java.io.FileInputStream;  import java.io.FileOutputStream;  import java.io.FileReader;  import java.io.FileWriter;  import java.io.IOException;  import java.io.InputStream;  import java.NIO.ByteBuffer;  import java.nio.CharBuffer;  import java.nio.channels.FileChannel;   import com.eric.reflect.ExecuteTimerHandler;   public class ReadFileTools implements IReadFileTools {                 public static final int     BUFFSIZE     = 180;      public static final String  root         = "E:\\sourcecode\\corejava\\src\\com\\eric\\io\\";      public static final boolean printContext    = false;            public static void main(String[] args) throws Exception {          String file = root + "VISA_INPUT_FULL";          IReadFileTools bi = (IReadFileTools) ExecuteTimerHandler.newInstance(new ReadFileTools());          bi.readByBufferReader(file);          bi.readByBufferedInputStreamNoArray(file);          bi.readByBufferedInputStream(file);          bi.readByChannel(file);          bi.readByChannelMap(file);          bi.readByDataInputStream(file);      }                 public String readByBufferReader(String file) {          StringBuilder sb = new StringBuilder();          try {              BufferedReader br = new BufferedReader(new FileReader(new File(file)));              String line;              long count = 0;              while ((line = br.readLine()) != null) {                  if (printContext) {                      System.out.println(line);                  }                                    sb.append(line);                  count += line.length();              }              br.close();          } catch (Exception ex) {              ex.printStackTrace();          }          return sb.toString();      }            public void readByDataInputStream(String file) throws Exception {                    DataInputStream dis = new DataInputStream(new ByteArrayInputStream(new ReadFileTools().readByBufferReader(file).getBytes()));          while (dis.available() > 0) {              char c = (char) dis.read();              if (printContext) {                  System.out.println(c);              }          }      }      //this method not use byte array to get byte      public String readByBufferedInputStreamNoArray(String file) {          try {              InputStream is = new BufferedInputStream(new FileInputStream(new File(file)));              while (is.available() > 0) {                  char c = (char) is.read();                  if (printContext) {                      System.out.println(c);                  }              }          } catch (Exception ex) {              ex.printStackTrace();          }          return null;      }      //use byte array to get bytes from file      public void readByBufferedInputStream(String file) throws Exception {          BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));          byte[] bytes = new byte [BUFFSIZE];          while (input.available() > 0) {              input.read(bytes);          }      }      //use file channel to get byte from file      public void readByChannel(String file) throws Exception {                    FileChannel in = new FileInputStream(file).getChannel();          ByteBuffer buffer = ByteBuffer.allocate(BUFFSIZE);          while (in.read(buffer) != -1) {              buffer.flip(); // Prepare for writing              if (printContext) {                  System.out.println(buffer.getChar());              }              buffer.clear(); // Prepare for reading          }          in.close();      }      //use MappedByteBuffer to read byte from file      public void readByChannelMap(String file) throws Exception {          FileChannel fc = new FileInputStream(new File(file)).getChannel();          CharBuffer cb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()).asCharBuffer();          char c;          while (cb.hasRemaining())              c = cb.get();          if (printContext) {              System.out.println(c);          }          fc.close();      }            public void copyFileByChannel(String file, String file2) throws Exception {                    FileChannel in = new FileInputStream(file).getChannel();          FileChannel out = new FileOutputStream(file2).getChannel();          ByteBuffer buffer = ByteBuffer.allocate(BUFFSIZE);          while (in.read(buffer) != -1) {              buffer.flip(); // Prepare for writing              out.write(buffer);              buffer.clear(); // Prepare for reading          }      }            public void test() {          System.out.println("test");      }            public void copyFile(String source, String dest) throws Exception {          BufferedReader br = new BufferedReader(new FileReader(new File(source)));          BufferedWriter bw = new BufferedWriter(new FileWriter(new File(dest)));          String temp;          while ((temp = br.readLine()) != null) {              bw.write(temp + "\n");          }      }            public void storingAndRecoveringData(String file) throws Exception {          DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));          DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));          dos.writeBoolean(false);          dos.writeByte(10);          dos.writeDouble(1213654);          dos.writeUTF("aihua");          dos.close();          System.out.println(dis.readBoolean());          System.out.println(dis.readByte());          System.out.println(dis.readDouble());          System.out.println(dis.readUTF());          dis.close();                }            public void doCopyFile(String src, String dest) throws IOException {          File srcFile = new File(src);          File destFile = new File(dest);          if (destFile.exists()) {              boolean d = destFile.delete();                            if (d) {                  System.out.print("删除成功!");              } else {                  System.out.print("删除失败!");              }          }          BufferedInputStream input = new BufferedInputStream(new FileInputStream(srcFile));          try {              BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(destFile));              try {                  byte[] buffer = new byte [4096];                  int n = 0;                  while (-1 != (n = input.read(buffer))) {                      output.write(buffer, 0, n);                  }                  System.out.println("Copy Successful::" + dest);              } finally {                  try {                      if (output != null) {                          output.close();                      }                  } catch (IOException ioe) {                      ioe.printStackTrace();                  }              }          } finally {              try {                  if (input != null) {                      input.close();                  }              } catch (IOException ioe) {                  System.out.println("failed src file:" + src + " reason:" + ioe.getMessage());              }          }      }        }   

到此,关于“Java中常见的IO读写效率对比”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!

--结束END--

本文标题: Java中常见的IO读写效率对比

本文链接: https://www.lsjlt.com/news/288417.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Java中常见的IO读写效率对比
    这篇文章主要介绍“Java中常见的IO读写效率对比”,在日常操作中,相信很多人在Java中常见的IO读写效率对比问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Java中常见的IO读写效率对比”的疑惑有所帮助!...
    99+
    2023-06-17
  • Java集合中contains方法的效率对比分析
    最近让部门技术大佬帮忙代码review的时候,他给我指出了一个小的技术细节,就是对于集合的contains方法尽量选用Set而不是List,平时没怎么注意,仔细看了下源码,大佬就是大...
    99+
    2022-11-12
  • java中int转string与string转int的效率对比
    目录int转string与string转int的效率对比string转int,两种方法int转string一般用三种方法string转int问题分析分析一波1、针对传入的字符串为空对...
    99+
    2022-11-13
  • Java中的异常对程序效率有没有影响
    本篇内容介绍了“Java中的异常对程序效率有没有影响”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!当异常没有发生时,没有影响。其实从异常实现...
    99+
    2023-06-17
  • Java开发中,如何避免文件读写出现的常见问题?
    Java开发中,文件读写是常见的操作之一。然而,由于各种原因,文件读写可能会出现一些常见问题。这些问题可能会导致数据损坏,程序崩溃等严重后果。因此,在Java开发中,我们需要特别注意文件读写操作,并采取一些措施来避免常见问题的出现。 本文将...
    99+
    2023-09-12
    关键字 开发技术 文件
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作