Recently encountered a small problem, it is normal to read and write txt files in the UTF-8 format in the Eclipse environment, but when exported to a jar package, there are some Chinese garbled problems when reading and writing files by clicking.
Workaround:
Started with FileReader and FileWriter to read and write, later changed to use InputStreamReader and outputstreamwriter after the problem solved
Key code:
BufferedReader reader; BufferedWriter Writer;//reader = new BufferedReader (new FileReader (New File (Filedir,filename))); reader = new BufferedReader (New InputStreamReader (New FileInputStream (Filedir,filename), "UTF-8"));//writer = new BufferedWriter (New FileWriter (New File (filedir,emaildomain+ ". txt"), true)); writer = new BufferedWriter (new OutputStreamWriter (New FileOutputStream (New File (filedir,emaildomain+ ". txt"), true), "UTF-8");
(PS: Do not control the code in the specific file path and file name, to understand the idea can be)
This article is from the "Zifangsky" blog, make sure to keep this source http://983836259.blog.51cto.com/7311475/1661761
Solve the problem of reading and writing UTF-8 files in Eclipse when Java project is exported to jar package