码迷,mamicode.com
首页 > 编程语言 > 详细

How to write UTF-8 encoded data into a file – Java

时间:2015-06-30 12:33:58      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

Here’s the Java example to demonstrate how to write UTF-8 encoded data into a text file – “c:\\temp\\test.txt”

P.S Symbol “??” is some “UTF-8″ data in Chinese and Japanese

package com.mkyong;
 
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
 
public class test {
	public static void main(String[] args){
 
	  try {
		File fileDir = new File("c:\\temp\\test.txt");
 
		Writer out = new BufferedWriter(new OutputStreamWriter(
			new FileOutputStream(fileDir), "UTF8"));
 
		out.append("Website UTF-8").append("\r\n");
		out.append("?? UTF-8").append("\r\n");
		out.append("??????? UTF-8").append("\r\n");
 
		out.flush();
		out.close();
 
	    } 
	   catch (UnsupportedEncodingException e) 
	   {
		System.out.println(e.getMessage());
	   } 
	   catch (IOException e) 
	   {
		System.out.println(e.getMessage());
	    }
	   catch (Exception e)
	   {
		System.out.println(e.getMessage());
	   } 
	}	
}

  

How to write UTF-8 encoded data into a file – Java

标签:

原文地址:http://www.cnblogs.com/hephec/p/4609898.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!