s-day09-字符输出流写数据
This commit is contained in:
@@ -0,0 +1,58 @@
|
|||||||
|
package com.inmind.io_writer04;
|
||||||
|
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/*
|
||||||
|
字符输出流写数据
|
||||||
|
在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 WriterDemo11 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
/*
|
||||||
|
1.创建对象
|
||||||
|
字符输出流,如果文件不存在则主动创建
|
||||||
|
*/
|
||||||
|
FileWriter fw = new FileWriter("source2.txt");
|
||||||
|
//2.调用写方法
|
||||||
|
//void write(int c) 写一个字符
|
||||||
|
// fw.write(97);
|
||||||
|
fw.write('a');//char --->int
|
||||||
|
char[] chars = {'b','c','d'};
|
||||||
|
//void write(char[] cbuf) 写入一个字符数组。
|
||||||
|
fw.write(chars);
|
||||||
|
//void write(String str) 写一个字符串
|
||||||
|
fw.write("我爱中国");
|
||||||
|
//void write(String str, int off, int len) 写一个字符串的一部分。
|
||||||
|
fw.write("中国很美好",2,3);
|
||||||
|
|
||||||
|
//3.刷新
|
||||||
|
//4.释放资源
|
||||||
|
fw.close();
|
||||||
|
System.out.println("程序结束");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user