這篇文章給大家分享的是有關(guān)java如何實(shí)現(xiàn)寫入并保存txt文件的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
超過十余年行業(yè)經(jīng)驗(yàn),技術(shù)領(lǐng)先,服務(wù)至上的經(jīng)營模式,全靠網(wǎng)絡(luò)和口碑獲得客戶,為自己降低成本,也就是為客戶降低成本。到目前業(yè)務(wù)范圍包括了:成都網(wǎng)站建設(shè)、成都做網(wǎng)站,成都網(wǎng)站推廣,成都網(wǎng)站優(yōu)化,整體網(wǎng)絡(luò)托管,微信小程序開發(fā),微信開發(fā),成都app開發(fā),同時(shí)也可以讓客戶的網(wǎng)站和網(wǎng)絡(luò)營銷和我們一樣獲得訂單和生意!
實(shí)例代碼如下:
package TEST;import java.io.BufferedWriter;import java.io.FileWriter;import java.io.IOException;public class BufferedWriterDemo { public static void main(String[] args) throws IOException { write("E:\\1.txt"); //運(yùn)行主方法 } public static void write(String path) throws IOException { //將寫入轉(zhuǎn)化為流的形式 BufferedWriter bw = new BufferedWriter(new FileWriter(path)); //一次寫一行 String ss = "測試數(shù)據(jù)"; bw.write(ss); bw.newLine(); //換行用 //關(guān)閉流 bw.close(); System.out.println("寫入成功"); } }
java創(chuàng)建txt文件并存入內(nèi)容
import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;public class txtExport {private static String path = "D:/";private static String filenameTemp;public static void main(String[] args) throws IOException {txtExport.creatTxtFile("你好");txtExport.writeTxtFile("你好");}/*** 創(chuàng)建文件** @throws IOException*/public static boolean creatTxtFile(String name) throws IOException {boolean flag = false;filenameTemp = path + name + ".txt";File filename = new File(filenameTemp);if (!filename.exists()) {filename.createNewFile();flag = true;}return flag;}/*** 寫文件** @param newStr* 新內(nèi)容* @throws IOException*/public static boolean writeTxtFile(String newStr) throws IOException {// 先讀取原有文件內(nèi)容,然后進(jìn)行寫入操作boolean flag = false;String filein = newStr + "\r\n";String temp = "";FileInputStream fis = null;InputStreamReader isr = null;BufferedReader br = null;FileOutputStream fos = null;PrintWriter pw = null;try {// 文件路徑File file = new File(filenameTemp);// 將文件讀入輸入流fis = new FileInputStream(file);isr = new InputStreamReader(fis);br = new BufferedReader(isr);StringBuffer buf = new StringBuffer();// 保存該文件原有的內(nèi)容for (int j = 1; (temp = br.readLine()) != null; j++) {buf = buf.append(temp);// System.getProperty("line.separator")// 行與行之間的分隔符 相當(dāng)于“\n”buf = buf.append(System.getProperty("line.separator"));}buf.append(filein);fos = new FileOutputStream(file);pw = new PrintWriter(fos);pw.write(buf.toString().toCharArray());pw.flush();flag = true;} catch (IOException e1) {// TODO 自動(dòng)生成 catch 塊throw e1;} finally {if (pw != null) {pw.close();}if (fos != null) {fos.close();}if (br != null) {br.close();}if (isr != null) {isr.close();}if (fis != null) {fis.close();}}return flag;}}
感謝各位的閱讀!關(guān)于“java如何實(shí)現(xiàn)寫入并保存txt文件”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
當(dāng)前題目:java如何實(shí)現(xiàn)寫入并保存txt文件
網(wǎng)站路徑:http://jinyejixie.com/article32/ggcppc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、網(wǎng)站維護(hù)、做網(wǎng)站、靜態(tài)網(wǎng)站、標(biāo)簽優(yōu)化、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)