package com.heima.test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo1_Test {
/**
* 字节流读取中文的问题
* 字节流在读中文的时候有可能会读到半个中文,造成乱码
* 字节流写出中文的问题
* 字节流直接操作的字节,所以写出中文必须将字符串转换成字节数组
* 写出回车换行 write("\r\n".getBytes());
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//demo1();
FileOutputStream fos = new FileOutputStream("jjj.txt");
fos.write("流年,等待谁的旧时光".getBytes());
fos.write("\r\n".getBytes());
}
/**
* @throws FileNotFoundException
* @throws IOException
*/
private static void demo1() throws IOException {
FileInputStream fis = new FileInputStream("yyy.txt");
int len;
byte[] arr = new byte[2];
while((len = fis.read(arr)) != -1) {
System.out.print(new String(arr,0,len));
}
fis.close();
}
}
|
|