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();
|
}
|
}
|