java 使用IO流读取指定文件中的内容

一、使用字节流读取

我们先使用字节流一个一个读取

package com.uwo9.test01;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class Test08 {

	public static void main(String[] args) {
		// 1.定义目标文件
		File srcFile = new File("E:/Temp/Test1.txt");
		// 2.创建一个流,指向目标文件
		InputStream is = null;
		try {
			is = new FileInputStream(srcFile);
			// 3.循环往外流
			int content = is.read();
			// 4.循环打印
			while (content != -1) {
				System.out.print((char) content);
				content = is.read();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭io流
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
我们会发现中文无法识别,汉字占两个字符。

解决方法:按字节数组读取


package com.uwo9.test01;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class Test08 {

	public static void main(String[] args) {
		// 1.定义目标文件
		File srcFile = new File("E:/Temp/Test1.txt");
		// 2.创建一个流,指向目标文件
		InputStream is = null;
		try {
			is = new FileInputStream(srcFile);
			//3.创建一个用来存储读取数据的缓冲数组
			byte[]array = new byte[128];
			//4.循环往外流(count为每次读取数组中的有效字节总数)
			int count = is.read(array);
			// 5.循环打印
			while (count != -1) {
				// 将byte[] -》 String
				// 将byte数组读取到的有效字节转换成字符串
				String string = new String(array, 0, count);
				System.out.print(string);
				count = is.read(array);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭io流
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}

可以看到,中文成功读取。但按照字节数组读取,还是有可能出现部分中文乱码问题,我们可以用字符流


二、使用字符流读取


package com.uwo9.test01;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class Test08 {

	public static void main(String[] args) {
		// 1.创建文件对象
		File fromFile = new File("E:/Temp/Test1.txt");
		// 2.创建字符输入流
		Reader reader = null;
		try {
			reader = new FileReader(fromFile);
			// 3.循环读取(打印)
			int content = reader.read();
			while (content != -1) {
				System.out.print((char) content);
				content = reader.read();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			// 4.关闭流
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		

	}

}


补:

字节流是 8 位通用字节流,字符流是 16 位 Unicode 字符流

获得当前开发环境的字符编码方式

System.out.println(System.getProperty("file.encoding"));


  • 1
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值