DELL
2025-03-03 2cdf5ae1cc004e7c36375874d28eacdc191f2de6
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
package com.app.reg;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
 
public class FileUtils {
    /**
     * ½«Îı¾ÎļþÖеÄÄÚÈݶÁÈëµ½bufferÖÐ
     * @param buffer buffer
     * @param filePath Îļþ·¾¶
     * @throws IOException Òì³£
     * @author cn.outofmemory
     * @date 2013-1-7
     */
    public static void readToBuffer(StringBuffer buffer, String filePath) throws IOException {
        InputStream is = new FileInputStream(filePath);
        String line; // ÓÃÀ´±£´æÃ¿ÐжÁÈ¡µÄÄÚÈÝ
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        line = reader.readLine(); // ¶ÁÈ¡µÚÒ»ÐÐ
        while (line != null) { // Èç¹û line Îª¿Õ˵Ã÷¶ÁÍêÁË
            buffer.append(line); // ½«¶Áµ½µÄÄÚÈÝÌí¼Óµ½ buffer ÖÐ
            buffer.append("\n"); // Ìí¼Ó»»Ðзû
            line = reader.readLine(); // ¶ÁÈ¡ÏÂÒ»ÐÐ
        }
        reader.close();
        is.close();
    }
 
    /**
     * ¶ÁÈ¡Îı¾ÎļþÄÚÈÝ
     * @param filePath ÎļþËùÔÚ·¾¶
     * @return Îı¾ÄÚÈÝ
     * @throws IOException Òì³£
     * @author cn.outofmemory
     * @date 2013-1-7
     */
    public static String readFile(String filePath) throws IOException {
        StringBuffer sb = new StringBuffer();
        FileUtils.readToBuffer(sb, filePath);
        return sb.toString();
    }
}