Here you can find the source of readFile(String filePath)
Parameter | Description |
---|---|
filePath | a parameter |
public static Object[] readFile(String filePath)
//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(); } }