java FileWriter 追加文件及文件改名方式
FileWriter 追加文件及文件改名
我就廢話不多說(shuō)了,大家還是直接看代碼吧~
import java.io.File; import java.io.FileWriter; import java.io.IOException; public class FileWriterUtil { /** * 追加文件:使用FileWriter */ public static void appendMethod(String fileName, String content) { try { //打開(kāi)一個(gè)寫(xiě)文件器,構(gòu)造函數(shù)中的第二個(gè)參數(shù)true表示以追加形式寫(xiě)文件 FileWriter writer = new FileWriter(fileName, true); writer.write(content); writer.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 修改文件名 * @param oldFilePath * @param newFileName */ public static void reNameLogFile(String oldFilePath,String newFileName){ File f=new File(oldFilePath); String c=f.getParent(); // File mm=new File(c + File.pathSeparator + newFileName + "_" + CommonUtil.getCurrTimeForString()); File mm=new File(c + "/" + newFileName + "_" + CommonUtil.getBeforeDateStr()); if(f.renameTo(mm)){ System.out.println("修改文件名成功!"); }else{ System.out.println("修改文件名失敗"); } } public static void main(String[] args) { String fileName = "/Users/qin/Downloads/callLog.txt"; String content = "new append!"; FileWriterUtil.appendMethod(fileName, content); FileWriterUtil.appendMethod(fileName, "append end. \n"); FileWriterUtil.reNameLogFile("/Users/qin/Downloads/callLog.txt","rayda"); } }
Java PrintWriter&FileWriter 寫(xiě)入追加到文件
用PrintWriter寫(xiě)入文件
import java.io.IOException; import java.io.PrintWriter; public class PrintWriteDemo { public static void main(String[] args) throws IOException { PrintWriter out = new PrintWriter("01.txt"); out.print("the quick brown fox"); out.println(" jumps over the lazy dog."); out.write("work is like a capricious lover whose "); out.write("incessant demands are resented but who is missed terribly when she is not there\n"); out.close(); //如果不關(guān)閉文件,文件停留在buffer zone, 不會(huì)寫(xiě)進(jìn)"01.txt"中 } }
FileWriter只能寫(xiě)入文件,無(wú)法往文件中追加內(nèi)容
用FileWriter寫(xiě)入和追加文件
import java.io.IOException; import java.io.FileWriter; public class FileWriterDemo { public static void main(String[] args) throws IOException { FileWriter out = new FileWriter("02.txt"); //constructor中添加true,即FileWriter out = new FileWriter("02.txt", true)就是往02.txt中追加文件了 out.write("work is like a capricious lover whose "); out.write("incessant demands are resented but who is missed terribly when she is not there\n"); out.write(98.7 + "\n"); out.close(); //很重要,一定記得關(guān)閉文件 } }
都別忘記 throws IOException
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
springcloud 中 zuul 修改請(qǐng)求參數(shù)信息的方法
這篇文章主要介紹了springcloud 中 zuul 修改請(qǐng)求參數(shù)信息的方法,需要的朋友可以參考下2018-02-02Jdk11使用HttpClient提交Http2請(qǐng)求的實(shí)現(xiàn)方法
這篇文章主要介紹了Jdk11使用HttpClient提交Http2請(qǐng)求的實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08使用redisTemplate從redis獲取所有數(shù)據(jù)
這篇文章主要介紹了使用redisTemplate從redis獲取所有數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06淺談Java中replace與replaceAll區(qū)別
這篇文章主要介紹了Java中replace與replaceAll區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03SpringBoot預(yù)加載與懶加載實(shí)現(xiàn)方法超詳細(xì)講解
Spring一直被詬病啟動(dòng)時(shí)間慢,可Spring/SpringBoot是輕量級(jí)的框架。因?yàn)楫?dāng)Spring項(xiàng)目越來(lái)越大的時(shí)候,在啟動(dòng)時(shí)加載和初始化Bean就會(huì)變得越來(lái)越慢,很多時(shí)候我們?cè)趩?dòng)時(shí)并不需要加載全部的Bean,在調(diào)用時(shí)再加載就行,那這就需要預(yù)加載與懶加載的功能了2022-11-11Java8函數(shù)式編程應(yīng)用小結(jié)
Java8非常重要的就是引入了函數(shù)式編程的思想,使得這門經(jīng)典的面向?qū)ο笳Z(yǔ)言有了函數(shù)式的編程方式,彌補(bǔ)了很大程度上的不足,函數(shù)式思想在處理復(fù)雜問(wèn)題上有著更為令人稱贊的特性,本文給大家介紹Java8函數(shù)式編程應(yīng)用小結(jié),感興趣的朋友一起看看吧2023-12-12SpringBoot項(xiàng)目依賴和配置最新示例講解
這篇文章主要介紹了SpringBoot項(xiàng)目依賴和配置,這里主要是搭建項(xiàng)目常用到的maven依賴以及搭建項(xiàng)目會(huì)需要用到的一些配置文件,本文通過(guò)示例代碼給大家詳細(xì)講解,需要的朋友可以參考下2022-11-11Struts中使用validate()輸入校驗(yàn)方法詳解
這篇文章主要介紹了Struts中使用validate()輸入校驗(yàn)方法,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-09-09