- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- java如何實(shí)現寫(xiě)入并保存txt文件
這篇文章給大家分享的是有關(guān)java如何實(shí)現寫(xiě)入并保存txt文件的內容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
實(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"); //運行主方法 } public static void write(String path) throws IOException { //將寫(xiě)入轉化為流的形式 BufferedWriter bw = new BufferedWriter(new FileWriter(path)); //一次寫(xiě)一行 String ss = "測試數據"; bw.write(ss); bw.newLine(); //換行用 //關(guān)閉流 bw.close(); System.out.println("寫(xiě)入成功"); } }
java創(chuàng )建txt文件并存入內容
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;}/*** 寫(xiě)文件** @param newStr* 新內容* @throws IOException*/public static boolean writeTxtFile(String newStr) throws IOException {// 先讀取原有文件內容,然后進(jìn)行寫(xiě)入操作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();// 保存該文件原有的內容for (int j = 1; (temp = br.readLine()) != null; j++) {buf = buf.append(temp);// System.getProperty("line.separator")// 行與行之間的分隔符 相當于“\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;}}
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站