package com.bjsxt.init;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
public class mFileTest05 {
public static void main(String[] args) {
File newFile = new File("e:\\test\\myText.txt");
try {
FileOutputStream fos = new FileOutputStream(newFile);//用字节流写入文件
OutputStreamWriter osw = new OutputStreamWriter(fos);//字符流字节流转换
BufferedWriter bw = new BufferedWriter(osw);//缓存流执行效率更快
//fos.write("你好啊c");
//fos.write(97);
int ch = 20320;//你
// fos.write(ch);
char cbuf[] =new char[1];
cbuf[0]=(char)ch;
osw.write(cbuf);
osw.write("张志勤");
osw.write("c");
osw.flush();
// Long a=System.currentTimeMillis() ;
// for(int i=0;i<10000;i++){
// bw.write("窗前明月光\r\n");
// bw.write("疑是地上霜\r\n");
//// osw.write("窗前明月光\r\n");
//// osw.write("疑是地上霜\r\n");
// }
//
// Long a1=System.currentTimeMillis() ;
// Long a2=a1-a;
// System.out.println("用了多少秒:"+a2);
// osw.flush();
bw.close();
osw.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
File file = new File("e:\\test\\myText.txt");
try {
FileInputStream fis = new FileInputStream(file);//读一个字节
InputStreamReader isr = new InputStreamReader(fis);//是字节流与字符流之间的桥梁,能将字节流输出为字符流,
BufferedReader br = new BufferedReader(isr);
// int a;
// while((a = fis.read()) != -1){
// System.out.print((char)a);
// }
// int c;
// while((c = isr.read()) != -1){
// System.out.println((char)c);
// }
String line;
while((line = br.readLine()) != null){
System.out.println(line);
}
br.close();
isr.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
原文地址:https://www.cnblogs.com/zhangzhiqin/p/9096782.html