Java InputStreamReader Read readFile(String filePath)

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

Description

get object list from file .

License

Open Source License

Parameter

Parameter Description
filePath a parameter

Declaration

public static Object[] readFile(String filePath) 

Method Source Code


//package com.java2s;
import java.io.BufferedReader;

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

import java.io.IOException;
import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**/*from w  w w .j  a  v  a  2 s .  c  o m*/
     * read the file content and convert to Object list.
     * 
     * @param filePath
     * @param object
     * @return
     */
    public static List<Object> readFile(String filePath, Object object) {
        if (filePath == null) {
            filePath = "";
        }
        filePath = filePath.replaceAll(" ", "");
        File file = new File(filePath);
        List<Object> list = new ArrayList<Object>();
        if (file != null && file.getAbsolutePath() != null && !file.getAbsolutePath().equals("") && file.exists()
                && !file.isDirectory()) {
            BufferedReader bufferedReader = null;
            try {
                bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "utf-8"));
                String buffer = "";
                while ((buffer = bufferedReader.readLine()) != null) {
                    list.add(buffer);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (bufferedReader != null) {
                    try {
                        bufferedReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            if (list.size() == 0) {
                list.add("");
            }
            return list;
        } else {
            return new ArrayList<Object>();
        }
    }

    /**
     * get object list from file .
     * 
     * @param filePath
     * @return
     */
    public static Object[] readFile(String filePath) {
        if (filePath == null) {
            filePath = "";
        }
        filePath = filePath.replaceAll(" ", "");
        File file = new File(filePath);
        List<String> list = new ArrayList<String>();
        if (file != null && file.getAbsolutePath() != null && !file.getAbsolutePath().equals("") && file.exists()
                && !file.isDirectory()) {
            BufferedReader bufferedReader = null;
            try {
                bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "utf-8"));
                String buffer = "";
                while ((buffer = bufferedReader.readLine()) != null) {
                    list.add(buffer);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (bufferedReader != null) {
                    try {
                        bufferedReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return list.toArray();
        } else {
            return new String[0];
        }
    }

    /**
     * determine if the file is directory .
     * 
     * @param path
     * @return
     */
    public static boolean isDirectory(String path) {
        return new File(path).isDirectory();
    }
}

Related

  1. readFile(String fileName, String charSet)
  2. readFile(String fileName, String coloredLineIndicator, boolean useSections, boolean isXML)
  3. readFile(String filename1)
  4. readFile(String fileNm)
  5. readFile(String filePath)
  6. readFile(String filePath)
  7. readFile(String filePath)
  8. readFile(String filePath)
  9. readFile(String filePath)