IO流 笔记1(仅供自己参考)
输入流
BufferReader br = new BufferedReader();
//构造方法:
public BufferedReader(Reader in) {
this(in, defaultCharBufferSize);
}
从BufferedReader的方法中得知,需要传入一个reader,所以需要new一个InputStreamReader
InputStreamReader isr = new InputStreamReader();
//构造方法:
public InputStreamReader(InputStream in, String charsetName)
throws UnsupportedEncodingException
{
super(in);
if (charsetName == null)
throw new NullPointerException("charsetName");
sd = StreamDecoder.forInputStreamReader(in, this, charsetName);
}
从此构造方法得知,需要new一个InputStrem,可以通过FileInputStream创建
FileInputStream fis = new FileInputStream();
//构造方法:
public FileInputStream(String name) throws FileNotFoundException {
this(name != null ? new File(name) : null);
即输入流需要的前置条件为:
BufferedReader br = null;
InputStreamReader isr = null;
FileInputStream fis = null;
fis = new FileInputStream("D:/iofile.txt");
isr = new InputStreamReader(fis,"UTF8");
br = new BufferedReader(isr);
后面三段代码会报错,需要try/catch
try{
fis = new FileInputStream("D:/iofile.txt");
//""字符串为需要输入读取的路径,
isr = new InputStreamReader(fis,"UTF8");
//"UTF8"表示的是设置编码格式为 "UTF8"
br = new BufferedReader(isr);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
显示出读取到的字符:
String content = null;
while ((content = br.readLine()) != null){
System.out.println(content);
}
输出流
输出流的前置代码跟输入流差不多:
BufferedWriter bw = null;
OutputStreamWriter osw = null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream("D:/io2.txt",true);
//append:true的意思是再执行的操作是在原有的内容后面继续添加. append:false是默认的,默认覆盖掉原有内容
osw = new OutputStreamWriter(fos,"UTF8");
bw = new BufferedWriter(osw);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
输出流得到的值需要转换成StringBuffer类型,通过字符串可以进行很多操作。
StringBuffer sb = new StringBuffer();
String content = null;
while ((content = br.readLine()) != null){
sb.append(content);//讲读取到的内容添加到sb内存中
}
String content1 = sb.toString();
//将sb内存中的内容存入content1字符串中
content1 = content1.replace("hello","not hello");
//将content中的hello替换成 not hello
bw.write(content1);//创建一个新的字符流,将content1传给bw
bw.flush();//强制将缓存区中的数据写入到输出流中
System.out.println(sb);
最后需要将输入输出流都关闭
finally{
try {
br.close();
isr.close();
fis.close();
fos.close();
osw.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
close方法也需要try/catch抛出
完整代码
public static void main(String[] args) {
BufferedReader br = null;
InputStreamReader isr = null;
FileInputStream fis = null;
BufferedWriter bw = null;
OutputStreamWriter osw = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("D:/iofile.txt");
isr = new InputStreamReader(fis,"UTF8");
br = new BufferedReader(isr);
fos = new FileOutputStream("D:/io2.txt",true);
osw = new OutputStreamWriter(fos,"UTF8");
bw = new BufferedWriter(osw);
//以上都能记住了
StringBuffer sb = new StringBuffer();
String content = null;
while ((content = br.readLine()) != null){
// System.out.println(content);
sb.append(content);
}
String content1 = sb.toString();
content1 = content1.replace("hello","not hello");
bw.write(content1);
bw.flush();
System.out.println(sb);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
br.close();
isr.close();
fis.close();
fos.close();
osw.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
执行结果
执行前的读取文件iofile.txt内容为:
执行一次后的输出文件io2.txt
执行5次的io2.txt