字节流转字符流

IO中字节流转字符流

public class Demo1 {

	public static void main(String[] args) {
		InputStream is=null;
		InputStreamReader isr=null;
		try {
			File file=new File("d:\\First1.txt");
			
			//实例化字节输入流
			is=new FileInputStream(file);
			
			//将字节输入流转化为字符输入流,可以设置格式编码
//	    InputStreamReader isr=new InputStreamReader(is,"UTF-8");
			 isr=new InputStreamReader(is);
			
			//以上三步简写
//	    InputStreamReader isr=new InputStreamReader(new FileInputStream(new File("d:\\First.txt")));
   
			int a;
			while ((a=isr.read())!=-1) {
				System.out.println((char)a);
			}
        //抛出异常
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			//
			try {
                //资源释放
				isr.close();
				is.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}

猜你喜欢

转载自blog.csdn.net/qwq1518346864/article/details/115383300