IO流之文件寫(xiě)入
對(duì)于FileWriter,一定要關(guān)閉流,或者flush才能真正的把數(shù)據(jù)寫(xiě)入到文件中,否則文件是空白的
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)是覆蓋寫(xiě)入 //write(int):寫(xiě)入單個(gè)字符 fileWriter.write('C'); //write(char[]):寫(xiě)入指定數(shù)組 fileWriter.write(chars); //write(char[],off,len):寫(xiě)入指定數(shù)組的指定部分 fileWriter.write("晨沐晴學(xué)java".toString(),0,3); //write(string):寫(xiě)入整個(gè)字符串 fileWriter.write("你好丫"); //write(string,off,len):寫(xiě)入字符串的指定部分 fileWriter.write("福建廈門",0,2); } catch (IOException e) { e.printStackTrace(); } finally { //對(duì)于FileWriter,一定要關(guān)閉流,或者flush才能真正的把數(shù)據(jù)寫(xiě)入到文件中 try { fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } System.out.println("程序結(jié)束"); } }
?
?
本文摘自 :https://www.cnblogs.com/