gpt4 book ai didi

java - 使用 Scanner 读取 UTF-8 字符

转载 作者:行者123 更新时间:2023-12-04 22:00:03 25 4
gpt4 key购买 nike

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 程序)编写的文件时,它显示的是奇怪的符号,后面跟着它的字符串名称?

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");

关于java - 使用 Scanner 读取 UTF-8 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11963563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com