使用java prog读取SchedLgU.Txt文件,通过框获取junvalue输出
我编写了以下代码来读取计划任务的系统生成文件,即SchedLgU.Txt使用java prog读取SchedLgU.Txt文件,通过框获取junvalue输出,java,Java,我编写了以下代码来读取计划任务的系统生成文件,即SchedLgU.Txt File f = new File("C:\\WINDOWS\\Tasks\\SchedLgU.Txt"); FileInputStream fstream = new FileInputStream(f); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br =
File f = new File("C:\\WINDOWS\\Tasks\\SchedLgU.Txt");
FileInputStream fstream = new FileInputStream(f);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line="";
do{
line = br.readLine();
System.out.println(line);
} while(br.readLine()!=null);
我得到一些垃圾价值与字符框
我需要定义不同的格式吗?好吧,您使用的是
InputStreamReader
,没有指定字符编码,因此它将始终使用平台默认编码。看起来这个文件是UTF-16格式的(至少在我的盒子上)。只需将该编码传递给InputStreamReader
构造函数即可正确读取文件
试着用你的测试程序,这似乎是可行的。请注意,如果要将其包装在InputStreamReader
中,则使用DataInputStream
没有意义-只需将FileInputStream
直接传递给InputStreamReader
构造函数即可:
BufferedReader br = new BufferedReader(new InputStreamReader(fstream, "UTF-16"));