Java InputStreamReader Read readFile(File f)

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

Description

read File

License

Apache License

Declaration

public static String readFile(File f) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.BufferedReader;

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static String readFile(File f) {
        BufferedReader reader = null;
        StringBuilder sb = new StringBuilder();
        try {/*ww  w . j  a  va  2s . c  om*/
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(f), "utf-8"));
            String line = null;
            while ((line = reader.readLine()) != null)
                sb.append(line);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return sb.toString();
    }

    public static List<String> readLine(File f) {
        List<String> result = new ArrayList<String>();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(f), "utf-8"));
            String line = null;
            while ((line = reader.readLine()) != null)
                result.add(line);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return result;
    }
}

Related

  1. load(String fileName)
  2. loadAsText(InputStream in, String encoding, int bufferSize)
  3. readFile(Class cl, String filename)
  4. readFile(Class cls, String filename)
  5. readFile(ClassLoader classloader, String filename)
  6. readFile(File f)
  7. readFile(File f)
  8. readFile(File f)
  9. readFile(File f)