国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > Java > 正文

java創(chuàng)建txt文件并存入內(nèi)容

2024-07-14 08:42:01
字體:
供稿:網(wǎng)友

本文實(shí)例為大家分享了java創(chuàng)建txt文件并存入內(nèi)容的具體代碼,供大家參考,具體內(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; } }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到JAVA教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 孝感市| 全州县| 内江市| 吉隆县| 诸暨市| 抚顺市| 桐梓县| 桃源县| 兴安县| 河西区| 定襄县| 新竹县| 宁波市| 蒙山县| 加查县| 双鸭山市| 普兰县| 五原县| 铅山县| 桂东县| 永康市| 平定县| 巨野县| 天镇县| 南康市| 昌邑市| 铅山县| 丰原市| 清河县| 通城县| 康马县| 德昌县| 岳池县| 石屏县| 土默特右旗| 武冈市| 茂名市| 大庆市| 台南县| 调兵山市| 都匀市|