<>字符流写数据的五种方式
<>1.构造方法
OutputStreamWriter(OutputStream out):创建一个使用默认字符集编码的OutputStreamWriter
<>2.写数据的五种方式
* void write(int c):写一个字符 package com.characterstream; import java.io.
FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter;
public class OutputStreamWriterDemo { public static void main(String[] args)
throws IOException { OutputStreamWriter osw = new OutputStreamWriter(new
FileOutputStream("基础语法\\osw.txt")); //void write(int c):写入一个字符 osw.write(97);
//void flush():刷新流 osw.flush(); //关闭流,先刷新再关闭 osw.close(); } }
* void write(char[] cbuf):写入一个字符数组 package com.characterstream; import java.io
.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter
; public class OutputStreamWriterDemo { public static void main(String[] args)
throws IOException { OutputStreamWriter osw = new OutputStreamWriter(new
FileOutputStream("基础语法\\osw.txt")); //void write(char[] cbuf):写入一个字符数组 char[]
chs= {'a', 'b', 'c', 'd', 'e'}; osw.write(chs); osw.flush(); osw.close(); } }
* void write(char[] cbuf,int off,int len):写入字符数组一部分 package com.
characterstream; import java.io.FileOutputStream; import java.io.IOException;
import java.io.OutputStreamWriter; public class OutputStreamWriterDemo { public
static void main(String[] args) throws IOException { OutputStreamWriter osw =
new OutputStreamWriter(new FileOutputStream("基础语法\\osw.txt")); char[] chs = {'a'
, 'b', 'c', 'd', 'e'}; // void write(char[] cbuf, int off, int len):写入字符数组的一部分
//osw.write(chs,0, chs.length); osw.write(chs,1,3); osww.flush(); osw.close(); }
}
* void write(String str):写一个字符串 package com.characterstream; import java.io.
FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter;
public class OutputStreamWriterDemo { public static void main(String[] args)
throws IOException { OutputStreamWriter osw = new OutputStreamWriter(new
FileOutputStream("基础语法\\osw.txt")); //void write(String str):写一个字符串 osw.write(
"abcde"); osw.flush(); osw.close(); } }
* void write(String str,int off,int len):写一个字符串的一部分 package com.
characterstream; import java.io.FileOutputStream; import java.io.IOException;
import java.io.OutputStreamWriter; public class OutputStreamWriterDemo { public
static void main(String[] args) throws IOException { OutputStreamWriter osw =
new OutputStreamWriter(new FileOutputStream("基础语法\\osw.txt")); //void
write(String str, int off, int len):写一个字符串的一部分
//osw.write("abcde",0,"abcde".length()); osw.write("abcde",1,3); osw.close(); }
}