int ch;
File fp=new File("C:\\Users\\jiang\\Desktop\\shiti.txt");
try{
FileInputStream fin=new FileInputStream(fp);
System.out.println("xinxiwei");
ch=fin.read();
while(ch!=-1)
{
System.out.println((char)ch);
ch=fin.read();
}
fin.close();
}
catch(Exception e)
{
System.out.println(e);
}
******************************************************************************************************
File file = new File("C:\\Users\\jiang\\Desktop\\shiti.txt");
InputStream in = null;
try {
System.out.println("以字节为单位读取文件内容,一次读一个字节:");
// 一次读一个字节
in = new FileInputStream(file);
int tempbyte;
while ((tempbyte = in.read()) != -1) {
System.out.write(tempbyte);
}
in.close();
}
catch (IOException e) {
e.printStackTrace();
return;
}
这两段代码都是错文件读取数据,内容是 数字和汉字、为什么第一段程序数字可以读出,但是汉字全是问号。第二段程序则正确读出汉字和数字、很是纠结,没有看出哪里有不同、求高人指点!~O(∩_∩)O谢谢
File fp=new File("C:\\Users\\jiang\\Desktop\\shiti.txt");
try{
FileInputStream fin=new FileInputStream(fp);
System.out.println("xinxiwei");
ch=fin.read();
while(ch!=-1)
{
System.out.println((char)ch);
ch=fin.read();
}
fin.close();
}
catch(Exception e)
{
System.out.println(e);
}
******************************************************************************************************
File file = new File("C:\\Users\\jiang\\Desktop\\shiti.txt");
InputStream in = null;
try {
System.out.println("以字节为单位读取文件内容,一次读一个字节:");
// 一次读一个字节
in = new FileInputStream(file);
int tempbyte;
while ((tempbyte = in.read()) != -1) {
System.out.write(tempbyte);
}
in.close();
}
catch (IOException e) {
e.printStackTrace();
return;
}
这两段代码都是错文件读取数据,内容是 数字和汉字、为什么第一段程序数字可以读出,但是汉字全是问号。第二段程序则正确读出汉字和数字、很是纠结,没有看出哪里有不同、求高人指点!~O(∩_∩)O谢谢
第一段程序是打印字节,把汉字给拆开了。
System.out.write(tempbyte);
之前加一句
System.out.println(tempbyte);
试试还可以打印出中文吗?
使用bufferedInputReader(new InputStreamReader(new FileInputStream(File)))(可能拼写错误)