Java创建TXT文件并进行读、写、修改操作

  1. import java.io.*;  
  2. /**  
  3.  *   
  4.  * 功能描述:创建TXT文件并进行读、写、修改操作  
  5.  * @version 1.0   
  6.  */  
  7. public class ReadWriteFile {  
  8.     public static BufferedReader bufread;  
  9.     //指定文件路径和名称  
  10.     private static String path = "D:/suncity.txt";  
  11.     private static  File filename = new File(path);  
  12.     private static String readStr ="";  
  13.   
  14.   
  15.   
  16.   
  17.     /**  
  18.      * 创建文本文件.  
  19.      * @throws IOException   
  20.      *   
  21.      */  
  22.     public static void creatTxtFile() throws IOException{  
  23.         if (!filename.exists()) {  
  24.             filename.createNewFile();  
  25.             System.err.println(filename + "已创建!");  
  26.         }  
  27.     }  
  28.       
  29.     /**  
  30.      * 读取文本文件.  
  31.      *   
  32.      */  
  33.     public static String readTxtFile(){  
  34.         String read;  
  35.         FileReader fileread;  
  36.         try {  
  37.             fileread = new FileReader(filename);  
  38.             bufread = new BufferedReader(fileread);  
  39.             try {  
  40.                 while ((read = bufread.readLine()) != null) {  
  41.                     readStr = readStr + read+ "\r\n";  
  42.                 }  
  43.             } catch (IOException e) {  
  44.                 // TODO Auto-generated catch block  
  45.                 e.printStackTrace();  
  46.             }  
  47.         } catch (FileNotFoundException e) {  
  48.             // TODO Auto-generated catch block  
  49.             e.printStackTrace();  
  50.         }  
  51.   
  52.   
  53.         System.out.println("文件内容是:"+ "\r\n" + readStr);  
  54.         return readStr;  
  55.     }  
  56.       
  57.     /**  
  58.      * 写文件.  
  59.      *   
  60.      */  
  61.     public static void writeTxtFile(String newStr) throws IOException{  
  62.         //先读取原有文件内容,然后进行写入操作  
  63.         String filein = newStr + "\r\n" + readStr + "\r\n";  
  64.         RandomAccessFile mm = null;  
  65.         try {  
  66.             mm = new RandomAccessFile(filename, "rw");  
  67.             mm.writeBytes(filein);  
  68.         } catch (IOException e1) {  
  69.             // TODO 自动生成 catch 块  
  70.             e1.printStackTrace();  
  71.         } finally {  
  72.             if (mm != null) {  
  73.                 try {  
  74.                     mm.close();  
  75.                 } catch (IOException e2) {  
  76.                     // TODO 自动生成 catch 块  
  77.                     e2.printStackTrace();  
  78.                 }  
  79.             }  
  80.         }  
  81.     }  
  82.       
  83.     /**  
  84.      * 将文件中指定内容的第一行替换为其它内容.  
  85.      *   
  86.      * @param oldStr  
  87.      *            查找内容  
  88.      * @param replaceStr  
  89.      *            替换内容  
  90.      */  
  91.     public static void replaceTxtByStr(String oldStr,String replaceStr) {  
  92.         String temp = "";  
  93.         try {  
  94.             File file = new File(path);  
  95.             FileInputStream fis = new FileInputStream(file);  
  96.             InputStreamReader isr = new InputStreamReader(fis);  
  97.             BufferedReader br = new BufferedReader(isr);  
  98.             StringBuffer buf = new StringBuffer();  
  99.   
  100.   
  101.             // 保存该行前面的内容  
  102.             for (int j = 1; (temp = br.readLine()) != null  
  103.                     && !temp.equals(oldStr); j++) {  
  104.                 buf = buf.append(temp);  
  105.                 buf = buf.append(System.getProperty("line.separator"));  
  106.             }  
  107.   
  108.   
  109.             // 将内容插入  
  110.             buf = buf.append(replaceStr);  
  111.   
  112.   
  113.             // 保存该行后面的内容  
  114.             while ((temp = br.readLine()) != null) {  
  115.                 buf = buf.append(System.getProperty("line.separator"));  
  116.                 buf = buf.append(temp);  
  117.             }  
  118.   
  119.   
  120.             br.close();  
  121.             FileOutputStream fos = new FileOutputStream(file);  
  122.             PrintWriter pw = new PrintWriter(fos);  
  123.             pw.write(buf.toString().toCharArray());  
  124.             pw.flush();  
  125.             pw.close();  
  126.         } catch (IOException e) {  
  127.             e.printStackTrace();  
  128.         }  
  129.     }  
  130.     /**  
  131.      * main方法测试  
  132.      * @param s  
  133.      * @throws IOException  
  134.      */  
  135.     public static void main(String[] s) throws IOException {  
  136.         ReadWriteFile.creatTxtFile();  
  137.         ReadWriteFile.readTxtFile();  
  138.         ReadWriteFile.writeTxtFile("20130506:12:00");  
  139.         ReadWriteFile.replaceTxtByStr("zehui", "zhou");  
  140.     }  

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值