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


    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("
    ");
    		out.append("?? UTF-8").append("
    ");
    		out.append("??????? UTF-8").append("
    ");
     
    		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());
    	   } 
    	}	
    }
    

      

  • 相关阅读:
    CSS 基本语法
    Html 块
    Html 表单
    Html 创建表格
    Html 建立超链接
    Html 在网页中使用图像
    CentOS7 重启问题
    Java 多线程——多线程的引入
    c++ 自然排序-window文件排序
    MFC更改图标
  • 原文地址:https://www.cnblogs.com/hephec/p/4609898.html
Copyright © 2020-2023  润新知