使用Scanner读取UTF-8字符

use*_*206 2 java io 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;
    }
Run Code Online (Sandbox Code Playgroud)

为什么当我读取由UTF-8(由另一个java程序)编写的文件时,它会显示奇怪的符号,后跟其String名称?

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){

        }
    } 
Run Code Online (Sandbox Code Playgroud)

Jon*_*Rey 7

这是一个简单的方法:

File words = new File(path);
Scanner s = new Scanner(words,"utf-8");
Run Code Online (Sandbox Code Playgroud)