s-day09--字符输出流写数据
This commit is contained in:
@@ -0,0 +1,52 @@
|
|||||||
|
package com.inmind.writer04;
|
||||||
|
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/*
|
||||||
|
14.字符输出流写数据
|
||||||
|
在java中使用一个抽象的类Writer表示字符输出流
|
||||||
|
常用子类:FileWriter
|
||||||
|
|
||||||
|
构造方法:
|
||||||
|
FileWriter(File file) 给一个File对象构造一个FileWriter对象。
|
||||||
|
FileWriter(String fileName) 构造一个给定文件名的FileWriter对象。
|
||||||
|
|
||||||
|
FileWriter(File file, boolean append) 给一个File对象构造一个FileWriter对象。
|
||||||
|
FileWriter(String fileName, boolean append) 构造一个FileWriter对象,给出一个带有布尔值的文件名,表示是否附加写入的数据。
|
||||||
|
|
||||||
|
|
||||||
|
常用方法:
|
||||||
|
abstract void close() 关闭资源
|
||||||
|
|
||||||
|
void write(char[] cbuf) 写入一个字符数组。
|
||||||
|
abstract void write(char[] cbuf, int off, int len) 写入字符数组的一部分。
|
||||||
|
void write(int c) 写一个字符
|
||||||
|
void write(String str) 写一个字符串
|
||||||
|
void write(String str, int off, int len) 写一个字符串的一部分。
|
||||||
|
|
||||||
|
abstract void flush() 刷新流。
|
||||||
|
使用步骤:
|
||||||
|
1.创建对象
|
||||||
|
2.调用写方法
|
||||||
|
3.刷新
|
||||||
|
4.释放资源
|
||||||
|
*/
|
||||||
|
public class Demo12 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
//1.创建对象
|
||||||
|
FileWriter fw = new FileWriter("target.txt");
|
||||||
|
// FileWriter fw = new FileWriter("target.txt",true);
|
||||||
|
//2.调用写方法
|
||||||
|
// fw.write('国');
|
||||||
|
/*char[] chars = {'我', '爱', '中', '国'};
|
||||||
|
fw.write(chars);*/
|
||||||
|
fw.write("我是谁我在哪我在干嘛");
|
||||||
|
//3.刷新
|
||||||
|
fw.flush();//将要写出的字符数据从缓冲区,真的写出到文件
|
||||||
|
//4.释放资源
|
||||||
|
fw.close();
|
||||||
|
|
||||||
|
System.out.println("程序结束");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user