Java InputStreamReader Read readFile(File file)

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

Description

Reads given file and returns file contents

License

Open Source License

Parameter

Parameter Description
file input file

Return

file contents as string

Declaration


public static String readFile(File file) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2006 DBE./*from   ww w  . jav a2  s. c  o m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://opensource.org/licenses/eclipse-1.0.php
 *
 * Contributors:
 *     Isufi
 *
 * Authors:
 *   Maurizio De Tommasi
 *   Pierpaolo Cira
 *   Valerio Cisternino
 *
 *******************************************************************************/

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class Main {
    /**
     * Reads given file and returns file contents
     * @param file input file
     * @return file contents as string 
     */

    public static String readFile(File file) {
        String fileStr = "";
        String strLineCurrent = "";
        try {
            // Open the file that is the first 
            // command line parameter
            FileInputStream fstream = new FileInputStream(file);
            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));

            //Read File Line By Line
            while ((strLineCurrent = br.readLine()) != null) {
                // Print the content on the console
                fileStr = fileStr + strLineCurrent + "\n";
            }
            //Close the input stream
            in.close();
        } catch (Exception e) {//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
        return fileStr;
    }
}

Related

  1. readFile(File file)
  2. readFile(File file)
  3. readFile(File file)
  4. readFile(File file)
  5. readFile(File file)
  6. readFile(File file)
  7. readFile(File file)
  8. readFile(File file)
  9. readFile(File file, String default_encoding)