1 2 3
|
java.lang.Object |----> java.io.Writer |----> java.io.BufferedWriter
|
BufferedWriter(Writer out)
|
创建一个缓冲字符输出流,使用默认大小的输出缓冲区
|
BufferedWriter(Writer out, int sz)
|
创建一个缓冲字符输出流,使用给定大小的输出缓冲区
|
void write(int c)
|
写入单个字符。
|
void write(char[] cbuf, int off, int len)
|
写入字符数组的某一部分。
|
void write(String s, int off, int len)
|
写入字符串的某一部分。
|
void newLine()
|
写入一个行分隔符。
|
void close()
|
关闭此流,但要先刷新它。
|
void flush()
|
刷新该流的缓冲。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
public static void main(String[] args) throws IOException { BufferedWriter writer=new BufferedWriter(new FileWriter("静夜思.txt")); char ch='床'; writer.write(ch); String next="前明月光,"; char[] nexts=next.toCharArray(); writer.write(nexts,0,nexts.length); writer.newLine(); String nextLine="疑是地上霜。"; writer.write(nextLine); writer.close(); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
static void copyByChar(String srcFile, String destFile) throws IOException { BufferedReader reader = new BufferedReader(new FileReader(srcFile)); BufferedWriter writer = new BufferedWriter(new FileWriter(destFile)); int ch=0; while ((ch = reader.read()) != -1) { writer.write(ch); } reader.close(); writer.close(); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
static void copyByCharArray(String srcFile, String destFile) throws IOException { BufferedReader reader = new BufferedReader(new FileReader(srcFile)); BufferedWriter writer = new BufferedWriter(new FileWriter(destFile)); int size=0; char[] cbuf=new char[20]; while ((size = reader.read(cbuf)) != -1) { writer.write(cbuf,0,size); } reader.close(); writer.close(); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
static void copyByLine(String srcFile,String destFile) throws IOException { BufferedReader reader=new BufferedReader(new FileReader(srcFile)); BufferedWriter writer=new BufferedWriter(new FileWriter(destFile)); String line; while((line=reader.readLine())!=null) { writer.write(line); writer.newLine(); } reader.close(); writer.close(); }
|
1 2 3 4 5 6 7 8 9 10
|
public static void main(String[] args) throws IOException { String from = "gbk.txt"; String to = "gbk_copy.txt"; String to1 = "gbk_copy1.txt"; String to2 = "gbk_copy2.txt"; copyByChar(from, to); copyByCharArray(from, to1); copyByLine(from, to2); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| static void copyByLineEncoding(String srcFile, String srcEncoding, String destFile, String destEncoding) { BufferedReader reader = null; BufferedWriter writer = null; try { reader = new BufferedReader(new InputStreamReader( new FileInputStream(srcFile), srcEncoding)); writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(destFile), destEncoding)); char[] charArray = new char[512]; int size; while ((size = reader.read(charArray, 0, charArray.length)) != -1) { writer.write(charArray, 0, size); }
} catch (UnsupportedEncodingException | FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (writer != null) { try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } }
|