import java.io.FileInputStream;
import java.io.IOException;
//由于字体的设置的差异,最终运行结果会出现乱码
public class TestFileIn {
public static void main(String[] args) {
//JAVA会在try块中自动调用close()
try(FileInputStream in = new FileInputStream("D:\\a.txt")) {
int bytesRead = 0;
int bytesToRead = 1024;
byte[] input = new byte[bytesToRead];
while(bytesRead < bytesToRead ) {
//提高效率的读方法
int result = in.read(input, bytesRead, bytesToRead-bytesRead);
if(result == -1) {
break;
}
bytesRead += result;
}
//将字节数组转化为字符串并输出
String str = new String(input);
System.out.println(str);
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
乱码的如何解决待再更