Here you can find the source of readFile(File file)
Parameter | Description |
---|---|
file | input file |
public static String readFile(File file)
//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; } }