java創(chuàng)建文件和相關(guān)操作

*? * 功能描述:創(chuàng)建TXT文件并進(jìn)行讀、寫、修改操作 *? ? ? * @authorKenZhang* @version 1.0? * Creation date: 2007-12-18 - 下午06:48:45 */

public class ReadWriteFile {??

? public static BufferedReader bufread;? ? //指定文件路徑和名稱? ? private static String path = "D:/suncity.txt";? ? private static? File filename = new File(path);? ? private static String readStr ="";? ? /**? ? * 創(chuàng)建文本文件.? ? * @throws IOException? ? ? *? ? ? */? ? public static void creatTxtFile() throws IOException{? ? ? ? if (!filename.exists()) {? ? ? ? ? ? filename.createNewFile();? ? ? ? ? ? System.err.println(filename + "已創(chuàng)建!");? ? ? ? }? ? }? ?

?? ? /**? ? * 讀取文本文件.? ? *? ? ? */??

? public static String readTxtFile(){? ? ? ? String read;? ? ? ? FileReader fileread;? ? ? ? try {? ? ? ? ? ? fileread = new FileReader(filename);? ? ? ? ? ? bufread = new BufferedReader(fileread);? ? ? ? ? ? try {? ? ? ? ? ? ? ? while ((read = bufread.readLine()) != null) {? ? ? ? ? ? ? ? ? ? readStr = readStr + read+ "\r\n";? ? ? ? ? ? ? ? }? ? ? ? ? ? } catch (IOException e) {? ? ? ? ? ? ? ? // TODO Auto-generated catch block? ? ? ? ? ? ? ? e.printStackTrace();? ? ? ? ? ? }? ? ? ? } catch (FileNotFoundException e) {? ? ? ? ? ? // TODO Auto-generated catch block? ? ? ? ? ? e.printStackTrace();? ? ? ? }? ? ? ? System.out.println("文件內(nèi)容是:"+ "\r\n" + readStr);? ? ? ? return readStr;? ? }? ? ?

?? /**? ? * 寫文件.? ? *? ? ? */? ? public static void writeTxtFile(String newStr) throws IOException{? ? ? ?

?//先讀取原有文件內(nèi)容,然后進(jìn)行寫入操作? ? ?

?? String filein = newStr + "\r\n" + readStr + "\r\n";? ? ? ? RandomAccessFile mm = null;? ? ? ? try {? ? ? ? ? ? mm = new RandomAccessFile(filename, "rw");? ? ? ? ? ? mm.writeBytes(filein);? ? ? ? } catch (IOException e1) {? ? ? ? ? ? // TODO 自動生成 catch 塊? ? ? ? ? ? e1.printStackTrace();? ? ? ? } finally {? ? ? ? ? ? if (mm != null) {? ? ? ? ? ? ? ? try {? ? ? ? ? ? ? ? ? ? mm.close();? ? ? ? ? ? ? ? } catch (IOException e2) {? ? ? ? ? ? ? ? ? ? // TODO 自動生成 catch 塊? ? ? ? ? ? ? ? ? ? e2.printStackTrace();? ? ? ? ? ? ? ? }? ? ? ? ? ? }? ? ? ? }? ? }? ? ??

? /**? ? * 將文件中指定內(nèi)容的第一行替換為其它內(nèi)容.? ? *?

?? ? * @param oldStr? ? *? ? ? ? ? ? 查找內(nèi)容? ? *

?@param replaceStr? ? *? ? ? ? ? ? 替換內(nèi)容? ? */?

?? public static void replaceTxtByStr(String oldStr,String replaceStr) {? ? ? ? String temp = "";? ? ? ? try {? ? ? ? ? ? File file = new File(path);? ? ? ? ? ? FileInputStream fis = new FileInputStream(file);? ? ? ? ? ? InputStreamReader isr = new InputStreamReader(fis);? ? ? ? ? ? BufferedReader br = new BufferedReader(isr);? ? ? ? ? ? StringBuffer buf = new StringBuffer();? ? ? ??

? ? // 保存該行前面的內(nèi)容? ? ? ? ?

?? for (int j = 1; (temp = br.readLine()) != null? ? ? ? ? ? ? ? ? ? && !temp.equals(oldStr); j++) {? ? ? ? ? ? ? ? buf = buf.append(temp);? ? ? ? ? ? ? ? buf = buf.append(System.getProperty("line.separator"));? ? ? ? ? ? }? ?

?? ? ? ? // 將內(nèi)容插入? ? ? ? ? ? buf = buf.append(replaceStr);? ??

? ? ? ? // 保存該行后面的內(nèi)容? ? ? ? ? ? while ((temp = br.readLine()) != null) {? ? ? ? ? ? ? ? buf = buf.append(System.getProperty("line.separator"));? ? ? ? ? ? ? ? buf = buf.append(temp);? ? ? ? ? ? }? ? ? ? ? ? br.close();? ? ? ? ? ? FileOutputStream fos = new FileOutputStream(file);? ? ? ? ? ? PrintWriter pw = new PrintWriter(fos);? ? ? ? ? ? pw.write(buf.toString().toCharArray());? ? ? ? ? ? pw.flush();? ? ? ? ? ? pw.close();? ? ? ? } catch (IOException e) {? ? ? ? ? ? e.printStackTrace();? ? ? ? }? ? }?

?? /**? ? * main方法測試? ? * @param s? ? * @throws IOException? ? */? ? public static void main(String[] s) throws IOException {? ? ? ? ReadWriteFile.creatTxtFile();? ? ? ? ReadWriteFile.readTxtFile();? ? ? ? ReadWriteFile.writeTxtFile("20080808:12:13");//? ? ? ? ReadWriteFile.replaceTxtByStr("ken", "zhang");? ? }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、流的概念和作用。 流是一種有順序的,有起點(diǎn)和終點(diǎn)的字節(jié)集合,是對數(shù)據(jù)傳輸?shù)目偝苫虺橄?。即?shù)據(jù)在兩設(shè)備之間的傳輸...
    布魯斯不吐絲閱讀 10,310評論 2 95
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,637評論 18 399
  • 一、 1、請用Java寫一個冒泡排序方法 【參考答案】 public static void Bubble(int...
    獨(dú)云閱讀 1,494評論 0 6
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 7,311評論 0 17
  • 點(diǎn)擊關(guān)注堅持看,健康其實(shí)很簡單,不打針不吃藥,學(xué)習(xí)穴位百病消。 關(guān)注《一代宗師》訂閱號,就能快樂輕松學(xué)穴位 【甲】...
    金牌育兒課閱讀 1,710評論 0 0

友情鏈接更多精彩內(nèi)容