Here you can find the source of readLine(String filePathAndName, String encoding)
Parameter | Description |
---|---|
filePathAndName | String file name with absolute path |
encoding | String file encoding |
public static List<String> readLine(String filePathAndName, String encoding)
//package com.java2s; //License from project: Open Source License import java.io.*; import java.util.*; public class Main { public static String DEFAULT_ENCODING = "UTF-8"; /**// www. j a v a2 s .c o m * read the line content of text file, in array <br> * <b>PS.</b> size of element = number of line * * @param filePathAndName * String file name with absolute path * @param encoding * String file encoding * @return Array of string content separate by "\n" (new line) */ public static List<String> readLine(String filePathAndName, String encoding) { if (encoding == null) encoding = DEFAULT_ENCODING; String string = ""; List<String> stringList = new ArrayList<>(); long i = 0; BufferedReader reader = getBuffer(filePathAndName, encoding); if (reader == null) return stringList; try { String data; while ((data = reader.readLine()) != null) { stringList.add(data); } return stringList; } catch (Exception e) { return stringList; } } /** * read the specified line content of text file * * @param filePathAndName * String file name with absolute path * @param rowIndex * the row number that want to read * @return String text content of the line */ public static String readLine(String filePathAndName, long rowIndex) { return readLine(filePathAndName, DEFAULT_ENCODING).get(Math.toIntExact(rowIndex)); } /** * convert string of file path to bufferReader * * @param fileAndPath * String file name with absolute path * @param encoding * (optional) can be null, Default -> {@link #DEFAULT_ENCODING} * @return bufferReader of file */ private static BufferedReader getBuffer(String fileAndPath, String encoding) { if (encoding == null || encoding.equals("")) encoding = DEFAULT_ENCODING; try { return new BufferedReader(new InputStreamReader(new FileInputStream(fileAndPath), encoding)); } catch (FileNotFoundException | UnsupportedEncodingException e) { return null; } } }