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

The solution of garbled code in Java file character input stream FileReader reading txt file

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

Share

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

This article mainly introduces "the solution of Java file character input stream FileReader reading txt file garbled code". In the daily operation, it is believed that many people have doubts about the solution of Java file character input stream FileReader reading txt file garbled code. The editor consulted all kinds of materials and sorted out simple and useful operation methods. Hope to answer "Java file character input stream FileReader read txt file garbled solution" to help! Next, please follow the editor to study!

Catalogue

Java file character input stream FileReader reads txt file garbled

Put the code first.

The output from the console is as follows

The reason is

The result after running is

The problem of garbled code in reading UTF-8 and writing out txt files by character stream

Don't say much, just go straight to the picture.

Solve

Java file character input stream FileReader reads txt file garbled first code public class FileInAndOut {public static void main (String [] args) {/ / define the File object File file = new File of the file on the specified disk ("java / second semester / Android/ assignment requirements / java knowledge consolidation / work5.txt"); if (! File.exists () {System.out.println ("sorry, file that does not contain the specified path");} else {/ / create the FileReader object try {FileReader fr = new FileReader (file) based on the File object of the specified path Char [] data = new char [51]; / / define the char array int length = 0 While ((length = fr.read (data)) > 0) {/ / read data in the file String str = new String (data,0,length) / / create a String object System.out.println (str) based on the contents of the read file; / / output the read content} fr.close () / / close the stream} catch (Exception e) {/ / TODO Auto-generated catch block e.printStackTrace () }} the console output is as follows

The reason is

The most basic unit of character stream processing in Java is the Unicode symbol (size 2 bytes), so we need to change the encoding format of the file to utf-8 when we save it.

The result after running is

The problem of garbled code in reading UTF-8 and writing out txt files by character stream

The problem of garbled code has always been a very difficult problem. This paper solves the problem of using character stream to read UTF-8 and write txt file garbled in Java.

Don't say much, just go straight to the picture.

Output result:

Use the code:

Solve

At this point, on the "Java file character input stream FileReader read txt file garbled solution" on the end of the study, 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