Android Text File Read readClasspathTextFile(String filename)

Here you can find the source of readClasspathTextFile(String filename)

Description

read Classpath Text File

License

Open Source License

Declaration

public static String[] readClasspathTextFile(String filename) 

Method Source Code

/*/*from  w  w w . j av  a2s.  c o  m*/
 * *************************************************************************************
 *  Copyright (C) 2008 EsperTech, Inc. All rights reserved.                            *
 *  http://esper.codehaus.org                                                          *
 *  http://www.espertech.com                                                           *
 *  ---------------------------------------------------------------------------------- *
 *  The software in this package is published under the terms of the GPL license       *
 *  a copy of which has been included with this distribution in the license.txt file.  *
 * *************************************************************************************
 */

import java.io.File;
import java.io.FileInputStream;
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[] readClasspathTextFile(String filename) {
        String filenameCp = findClasspathFile(filename);
        if (filenameCp == null) {
            throw new RuntimeException("Failed to find file '" + filename
                    + "' in classpath");
        }
        List<String> lines = new ArrayList<String>();
        try {
            FileInputStream fis = new FileInputStream(filenameCp);
            Scanner scanner = new Scanner(fis);
            try {
                while (scanner.hasNextLine()) {
                    lines.add(scanner.nextLine());
                }
            } finally {
                scanner.close();
                fis.close();
            }
        } catch (IOException ex) {
            throw new RuntimeException("Failed to read file '" + filename
                    + "': " + ex.getMessage(), ex);
        }
        return lines.toArray(new String[lines.size()]);
    }
    public static String findClasspathFile(String filename) {
        URL url = FileUtil.class.getClassLoader().getResource(filename);
        if (url != null) {
            return url.getFile();
        }
        return null;
    }
}

Related

  1. read(File file, boolean raw)
  2. read(String file)
  3. read(String fileName)
  4. read(String pathName)
  5. readCipherSuites(String file)
  6. readFile(File file)
  7. readFile(File file)
  8. readFile(File file)
  9. readFile(File file, String encoding)