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 does JAVA read the contents of a text file

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "how to read the contents of a text file by JAVA". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Java reads the contents of a text file

Today, I wrote code to debug a long string, so I created a new text text with idea to store the contents of the long string. It turns out that the java code that reads the contents of the text file is not very good at writing. Sure enough, it is for Baidu programming, for control c or control v programming, awkward.

The final code is as follows:

Public static String readFileContent (String fileName) {File file = new File (fileName); BufferedReader reader = null; StringBuffer sbf = new StringBuffer (); try {reader = new BufferedReader (new FileReader (file)); String tempStr; while ((tempStr = reader.readLine ())! = null) {sbf.append (tempStr);} reader.close (); return sbf.toString ();} catch (IOException e) {e.printStackTrace ();} finally {if (reader! = null) {try {reader.close () } catch (IOException E1) {e1.printStackTrace ();} return sbf.toString ();}

Leave a small problem, this way can only read ordinary text files, for binary and other files, it is not allowed, so what should be done, well, wait for the needs of such a scenario to record it.

Java reads all the contents of a text file at once

When we do text processing, the most commonly used thing is to read and write files, especially to read files. No matter what files they are, I tend to read the original contents of the text directly into memory at one time and then process them. Of course, this requires you to have a machine with large memory, those who do not have enough memory. You can read a small part of the content at once and read it multiple times.

The fastest way to read a file is to read it all at once. Many people use methods such as readline (), which may need to access the file repeatedly, and each time readline () will call transcoding, which slows down the speed. Therefore, in the case of known encoding, the file is first read into memory by byte stream, and then one-time encoding conversion is the fastest way. The typical code is as follows:

Public String readToString (String fileName) {String encoding = "UTF-8"; File file = new File (fileName); Long filelength = file.length (); byte [] filecontent = new byte [filelength.intValue ()]; try {FileInputStream in = new FileInputStream (file); in.read (filecontent); in.close ();} catch (FileNotFoundException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace ();} try {return new String (filecontent, encoding) } catch (UnsupportedEncodingException e) {System.err.println ("The OS does not support" + encoding); e.printStackTrace (); return null;}}

This is the end of the content of "how JAVA reads the contents of a text file". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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