package bianchengti01;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/*转换流操作文本
要求:
转换流读取文本文件,源文件abc.txt是UTF-8编码
将文本中的所有字符以字符数组形式读出,将字符数组存储到字符串缓存区中
使用缓冲流将缓冲区中的字符串一次性写出到文件 abc22.txt,同样也是UTF-8编码
*/
public class Test01 {
public static void main(String[] args) throws Exception {
InputStreamReader isr = new
InputStreamReader(new FileInputStream("abc.txt"),"utf-8");
int len = 0 ;
StringBuilder builder = new StringBuilder();
char[] buf = new char[1024];
while((len = isr.read(buf))!=-1){
builder.append(new String(buf,0,len));
}
OutputStreamWriter osw = new
OutputStreamWriter(new FileOutputStream("abc22.txt"), "utf-8");
osw.write(builder.toString());
osw.flush();
isr.close();
osw.close();
/* BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream("abc.txt"), "utf-8"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
(new FileOutputStream("abc22.txt")), "utf-8"));
char[] buffer = new char[1024];
int len;
while ((len = br.read(buffer)) != -1) {
System.out.println(new String(buffer, 0, len));
}
bw.close();
br.close();*/
}
}
|
|