国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

java如何實(shí)現寫(xiě)入并保存txt文件

發(fā)布時(shí)間:2021-09-27 17:50 來(lái)源:億速云 閱讀:0 作者:小新 欄目: 開(kāi)發(fā)技術(shù) 歡迎投稿:712375056

這篇文章給大家分享的是有關(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í)歡迎投稿傳遞力量。

成人午夜A级毛片免费| 又大又粗弄得我出好多水| 国产AV无码专区亚洲AV手机| 中文字幕AV中文字无码亚| CHINESE熟女老女人HD视频| 亚洲永久精品ww47|