使用java读取utf-8编码的文本文件 [英] reading text file with utf-8 encoding using java

查看:51
本文介绍了使用java读取utf-8编码的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读取 utf-8 编码的文本文件时遇到问题我在 netbeans 7.2.1 平台上使用 java

I have problem in reading text file with utf-8 encoding I'm using java with netbeans 7.2.1 platform

我已经配置了java项目来处理UTF-8javaproject==>右键==>属性==>源码==>UTF-8

I already configured the java project to handle UTF-8 javaproject==>right click==>properties==>source==>UTF-8

但仍然得到未知字符输出:

but still get the unknown character output: ����� �������� ���� �

代码:

File fileDirs = new File("C:\file.txt");

BufferedReader in = new BufferedReader(
new InputStreamReader(new FileInputStream(fileDirs), "UTF-8"));

String str;

while ((str = in.readLine()) != null) {
    System.out.println(str);
}

还有其他想法吗?

谢谢

推荐答案

使用

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.UnsupportedEncodingException;     
    public class test {
    public static void main(String[] args){

    try {
        File fileDir = new File("PATH_TO_FILE");

        BufferedReader in = new BufferedReader(
           new InputStreamReader(new FileInputStream(fileDir), "UTF-8"));

        String str;

        while ((str = in.readLine()) != null) {
            System.out.println(str);
        }

                in.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());
        }
    }
}

您需要将 UTF-8 放在引号中

You need to put UTF-8 in quotes

这篇关于使用java读取utf-8编码的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆