java:IO流(字节流读写中文)

版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/qq_24644517/article/details/83419086

* 字节流读取中文的问题
    * 字节流在读中文的时候有可能会读到半个中文,造成乱码 

FileInputStream fis=new FileInputStream("yyy.txt");
		byte[] arr=new byte[4];
		int len;
		while((len=fis.read(arr))!=-1) {
			System.out.println(new String(arr,0,len));
		}


* 字节流写出中文的问题
    * 字节流直接操作的字节,所以写出中文必须将字符串转换成字节数组 

FileOutputStream fis=new FileOutputStream("yyy.txt");
		fis.write("你好啊".getBytes());
		fis.close();

猜你喜欢

转载自blog.csdn.net/qq_24644517/article/details/83419086