This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details

Read plain text files in Java?

There seem to be multiple ways to read and write file data in Java.

I want to read ASCII data from a file. What are the possible approaches and their differences?

Answer:


A lot of knowledge points, really need to write out will master ! ! !   \color{purple} a lot of knowledge points, really need to write out just can master!! {~}

ASCII is a TEXT file, so you use Readers for reading. Java also supports InputStreams using read binaries. If you are reading large files, you may want to use aFileReader on aBufferedReader to improve reading performance.

Read this article carefully to see how to use Reader

I also recommend that you download and read this wonderful (not yet free) book called Thinking In Java.

In Java 7:

new String(Files.readAllBytes(...) ) (docs) or files.readalllines (...)Copy the code

In Java 8:

Files.lines(..) .forEach(...)Copy the code

Answer:

My favorite way to read small files is to use BufferedReader and StringBuilder. It’s very simple and critical (not particularly effective, but sufficient for most cases) :

BufferedReader br = new BufferedReader(new FileReader("file.txt")); try { StringBuilder sb = new StringBuilder(); String line = br.readLine(); while (line ! = null) { sb.append(line); sb.append(System.lineSeparator()); line = br.readLine(); } String everything = sb.toString(); } finally { br.close(); }Copy the code

It was pointed out that after Java 7, you should use try-with-Resources (i.e., automatic shutdown) :

try(BufferedReader br = new BufferedReader(new FileReader("file.txt"))) { StringBuilder sb = new StringBuilder(); String line = br.readLine(); while (line ! = null) { sb.append(line); sb.append(System.lineSeparator()); line = br.readLine(); } String everything = sb.toString(); }Copy the code

When I read strings like this, I usually want to do some string manipulation on each line anyway, so I continue with the implementation.

Although I always use Apache Commons IO with the ioutils.toString () method if I really just want to read a file into a string. You can view the source code here:

www.docjar.com/html/api/or…

FileInputStream inputStream = new FileInputStream("foo.txt");
try {
    String everything = IOUtils.toString(inputStream);
} finally {
    inputStream.close();
}
Copy the code

Using Java 7 is even easier:

try(FileInputStream inputStream = new FileInputStream("foo.txt")) {     
    String everything = IOUtils.toString(inputStream);
    // do something with everything string
}
Copy the code

The article translated from yl2gl72eozkinivz3vc6swkesy – ac4c6men2g7xr2a – translate. Translate. Goog/questions / 4…

Write to a fileHow do I create and write a file?

The mainstream replication methods under single thread are as follows:

1, FileChannel

2, a FileInputStream

3, BufferedOutputStream

4, BufferedReader

5, FileReader

Take FileChannel, the fastest method in a single thread, and do multithreading

A simple comparison:

As you can see, The Performance of FileChannel is relatively high. Briefly, take rocketMQ, which is a popular file system for storing data, producing and consuming data as directly manipulated files, It involves page caching, FileChannel, and FileChannel reading a page of 4KB at a time, thanks to ByteBuffer buffers and MMAP memory mapping

RokcetMQ has also been tuned for better performance

  • Pre-allocated MappedFile
  • Mlock system call
  • File preheating
  • Read and write sequentially

I’m just doing a piece here, everybody come on!


Welcome to my column S t a c k O v e r F l o w . I select the best questions and answers and test them frequently in interviews ! ! !   \color{red} Welcome to my column StackOverFlow, I will filter the quality of the interview test!! {~}


There are the latest and elegant ways to do this, and I will write my thoughts on this q&A at the end of the article \color{red} has the latest, elegant implementation, and I will also write my opinion on this question at the end of the article {~}

Thank you for reading this, if this article is well written and if you feel there is something to it

Ask for a thumbs up 👍 ask for attention ❤️ ask for share 👥 for 8 abs I really very useful!!

If there are any mistakes in this blog, please comment, thank you very much! ❤ ️ ❤ ️ ❤ ️ ❤ ️