java實現(xiàn)寫入并保存txt文件的代碼詳解
更新時間:2020年02月05日 11:22:01 作者:V
在本篇文章里小編給大家整理了關(guān)于java實現(xiàn)寫入并保存txt文件的代碼實例內(nèi)容,需要的朋友們可以參考學(xué)習(xí)下。
java如何實現(xiàn)寫入并保存txt文件?
實例代碼如下:
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 { //將寫入轉(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)容,然后進行寫入操作 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 自動生成 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)知識點,希望腳本之家整理的內(nèi)容能夠幫助到大家。
相關(guān)文章
java?數(shù)組越界判斷和獲取數(shù)組長度的實現(xiàn)方式
這篇文章主要介紹了java?數(shù)組越界判斷和獲取數(shù)組長度的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12windows命令行中java和javac、javap使用詳解(java編譯命令)
最近重新復(fù)習(xí)了一下java基礎(chǔ),這里便講講對于一個類文件如何編譯、運行、反編譯的。也讓自己加深一下印象2014-03-03Java反轉(zhuǎn)字符串和相關(guān)字符編碼的問題解決
反轉(zhuǎn)字符串一直被當(dāng)作是簡單問題,大家的思想主要就是利用遍歷,首尾交換字符實現(xiàn)字符串的反轉(zhuǎn)。例如下面的代碼,就可以簡單實現(xiàn)反轉(zhuǎn)。2013-05-05