Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of read and write operation of java efficient file stream

2025-03-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "detailed explanation of read and write operation of java efficient file stream". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "java efficient file stream read and write operation detailed explanation" bar!

Introduction

In case you forget later, record the performance comparison of some file streams.

Usually, it is often used to read and write files. Java provides many classes for manipulating files. Generally speaking, file operations are also called stream operations, and can be classified in the following ways:

Classified by function, byte stream and character stream. According to the node flow and filter flow, the node flow directly manipulates the file, and the filter flow wraps the node flow and filter flow. For example, FileInputStream and BufferedFileInputStream are node flow and filter flow, respectively.

File stream comparison

Let's focus on comparing several streams that we often use.

(1) DataInputStream+FileInputStream

(2) DataInputStream+BufferedInputStream

(3) BufferedReader+FileReader (buffer stream size varies with performance, such as 1K and 8K)

(4) BufferedFileReader

Post the conclusion first:

The time-consuming comparison is:

(4) better than (3)-8096 better than (3)-1024 better than (2) = nChars) {n = fr.read (buffer, 0, 8192); if (n =-1) {/ / EOF break;} nChars = n; startChar = 0; nextChar = 0;} for (j = nextChar; j)

< nChars; j++) { c = buffer[j]; if ((c == '\n') || (c == '\r')) { eol = true; break; } } nextChar = j; int len = nextChar - startChar; if (eol) { nextChar++; if ((lineLength + len) >

MaxLineLength) {/ / error} else {System.arraycopy (buffer, startChar, lineBuf, lineLength, len);} lineLength + = len; / Process line here / / nlines++; if (c ='\ r') {if (nextChar > = nChars) {n = fr.read (buffer, 0, 8192); if (n! =-1) {nextChar = 0; nChars = n;} if (nextChar

< nChars) && (buffer[nextChar] == '\n')) nextChar++; } startChar = nextChar; lineLength = 0; continue; } if ((lineLength + len) >

MaxLineLength) {/ / error} else {System.arraycopy (buffer, startChar, lineBuf, lineLength, len);} lineLength + = len;} fr.close ();} catch (Exception e) {System.out.println ("exception:" + e);}

NIO file stream

FileChanel introduction

The channel, the previous stream is unidirectional, while the channel is bi-directional, which can be read or written. The efficiency of reading files can be found here.

At this point, I believe that everyone on the "java efficient file stream read and write operation of the detailed understanding" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report