Here you can find the source of loadSimpleTextFile(File file, int bufferSize)
Parameter | Description |
---|---|
file | target file |
bufferSize | the buffer size |
Parameter | Description |
---|---|
FileNotFoundException | ,IOException |
public static String loadSimpleTextFile(File file, int bufferSize) throws FileNotFoundException, IOException
//package com.java2s; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Main { public static final int DEFAULT_READ_BUFFER_SIZE = 1024; /**/*w w w .j av a2 s .c o m*/ * get the content of the file * * @param file * target file * @param bufferSize * the buffer size * @return String the file content * @throws FileNotFoundException, * IOException */ public static String loadSimpleTextFile(File file, int bufferSize) throws FileNotFoundException, IOException { StringBuffer strBuffer = new StringBuffer(); // // FileChannel in = new FileInputStream( file ).getChannel(); // // ByteBuffer buffer = ByteBuffer.allocate( bufferSize ); // while( in.read( buffer ) != -1 ) // { // buffer.flip(); // String temp = new String( buffer.array(), "gbk" ); // strBuffer.append( temp ); // buffer.clear(); // } // return strBuffer.toString(); BufferedReader in = new BufferedReader(new FileReader(file), bufferSize); // char[] buffer = new char[ bufferSize ]; String tempStr = in.readLine(); strBuffer.append(tempStr); while ((tempStr = in.readLine()) != null) { strBuffer.append("\n").append(tempStr); } in.close(); return strBuffer.toString(); } /** * get the content of the file with defalut buffer size 1024 * * @param file * target file * @return String the file content * @throws FileNotFoundException, * IOException */ public static String loadSimpleTextFile(File file) throws FileNotFoundException, IOException { return loadSimpleTextFile(file, DEFAULT_READ_BUFFER_SIZE); } }