|
对于一个大约1K的文本文件,需要频繁的读取它的全部内容。请问:下面两个函数哪个更适合?或者哪位高手给出个更好的函数,谢谢了。
- public static String getContents(String fileName){
- StringBuffer sb = new StringBuffer();
- try{
- BufferedReader in = new BufferedReader(new FileReader(fileName));
- String s;
- while((s = in.readLine())!=null){
- sb.append(s+"\n");
- }
- in.close();
- }catch(Exception e){}
- return sb.toString();
- }
- public static String getContents2(String fileName){
- String contents = "";
- try{
- RandomAccessFile rf = new RandomAccessFile(fileName,"r");
- byte[] text = new byte[(int)(rf.length())];
- rf.readFully(text);
- contents = new String(text);
- rf.close();
- }catch(Exception e){}
- return contents;
- }
复制代码 |
|