IO流之文件寫入
對(duì)于FileWriter,一定要關(guān)閉流,或者flush才能真正的把數(shù)據(jù)寫入到文件中,否則文件是空白的
package com.io.writer; import java.io.FileWriter; import java.io.IOException; /** *FileWriter使用 */ public class FileWriter_ { public static void main(String[] args) { String filePath="d:\note.txt"; //創(chuàng)建FileWriter對(duì)象 FileWriter fileWriter=null; char[] chars={'a','b','c'}; try { fileWriter=new FileWriter(filePath);//默認(rèn)是覆蓋寫入 //write(int):寫入單個(gè)字符 fileWriter.write('C'); //write(char[]):寫入指定數(shù)組 fileWriter.write(chars); //write(char[],off,len):寫入指定數(shù)組的指定部分 fileWriter.write("晨沐晴學(xué)java".toString(),0,3); //write(string):寫入整個(gè)字符串 fileWriter.write("你好丫"); //write(string,off,len):寫入字符串的指定部分 fileWriter.write("福建廈門",0,2); } catch (IOException e) { e.printStackTrace(); } finally { //對(duì)于FileWriter,一定要關(guān)閉流,或者flush才能真正的把數(shù)據(jù)寫入到文件中 try { fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } System.out.println("程序結(jié)束"); } }
?
?
本文摘自 :https://www.cnblogs.com/