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

How to use BufferedReader to read File

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

Share

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

This article mainly introduces "how to use BufferedReader to read File". In daily operation, I believe many people have doubts about how to use BufferedReader to read File. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to use BufferedReader to read File". Next, please follow the editor to study!

Use BufferedReader to read File

There are clouds in Java programming ideas, and to improve speed, you can use BufferedReader filter classes to provide buffering areas. But this explanation is so simple that I still don't know why I use it after reading this sentence, so I think it's worth studying.

In fact, only using FileReader can also complete the task of reading files.

FileReader has three functions for read

Public int read (): only read one character at a time

Public int read (char cbuf [], int offset, int length): each read character from offset to offset+length-1, and then put into the cbuf

Public int read (char cbuf []): all characters from an one-time read are put directly into cbuf.

These three read methods will read one file and one IO each time they are called. Whether it is multiple read or disposable read, it is not very elegant in the way of read files. Multiple read is bound to produce multiple IO, and one-time read is extremely unfriendly to memory if it encounters very large files.

So BufferedReader shows its benefits. BufferedReader uses decorator mode, and its IO behavior is to read 8k of data into the buffer each time, and then take the data directly out of the buffer to use if you need to use it. In this way, it not only improves the reading speed, but also saves the number of IO, which is a more elegant way to read files.

Use BufferedReader to read File code for example BufferedReader bufferedReader = new BufferedReader (new FileReader (". / pom.xml"); StringBuilder stringBuilder = new StringBuilder (); while ((s = bufferedReader.readLine ())! = null) {stringBuilder.append (s + "\ n");} System.out.println (stringBuilder.toString ()); use BufferedReader to loop through files (details to be noticed by beginners)

Using BufferedReader (cache read stream), you can read one line of a file at a time. If the contents of the file are arranged by behavior unit, it is convenient to use BufferedReader to read the file.

Examples are as follows

1. BufferedReader reads one line

There is a txt file under the F disk with the contents of

Import java.io.*;public class test {public static void main (String [] args) {BufferedReader br = null; try {br = new BufferedReader ("F:\\ test.txt")); System.out.println (br.readLine ());} catch (IOException e) {e.printStackTrace ();}}

The result is:

Haha 0

2. BufferedReader cycle through each line import java.io.*;public class test {public static void main (String [] args) {BufferedReader br = null; try {br = new BufferedReader (new FileReader ("F:\\ test.txt")); String text = null / / if the read content is null, it means that the end of the file while ((text = br.readLine ())! = null) {System.out.println (text);}} catch (IOException e) {e.printStackTrace ();}}

The result is:

Haha 0

Haha 1

Haha 2

Haha 3

Haha 4

Haha 5

Haha 6

Haha 7

Haha 8

Ha ha 9

3. Pay attention to details

If a novice doesn't pay attention, it's easy to write:

Import java.io.*;public class test {public static void main (String [] args) {BufferedReader br = null; try {br = new BufferedReader (new FileReader ("F:\\ test.txt")); String text = null / / while (true) {if (br.readLine () = = null) {break;} System.out.println (br.readLine ());}} catch (IOException e) {e.printStackTrace ();}}

The result is:

Haha 1

Haha 3

Haha 5

Haha 7

Ha ha 9

Reason

While (true) {if (br.readLine () = = null) {break;} System.out.println (br.readLine ());}

Because in the while loop, the if statement actually reads a line when making a judgment, and the line read is not printed, so only part of it is printed.

At this point, the study on "how to use BufferedReader to read File" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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