Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-03 19:10:17 11月12日 Update

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!

Tags: File garbled character input method result problem object learning output UTF-8 previous generation image above content reason console more knowledge path help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Huawei vpn Microsoft Shulou Tech Info