Java InputStreamReader Read readFileAddLine(String filePath, Object object)

Here you can find the source of readFileAddLine(String filePath, Object object)

Description

get the file content convert to list .

License

Open Source License

Parameter

Parameter Description
filePath a parameter
object a parameter

Declaration

public static List<Object> readFileAddLine(String filePath, Object object) 

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 av a2  s  .  c o m*/
     * get the file content convert to list .
     * 
     * @param filePath
     * @param object
     * @return
     */
    public static List<Object> readFileAddLine(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 = "";
                int lineNumber = 1;
                while ((buffer = bufferedReader.readLine()) != null) {
                    if (buffer.startsWith("==========")) {
                        if (lineNumber < 10) {
                            buffer = buffer.replaceFirst("=", "[  " + lineNumber + "  ]  ");
                        } else if (lineNumber < 100) {
                            buffer = buffer.replaceFirst("=", "[  " + lineNumber + " ]  ");
                        } else {
                            buffer = buffer.replaceFirst("=", "[ " + lineNumber + " ]  ");
                        }
                        lineNumber++;
                    }
                    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>();
        }
    }

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

Related

  1. readFile(String path)
  2. readFile(String path)
  3. readFile(String path)
  4. readFile(String path, String charset)
  5. readFile(ZipInputStream zin)
  6. readFileAll(File file, String charsetName)
  7. readFileAsArray(String path)
  8. readFileAsJson(String path)
  9. readFileAsList(String path)