输入流(byte)

package com.cyj.Byte;

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

public class Input {

	public static void main(String[] args) {
		
		//建立联系 File对象
		File src = new File("C:\\Users\\Chyjrily\\Documents\\Tencent Files\\2323010676\\FileRecv\\中国\\江苏省.txt");
		
		//选择输入输出流
		InputStream is = null; //提升作用域
		try {
			is = new FileInputStream(src);//与外界进行操作,即会有异常
			
			//读取操作,缓冲数组
			byte[] bt = new byte[1024]; 
			int len = 0;//接收实际读取大小
			
			//循环读取
			try {
				while(-1!=(len = is.read(bt))) {//当没有读取到内容时,就返回-1
					
					//输出将字节数组转成字符串
					String info = new String(bt,0,len);
					System.out.println(info);
					
				}
			} catch (IOException e) {
				e.printStackTrace();
				System.out.println("读取文件失败");
			}
							
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.out.println("文件不存在");
		}finally {
			//释放资源
			if(null != is) {
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					System.out.println("关闭文件输入流失败");
				}
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_42036616/article/details/80952725