Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;

import java.util.List;

public class Main {

    public static List<String> readListFromFile(String filePath) throws IOException {
        List<String> result = new ArrayList<String>();

        InputStream fis = null;
        BufferedReader br = null;

        try {
            fis = new FileInputStream(filePath);
            br = new BufferedReader(new InputStreamReader(fis, Charset.forName("UTF-8")));
            String line;
            while ((line = br.readLine()) != null) {
                String trimmed = line.trim();
                if (!trimmed.isEmpty()) {
                    result.add(line);
                }
            }
        } finally {
            if (br != null) {
                br.close();
            }
            if (fis != null) {
                fis.close();
            }
        }

        return result;

    }
}