使用Scanner读取UTF-8字符 [英] Reading UTF-8 characters using Scanner

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

问题描述

public boolean isValid(String username, String password)  {
        boolean valid = false;
        DataInputStream file = null;

        try{
            Scanner files = new Scanner(new BufferedReader(new FileReader("files/students.txt")));

            while(files.hasNext()){
                System.out.println(files.next());
            }

        }catch(Exception e){
            e.printStackTrace();
        }
        return valid;
    }

为什么我在读一个由UTF-8编写的文件(通过另一个java程序)它显示奇怪的符号后跟其字符串名称?

How come when I am reading a file that has been written by UTF-8(By another java program) it displays with weird symbols followed by its String name?

I wrote it using this

    private static void  addAccount(String username,String password){
        File file = new File(file_name);
        try{
            DataOutputStream dos = new DataOutputStream(new FileOutputStream(file,true));
            dos.writeUTF((username+"::"+password+"\n"));
        }catch(Exception e){

        }
    } 


推荐答案

这是一种简单的方法:

File words = new File(path);
Scanner s = new Scanner(words,"utf-8");

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

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