Java ClassLoader read(String fileName)

Here you can find the source of read(String fileName)

Description

read

License

Apache License

Declaration

public static String[] read(String fileName) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.IOException;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static String[] read(String fileName) {
        if (fileName == null || fileName.trim().length() <= 0) {
            return null;
        }/*from  w ww . ja va 2s . c o  m*/

        ClassLoader classLoader = ClassLoader.getSystemClassLoader();
        URL fileUrl = classLoader.getResource(fileName);
        if (fileUrl == null) {
            return null;
        }
        File file = new File(fileUrl.getFile());
        if (file == null || !file.exists()) {
            return null;
        }

        // Read
        List<String> lines = new ArrayList<String>();
        Scanner scanner = null;
        try {
            scanner = new Scanner(file);
            while (scanner.hasNextLine()) {
                lines.add(scanner.nextLine());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (scanner != null) {
                scanner.close();
            }
        }

        String[] strs = new String[lines.size()];
        lines.toArray(strs);
        return strs;
    }
}

Related

  1. loadProperties(String propFile)
  2. loadPropertiesFromFile(File parent)
  3. loadTextFile(final String location)
  4. loadXml()
  5. locateFile(String name)
  6. readFromFile(Object obj, String fileName)
  7. readSampleJson(String name)
  8. saveProperties(Properties properties, String name)
  9. scanPackage(String path)