public class FileInputStreamTest {
<span style="white-space:pre"> </span>public static void main(String[] args) {
<span style="white-space:pre"> </span>// TODO Auto-generated method stub
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>//FileInputStream读文件测试
<span style="white-space:pre"> </span>new FileInputStreamTest().readByByte("d:\\JAVA之路.txt");
<span style="white-space:pre"> </span>new FileInputStreamTest().readByByteArrays(("d:\\JAVA之路.txt"));
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>public void readByByteArrays(String filePath) {
<span style="white-space:pre"> </span>//由于是读取一个字节数组,只有当出现一个中文字符前面一个字节和后一个字节分开读取时才会出现乱码
<span style="white-space:pre"> </span>File file = new File(filePath);
<span style="white-space:pre"> </span>byte[] buf = new byte[1024];//定义一个字节数组用于接收读取的内容提要
<span style="white-space:pre"> </span>int length = 0;//定义一个变量保存读取的字节数
<span style="white-space:pre"> </span>FileInputStream fis = null;
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>fis = new FileInputStream(file);
<span style="white-space:pre"> </span>while((length = fis.read(buf)) != -1){
<span style="white-space:pre"> </span>String str = new String(buf,0,length);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>} catch (FileNotFoundException e) {
<span style="white-space:pre"> </span>// TODO Auto-generated catch block
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>} catch (IOException e) {
<span style="white-space:pre"> </span>// TODO Auto-generated catch block
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>} finally{
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>fis.close();
<span style="white-space:pre"> </span>} catch (IOException e) {
<span style="white-space:pre"> </span>// TODO Auto-generated catch block
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>public void readByByte(String filePath){
<span style="white-space:pre"> </span>//由于是读取一个字节,如果是中文的话会出现乱码,解决办法是读取到字节数组中,但也是有可能会出现乱码的。
<span style="white-space:pre"> </span>File file = new File(filePath);
<span style="white-space:pre"> </span>FileInputStream fis = null;
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>fis = new FileInputStream(file);
<span style="white-space:pre"> </span>/*char c;
<span style="white-space:pre"> </span>while((c = (char)fis.read()) != -1)
<span style="white-space:pre"> </span>System.out.println(c);*/
<span style="white-space:pre"> </span>for(int i = 0; i < file.length(); i++)
<span style="white-space:pre"> </span>System.out.print((char)fis.read());
<span style="white-space:pre"> </span>} catch (FileNotFoundException e) {
<span style="white-space:pre"> </span>// TODO Auto-generated catch block
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>} catch (IOException e) {
<span style="white-space:pre"> </span>// TODO Auto-generated catch block
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>} finally{
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>fis.close();
<span style="white-space:pre"> </span>} catch (IOException e) {
<span style="white-space:pre"> </span>// TODO Auto-generated catch block
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
}
注意,如果是二进制文件则不要输出字符了肯定会乱码的
学习笔记 - FileInputStream读字节文件并打印出来
最新推荐文章于 2023-09-05 21:32:26 发布
本文展示了如何使用Java的FileInputStream类来读取文本文件的字节内容,并通过两种方式打印:一是逐字节读取,二是通过字节数组读取。在读取过程中需要注意中文字符可能导致的乱码问题。
摘要由CSDN通过智能技术生成