Test examples:
Java read UTF-8 TXT file The first line is garbled "?" and solve
Test.txt File Contents:
1
00:00:06,000-00:00:06,010
<b>Allerleirauh</b> (2012)
<i>dtv-das Erste-20. Januar 2013</i>
2
00:00:10,280-00:00:12,680
Was Geh?rt zu einer guten Suppe?
3
00:00:14,200-00:00:15,839
Eine Gute Suppe ...
Test.txt files are saved in WordPad as UTF-8 format (here is a UTF-8 file with a BOM)
Save and close after using WordPad to open the UTF-8 document again, Chinese, letter normal display
Test code:
public static string Srt2txt (string filename) {file infile = new File (filename); String realfile = filename.substring (0, Filename.lastindexof (". SRT")) + ". txt"; String tempfile = realfile.replace ('/', ' \ \ '),//windows writes the file path format to filename outfile = new file (tempfile); BufferedReader bufferedreader = null; BufferedWriter BufferedWriter = null;try {BufferedReader = new BufferedReader (new FileReader (infile)); BufferedWriter = New BufferedWriter (New FileWriter (outfile)); String line;//is used to hold the contents of a row every time it reads (line = Bufferedreader.readline ()) = null) {lines = new String (Line.getbytes ("ISO-8859 -1 ")," iso-8859-1 "); Bufferedwriter.write (line); Bufferedwriter.newline ();//indicates newline Bufferedwriter.flush ();}} catch (IOException e) {e.printstacktrace ();} Finally{if (null! = BufferedReader) {try {bufferedreader.close ();} catch (IOException e) {e.printstacktrace ();}} if (null! = BufferedWriter) {try {bufferedwriter.close ();} catch (IOException e) {e.printstacktrace ();}}} return realfile;}
Test results:
??
00:00:06,000-00:00:06,010
<b>Allerleirauh</b> (2012)
<i>dtv-das Erste-20. Januar 2013</i>
2
00:00:10,280-00:00:12,680
Was Geh?rt zu einer guten Suppe?
3
00:00:14,200-00:00:15,839
Eine Gute Suppe ...
Workaround:
Use UltraEdit to save the above TXT file as UTF-8 no BOM format;
Use notepad++ to open the TXT file above and do the following "format-UTF-8 no BOM format encoding", and then save the TXT text after modification.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java read UTF-8 format file The first line appears garbled--question mark "?" and to solve and Java read with BOM UTF-8 file garbled reason and solution