I am trying to read file using Java and the file also containing special characters also. I am trying to write the contents of file into another file.
What is the solution to read special character files?
使用UTF-8编码文件,并使用支持Java编码的流运算符:
new InputStreamReader(new FileInputStream(file), "UTF-8")
You need to find out the exact encoding that the file uses, and then specify that in the
InputStreamReader
parameter.
Nevertheless it is often best to reader character data by wrapping your InputStream with an InputStreamReader into Reader or BufferedReader
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(your file), "UTF-8"));
Edit For Comment:
FileReader
uses Java's platform default encoding, which depends on the system settings of the computer on which its running on.
Unfortunately
FileReader
does not allow to set encoding. Instead, you have to use Above code to get the desire encoding for your file.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.