Java BufferedReader Read readFile(String filename)

Here you can find the source of readFile(String filename)

Description

read File

License

Open Source License

Declaration

public static String readFile(String filename) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright ? 2000, 2013 IBM Corporation and others.
 * 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://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*  w  w w. j  av  a2s. com*/
 * IBM Corporation - initial API and implementation
 *
 *******************************************************************************/

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

public class Main {
    public static String readFile(String filename) {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(filename));

            StringBuffer buffer = new StringBuffer();

            // Print the first line
            String line = reader.readLine();
            if (line != null) {
                buffer.append(line);

                // Print subsequent lines
                while (true) {
                    line = reader.readLine();
                    if (line == null)
                        break;

                    buffer.append("\r\n");
                    buffer.append(line);
                }
            }
            reader.close();

            return buffer.toString();
        } catch (FileNotFoundException e) {
            System.err.println("Testcase not found");
        } catch (IOException e) {
            System.err.println("Error reading testcase file");
        }

        return null;
    }
}

Related

  1. readFile(String fileName)
  2. readFile(String fileName)
  3. readFile(String fileName)
  4. readFile(String fileName)
  5. readFile(String filename)
  6. readFile(String fileName)
  7. readfile(String filename)
  8. readFile(String FileName)
  9. readFile(String fileName)