Java BufferedReader Read loadSimpleTextFile(File file, int bufferSize)

Here you can find the source of loadSimpleTextFile(File file, int bufferSize)

Description

get the content of the file

License

Open Source License

Parameter

Parameter Description
file target file
bufferSize the buffer size

Exception

Parameter Description
FileNotFoundException ,IOException

Return

String the file content

Declaration

public static String loadSimpleTextFile(File file, int bufferSize) throws FileNotFoundException, IOException 

Method Source Code


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

Related

  1. loadQueryNodeInfo(String input_file)
  2. loadReaderFromClasspath(Class c, String filename)
  3. loadReaderToList(Reader reader)
  4. loadRealDataGraphFromEmbers(String fileName, String splitter)
  5. loadSentences(InputStream stream, List jsonSentences)
  6. loadSqlFile(String path)
  7. loadSQLFromFile(String fileName)
  8. loadStream(InputStream is)
  9. loadStreamContent(InputStream stream)