java读取文件的方法

不知能否像程序中那样测试方法执行时间,个人感觉Read方法比ReadFile方法好。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;


public class BufferedReaderTest {
	
	public static String read(String flieName) throws Exception
	{
		BufferedReader in = new BufferedReader(new FileReader(flieName));
		String s;
		StringBuilder sb = new StringBuilder();
		while((s=in.readLine())!=null){
			sb.append(s+"\n");
		}
		in.close();
		return sb.toString();
	}
	
	public static String readFile(String fileName) throws Exception
	{
		File file = new File(fileName);
		FileReader in = new FileReader(file);
		char[] ch = new char[(int)file.length()];
		in.read(ch);
		in.close();
		String s = String.copyValueOf(ch);
		return s;
		
	}
	
	public static void main(String[] args) throws Exception
	{
		long startTime = System.currentTimeMillis();
//		System.out.println(readFile("D:\\workspace\\test1111\\src\\BufferedReaderTest.java"));
		System.out.println(read("D:\\workspace\\test1111\\src\\BufferedReaderTest.java"));
		long endTime = System.currentTimeMillis();
		System.out.println("程序运行时间:" +(endTime - startTime) + "ms");
	
	}
}

你可能感兴趣的:(java)