Java File Content Read getFileContent(File file)

Here you can find the source of getFileContent(File file)

Description

get File Content

License

Apache License

Declaration

public static String getFileContent(File file) throws IOException 

Method Source Code

//package com.java2s;
/*//w  ww  . j av  a  2 s.com
 *  Copyright 2008 biaoping.yin
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.  
 */

import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import java.io.Reader;

import java.io.StringWriter;
import java.io.Writer;

public class Main {
    public static String getFileContent(File file) throws IOException {
        Writer swriter = null;
        Reader reader = null;
        try {
            reader = new FileReader(file);
            swriter = new StringWriter();

            int len = 0;
            char[] buffer = new char[1024];
            while ((len = reader.read(buffer)) > 0) {
                swriter.write(buffer, 0, len);
            }
            swriter.flush();

            return swriter.toString();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw e;
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } finally {
            if (reader != null)
                try {
                    reader.close();
                } catch (IOException e) {
                }
            if (swriter != null)
                try {
                    swriter.close();
                } catch (IOException e) {
                }
        }

    }
}

Related

  1. getFileContent(File file)
  2. getFileContent(File file)
  3. getFileContent(File file)
  4. getFileContent(File file)
  5. getFileContent(File file)
  6. getFileContent(File file)
  7. getFileContent(File file)
  8. getFileContent(String filename)
  9. getFileContent(String filename)