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